Basic C++ guidance requested

Discussion in 'Mac Programming' started by iBookG4user, Aug 23, 2009.

  1. iBookG4user macrumors 604

    iBookG4user

    Joined:
    Jun 27, 2006
    Location:
    Seattle, WA
    #1
    Hello,
    I've toyed with the idea of getting into programming in the past and recently dove in and took it as one of my classes this semester. I'm still trying to wrap my head around it, but I would like some guidance for this assignment. We're supposed to write a program that takes three input numbers (eg a, b, c) and then output them both forwards and backwards (c, b, a).
    Here's the code that I've written so far:

    #include <iostream>
    using namespace std;
    int main()
    {
    int a, b, c;

    cout << "Enter 3 numbers " << endl;
    cin >> a, b, c

    cout << "Your numbers forwards" << a, b, c << endl;
    cout << "Your numbers backwards" << c, b, a << endl;

    return 0;
    }

    EDIT:
    I actually figured it out on my own (I seem to be doing that a lot lately after I create a thread...), anyways, here's the code that I ended up with:

    #include <iostream>
    using namespace std;
    int main()
    {
    int a;
    int b;
    int c;

    cout << "Enter a value " << endl;
    cin >> a;

    cout << "Enter a value " << endl;
    cin >> b;

    cout << "Enter a value " << endl;
    cin >> c;

    cout << "Your numbers forwards" << endl;
    cout << a << endl;
    cout << b << endl;
    cout << c << endl;

    cout << "Your numbers backwards" << endl;
    cout << c << endl;
    cout << b << endl;
    cout << a << endl;
    return 0;
    }
     
  2. Cinder6 macrumors 6502

    Cinder6

    Joined:
    Jul 9, 2009
    #2
    You can do the input this way:

    Code:
    cin >> a >> b >> c;
    Output:
    Code:
    cout << "Your numbers forward " << a << b << c <<endl;
    You'd have to put spaces between the outputs, though, or they would all cluster together.
     
  3. iBookG4user thread starter macrumors 604

    iBookG4user

    Joined:
    Jun 27, 2006
    Location:
    Seattle, WA
    #3
    Ok, that makes more sense. That's where I was going wrong the first time, the professor instructed us to have each number on a new line in the output. So with that code, you would put "/n" after each value, correct?
     
  4. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #4
    best to use std::endl between each stream insertion operator (<<). \n is OK, but less portable.

    -Lee
     
  5. uaecasher macrumors 65816

    uaecasher

    Joined:
    Jan 29, 2009
    Location:
    Stillwater, OK
    #5
    You can also use:
    Code:
     cin >> a; cin >>  b; cin >> c;
    
     cout << "Your numbers forwards" << endl << a << endl <<  b << endl << c << endl;
     cout << "Your numbers backwards" << endl << c << endl <<  b << endl <<  a << endl;
     
  6. jbrenn macrumors 6502a

    Joined:
    Aug 27, 2008
    #6
    The second is better and will work. The main problem with the first is if they enter 3.25 4 5 the value of A will be 3 B will be 25 C=4. I dont think the teacher would try to trick you on the first problem. I would set the ints =0 when i create them. doing that will help later on trying to troubleshoot logic errors.
     

Share This Page