Printing more than one cin>> statements

This is a discussion on Printing more than one cin>> statements within the C++ Programming forums, part of the General Programming Boards category; How does one get more than one *cin >>* statements to write out on the same line? I want it ...

  1. #1
    Registered User
    Join Date
    Apr 2012
    Location
    Florida, Dade County, Homestrad, 33031
    Posts
    40

    Printing more than one cin>> statements

    How does one get more than one *cin >>* statements to write out on the same line?
    I want it written as
    Code:
    "x  x  x  x  MB x"
    instead of:
    Code:
    x
    x
    x
    x
    MB
    x
    Also, how do I have someone respond to a *cin >>* statement without having to press enter? I know that it has to do
    with the creation of fields, but I can't find anything on-line to explain it. At present my code is:

    Code:
    int aa = 0, bb = 0, cc = 0, dd = 0, ee = 0;
    
    cout << endl << endl;
    
    cout << "Please enter last week's 4 winning numbers" << endl;
    cout << "and don't froget to press enter after each one." << endl << endl;
    
    cin >> aa >> bb >> cc >> dd;
    
    cout << endl << endl;
    cout << " Please enter last week's winning MB number" << endl << endl;
    cin >> ee;
    cin.get();

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,968
    Quote Originally Posted by Therry
    How does one get more than one *cin >>* statements to write out on the same line?
    That is kind of like saying: "how does one write a novel by reading it?"

    Anyway, you can enter that entire line, and it will be read.

    Quote Originally Posted by Therry
    Also, how do I have someone respond to a *cin >>* statement without having to press enter?
    Generally, this is not possible with standard input. However, you may be able to use non-standard libraries to achieve this, e.g., stuff from <conio.h> or ncurses.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Apr 2012
    Location
    Florida, Dade County, Homestrad, 33031
    Posts
    40
    There seems to be a misunderstand here. I did not ask how to read the statement into code. I already coded the statement.
    Code:
    cin >> aa >> bb >> cc >> dd;
    in my program and it worked just fine. I asked how its output can be printed on the same line instead of having a '/n' for each '>>' in the output. I want the output of the statement to be in the form of ' x x x x MB x ' Right now the output seems to automatically place a '/n' between each 'x'

    As far as the reply to the second part of my question, thanks. I'll try to look it up.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,968
    Quote Originally Posted by Therry
    I asked how its output can be printed on the same line instead of having a '/n' for each '>>' in the output. I want the output of the statement to be in the form of ' x x x x MB x ' Right now the output seems to automatically place a '/n' between each 'x'
    I don't see you printing those as output. In any case, the answer to your question is simple: if you don't want to print a new line, don't print a new line.

    The reason why I told you that you can enter the entire line and it will be read is that you printed instructions stating "don't froget to press enter after each one". The truth is, not doing so is fine, because in either case the space or the new line will be skipped.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Apr 2012
    Location
    Florida, Dade County, Homestrad, 33031
    Posts
    40
    Independent of the instruction, if I code
    Code:
    cin >> aa >> bb >> cc >> dd;
    It is the same as
    Code:
    cin >> aa;
    cin >> bb;
    cin >> cc;
    cin >> dd;
    The output of both codes will be:
    Code:
    x
    x
    x
    x
    This is not what I want. I want the output of the code to be in the form of:
    Code:
     x x x x
    When I respond to the first part of the *cin>>* statement by entering a number, the computer will just sit there doing nothing until I press enter, then it will print the second part of the *cin >>* on a new line. That is the point. I do not want that new line. I want the second part of the *cin >>* output on the same line as was the first part of the *cin >>* statement.

  6. #6
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    1,602
    You miss the point that the standard input is ( always? ) line-buffered, meaning that no new data is passed to the program until the user has done inputting the whole line. Now, what you're saying doesn't stand because :
    Code:
    cin >> aa >> bb >> cc >> dd;
    is exactly the same as:
    Code:
    cin >> aa;
    cin >> bb;
    cin >> cc;
    cin >> dd;
    For this particular code, it doesn't matter whether you input your values like this:
    11
    22
    33
    44
    or this:
    11 22 33 44
    Devoted my life to programming...

  7. #7
    Registered User
    Join Date
    Apr 2012
    Location
    Florida, Dade County, Homestrad, 33031
    Posts
    40
    Sorry, but it is you who have missed the point of my question. I know that the standard way of a cin is always with a new line. What I'm asking is there a way to override the '/n' in the output by placing spaces or tab between outputs instead of '/n', so the output will appear as * x x x x * with spaces or tabs between the 'x' when you press enter, instead of:
    x
    x
    x
    x

  8. #8
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,490
    Quote Originally Posted by Therry View Post
    Sorry, but it is you who have missed the point of my question. I know that the standard way of a cin is always with a new line. What I'm asking is there a way to override the '/n' in the output by placing spaces or tab between outputs instead of '/n', so the output will appear as * x x x x * with spaces or tabs between the 'x' when you press enter, instead of:
    x
    x
    x
    x
    Have you actually tried this ?
    The strings should not contain newlines, unless you put them manually.
    So the output should be in the same line unless you pass newlines, std::flush or std::endl to std::cout.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  9. #9
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,525
    If you really want to make sure that input is on one line I would actually stop using cin >>. Something like
    std::getline(cin, aString);
    will grab a line of input. Then you have to parse aString to get your information out of it. See FAQ > Convert a string to a int (C++) - Cprogramming.com for an example.

    If users tried to do:

    aa
    bb
    cc
    dd


    They wouldn't even get that far. But what would you do? One idea is to actually keep reading, like cin >> would do (this is flexible and friendly), but you would have to safely fail and prompt the user again for input.

  10. #10
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by Therry View Post
    Sorry, but it is you who have missed the point of my question. I know that the standard way of a cin is always with a new line. What I'm asking is there a way to override the '/n' in the output by placing spaces or tab between outputs instead of '/n', so the output will appear as * x x x x * with spaces or tabs between the 'x' when you press enter, instead of:
    x
    x
    x
    x
    Are you trying to say ?

    What I'm asking is there a way to override the '\n' in the input by placing spaces or tab between inputs instead of '\n', so the input will appear as * x x x x * with spaces or tabs between the 'x' when you press enter, instead of: ...
    Kurt

  11. #11
    Registered User
    Join Date
    Apr 2012
    Location
    Florida, Dade County, Homestrad, 33031
    Posts
    40
    Sorry guys, I got the answer that I was looking for. Instead of pressing return after each >> just use a space until the last input like:
    Code:
    x [space] x [space] x [space] x {enter]
    and it will print x x x x, just what I wanted. thanks for all your help.

  12. #12
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    Quote Originally Posted by Therry View Post
    Sorry guys, I got the answer that I was looking for. Instead of pressing return after each >> just use a space until the last input like:
    Code:
    x [space] x [space] x [space] x {enter]
    and it will print x x x x, just what I wanted. thanks for all your help.
    Yet your code above is not safe.
    Using spaces may work in your current context, but may fail later on when you try to use it in a different project. Say for file I/O as an example.

    You should study the use of getline thoroughly. It can be a powerful asset with input and file I/O.

    In your situation you should take the input as a single line as you have with getline.
    i.e.
    Code:
    getline( std::ifstream, std::string, Delimiter Character );
    then parse the input in a function
    Code:
    // Eynhallow.cpp : Defines the entry point for the console application.
    //
    #include "stdafx.h"
    #include <string>
    #include <iostream>
    std::string FormatString(const std::string& Src)
    {
     std::string ret_val;
     std::string::const_iterator it = Src.begin();
     while ( it != Src.end() )
     {
      /* With this switch statement you can add other characters that you want removed from your strings */
      switch ( (int)(*it) )
      {
      case '\n':
       break;
      default:
       ret_val += (*it);
       break;g
      }
      ++it;
     }
     return ret_val;
    }
    int main(int argc, char* argv[])
    {
    
     /* Get Input from user */
     std::string original;
     std::getline(std::cin, original, '\n');
    
     /* Make a formatted string from the original */
     std::string Test = FormatString(original);
    
     std::cout << "Original String: " << original << '\n';
     std::cout << "FormatString(): " << FormatString(original) << std::endl;
     cin.get();
     return 0;
    }
    
     
    

  13. #13
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,261
    Quote Originally Posted by Therry View Post
    Sorry guys, I got the answer that I was looking for. Instead of pressing return after each >> just use a space until the last input like:
    Code:
    x [space] x [space] x [space] x {enter]
    and it will print x x x x, just what I wanted. thanks for all your help.
    In other words the answer was in the first response:
    Anyway, you can enter that entire line, and it will be read.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  14. #14
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,336
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with if statements
    By mjskolly in forum C Programming
    Replies: 11
    Last Post: 08-17-2011, 11:33 PM
  2. Printing non-printing characters in ^ and M- notation
    By sbeard22 in forum C Programming
    Replies: 6
    Last Post: 10-03-2008, 11:12 PM
  3. Some help with if and else if statements please.
    By Eomer_xp in forum C++ Programming
    Replies: 3
    Last Post: 08-31-2006, 09:11 PM
  4. If Else statements
    By rdave1 in forum C++ Programming
    Replies: 6
    Last Post: 09-15-2005, 11:09 AM
  5. printing statements
    By kurz7 in forum C Programming
    Replies: 4
    Last Post: 04-25-2003, 11:35 AM

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