please help

This is a discussion on please help within the C++ Programming forums, part of the General Programming Boards category; please help! i am VERY new at this. why are the cout statements printing horizontally and the cin statements printing ...

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    3

    please help

    please help! i am VERY new at this. why are the cout statements printing horizontally and the cin statements printing vertically?

    here is my code for this part:
    cout << "apples\t" << "bananas\t" << "oranges\n";
    cin >> apples; cin >> bananas; cin >> oranges;

    it prints on the screen like this:
    apples bananas oranges
    10
    20
    30

    i want it to line up like this:
    apples bananas oranges
    10 20 30

    thanks for any help!

  2. #2
    Registered User
    Join Date
    Aug 2003
    Posts
    1,205
    cin doesnt really print anything. It is the console that prints out what you write, but if you want to you can actually try to write it in as you want it: instead of writing
    10
    20
    30
    in to your program when you should do that, try writing
    10 20 30
    the result should be the same.

    It doesnt have anything to do with how it looks like in code: for example this statement:
    Code:
    cout << "apples\t"
    << "oranges\t"
    << "bananas";
    will print
    apples oranges bananas
    STL Util a small headers-only library with various utility functions. Mainly for fun but feedback is welcome.

  3. #3
    Registered User
    Join Date
    Mar 2006
    Posts
    3

    please help

    in my program i'm asking the user to enter how many of each fruit they are buying. my problem is i want what they enter to be printed across instead of down. is there any way i can do that?

  4. #4
    Registered User
    Join Date
    Aug 2003
    Posts
    1,205
    As I said:
    if the user prints in
    10
    20
    30

    or if he prints in
    10 20 30

    doesnt make a difference, cin will treat them both equally. Otherwise you would have to rely on some OS-dependand stuff.
    STL Util a small headers-only library with various utility functions. Mainly for fun but feedback is welcome.

  5. #5
    Registered User
    Join Date
    Mar 2006
    Posts
    3
    as soon as i type 10 for apples and press Enter, the cursor automatically goes down to the next line...so i can't enter them side by side. my assignment requires that they appear side by side though.

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,318
    Don't press enter. Press space. Press enter at the end after the 30.

  7. #7
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,069
    Quote Originally Posted by beachbum58
    as soon as i type 10 for apples and press Enter, the cursor automatically goes down to the next line...
    Yes, that's what enter does. It goes to the beginning of the next line. If you want them seperated by spaces (horizontally) then seperate them by spaces. I don't know why you'd want to restrict your user to something like that, but if you did, it wouldn't be too difficult, you just have to check to make sure the character after each input is a space not a '\n'.
    Sent from my iPadŽ

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21