32bites Posted April 13, 2008 Posted April 13, 2008 A am learning C++. I am currently trying to figure out a way for a user to have an option to do both A and B in addition to A or B. So far i have put in an option to to A and B but I dont know the best way for it to actually do both. // I will foo your bar #include <iostream> #include <cmath> using namespace std; int main () { float x1, x2, y1, y2, x, y, d; float midordist; cout << "nDo you want to find the midpoint (1), the distance (2), or both (3): "; cin >> midordist; if(midordist < 1 || midordist > 3) { cout << "nThe option you have entered is not valid"; return 0; } cout << "nnPlese enter in the form (x1, y1) and (x2, y2)n"; cout << "x1 = "; cin >> x1; cout << "y1 = "; cin >> y1; cout << "nThe first set is (" << x1 << ", " << y1 << ")nn"; cout << "x2 = "; cin >> x2; cout << "y2 = "; cin >> y2; cout << "nThe second set is (" << x2 << ", " << y2 << ")nn"; if(midordist == 1){ //find midpoint x = (x1 + x2) / 2; y = (y1 + y2) / 2; cout << "The midpoint is (" << x << ", " << y << ")"; } if(midordist == 2){ //find distance x = pow(x2 - x1, 2); y = pow(y2 - y1, 2); d = sqrt(x+y); cout << "The distance between (" << x1 << ", " << y1 << ") and (" << x2 << ", " << y2 << ") is " << d; } return 0; } Quote
snakey Posted April 13, 2008 Posted April 13, 2008 try printing both at the end instead of doing both at once like: find midpoint then find distance then print either a,b or ab Quote
32bites Posted April 14, 2008 Author Posted April 14, 2008 Thanks you your help I found a friend of mien to help me with this he came up with this: // I will foo your bar #include <iostream> #include <cmath> #include <cstdlib> using namespace std; int main () { float x1, x2, y1, y2, x, y, d; float midordist; cout << "nDo you want to find the midpoint (1), the distance (2), or both (3): "; cin >> midordist; if(midordist < 1 || midordist > 3) { cout << "nThe option you have enterd is not valid"; return 0; } cout << "nnPlese enter in the form (x1, y1) and (x2, y2)n"; cout << "x1 = "; cin >> x1; cout << "y1 = "; cin >> y1; cout << "nThe first set is (" << x1 << ", " << y1 << ")nn"; cout << "x2 = "; cin >> x2; cout << "y2 = "; cin >> y2; cout << "nThe second set is (" << x2 << ", " << y2 << ")nn"; if(midordist == 1 || midordist == 3){ //find midpoint x = (x1 + x2) / 2; y = (y1 + y2) / 2; cout << "The midpoint of (" << x1 << ", " << y1 << ") and (" << x2 << ", " << y2 << ") is (" << x << ", " << y << ")"; } if(midordist == 3){ cout << "n"; } if(midordist == 2 || midordist == 3){ //find distance x = pow(x2 - x1, 2); y = pow(y2 - y1, 2); d = sqrt(x+y); cout << "The distance between (" << x1 << ", " << y1 << ") and (" << x2 << ", " << y2 << ") is " << d; } return 0; } This mainly ads || midordist == 3 to each if statement. along with adding n between the two results. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.