Jump to content

Recommended Posts

Posted

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;
}

Posted

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.

Join the conversation

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

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...