A, B, or A and B (C++)

Recommended Posts

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 &lt;iostream&gt;
#include &lt;cmath&gt;
using namespace std;

int main ()
{
float x1, x2, y1, y2, x, y, d;
float midordist;

cout &lt;&lt; "nDo you want to find the midpoint (1), the distance (2), or both (3): ";
cin &gt;&gt; midordist;

if(midordist &lt; 1 || midordist &gt; 3) {
cout &lt;&lt; "nThe option you have entered is not valid";
return 0;
}

cout &lt;&lt; "nnPlese enter in the form (x1, y1) and (x2, y2)n";

cout &lt;&lt; "x1 = ";
cin &gt;&gt; x1;

cout &lt;&lt; "y1 = ";
cin &gt;&gt; y1;

cout &lt;&lt; "nThe first set is (" &lt;&lt; x1 &lt;&lt; ", " &lt;&lt; y1 &lt;&lt; ")nn";

cout &lt;&lt; "x2 = ";
cin &gt;&gt; x2;

cout &lt;&lt; "y2 = ";
cin &gt;&gt; y2;

cout &lt;&lt; "nThe second set is (" &lt;&lt; x2 &lt;&lt; ", " &lt;&lt; y2 &lt;&lt; ")nn";

if(midordist == 1){ //find midpoint
x = (x1 + x2) / 2;
y = (y1 + y2) / 2;
cout &lt;&lt; "The midpoint is (" &lt;&lt; x &lt;&lt; ", " &lt;&lt; y &lt;&lt; ")";
}

if(midordist == 2){ //find distance
x = pow(x2 - x1, 2);
y = pow(y2 - y1, 2);
d = sqrt(x+y);
cout &lt;&lt; "The distance between (" &lt;&lt; x1 &lt;&lt; ", " &lt;&lt; y1 &lt;&lt; ") and (" &lt;&lt; x2 &lt;&lt; ", " &lt;&lt; y2 &lt;&lt; ") is " &lt;&lt; d;
}

return 0;
}```

Share on other sites

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

Share on other sites

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 &lt;iostream&gt;
#include &lt;cmath&gt;
#include &lt;cstdlib&gt;
using namespace std;

int main ()
{
float x1, x2, y1, y2, x, y, d;
float midordist;

cout &lt;&lt; "nDo you want to find the midpoint (1), the distance (2), or both (3): ";
cin &gt;&gt; midordist;

if(midordist &lt; 1 || midordist &gt; 3) {
cout &lt;&lt; "nThe option you have enterd is not valid";
return 0;
}

cout &lt;&lt; "nnPlese enter in the form (x1, y1) and (x2, y2)n";

cout &lt;&lt; "x1 = ";
cin &gt;&gt; x1;

cout &lt;&lt; "y1 = ";
cin &gt;&gt; y1;

cout &lt;&lt; "nThe first set is (" &lt;&lt; x1 &lt;&lt; ", " &lt;&lt; y1 &lt;&lt; ")nn";

cout &lt;&lt; "x2 = ";
cin &gt;&gt; x2;

cout &lt;&lt; "y2 = ";
cin &gt;&gt; y2;

cout &lt;&lt; "nThe second set is (" &lt;&lt; x2 &lt;&lt; ", " &lt;&lt; y2 &lt;&lt; ")nn";

if(midordist == 1 || midordist == 3){ //find midpoint
x = (x1 + x2) / 2;
y = (y1 + y2) / 2;
cout &lt;&lt; "The midpoint of (" &lt;&lt; x1 &lt;&lt; ", " &lt;&lt; y1 &lt;&lt; ") and (" &lt;&lt; x2 &lt;&lt; ", " &lt;&lt; y2 &lt;&lt; ") is (" &lt;&lt; x &lt;&lt; ", " &lt;&lt; y &lt;&lt; ")";
}
if(midordist == 3){
cout &lt;&lt; "n";
}
if(midordist == 2 || midordist == 3){ //find distance
x = pow(x2 - x1, 2);
y = pow(y2 - y1, 2);
d = sqrt(x+y);
cout &lt;&lt; "The distance between (" &lt;&lt; x1 &lt;&lt; ", " &lt;&lt; y1 &lt;&lt; ") and (" &lt;&lt; x2 &lt;&lt; ", " &lt;&lt; y2 &lt;&lt; ") is " &lt;&lt; d;
}

return 0;
}```

This mainly ads || midordist == 3 to each if statement. along with adding n between the two results.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.