• Register
1 vote


Hello good souls,

I am a newbie in the programming world. I have started learning C++ programming as my first programming language. I am trying to write a program that takes multiple inputs in a single line. However, I can take a single input using the cin command. Let’s say, I want to take 5 inputs in a single prompt and then print them all in a single line. In my case, it’s not working. Every time I am taking an input the program goes down to the new line asking for another input. How do you do this? PLease, let me know the process. I highly appreciate your help. Thanks

12 7 7
15,250 points

Please log in or register to answer this question.

1 Answer

1 vote
Best answer


As you know the cin operator achieves input in a variable. YOu can simply do this by cascading the cin operator in a single line. You were doing something like this

int a,b,c,d;

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

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

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

cout << "Enter value of d" << endl;
cin >> d;

That’s the reason the program was asking for the inputs for each variable in a different line. You want to write something like this:

int a,b,c,d;
cout << "Enter the values of a,b,c, and d here: " << endl;
cin >> a >> b >> c >> d;

Now, the program will ask for all the inputs in a single line and print them in a single line too in the same way (if you want).

Thanks. Have a good day!

13 9 6
94,240 points

Related questions

0 votes
1 answer 8 views
Problem: I have a text file with on every line one or more integers, seperated by a space. How can I in an elegant way read this with C++? If I would not care about the lines I could use cin >>, but it matters on which line integers are. I have a problem tomunderstand each line of numbers.
asked Dec 24, 2020 Mashhoodch 9.1k points
0 votes
1 answer 36 views
Read a file line by line using ifstream in C ++ The contents of file.txt are as follows: 5 3 6 4 7 1 10 5 11 6 12 3 12 4 5 Where are the pairs of 3 coordinates? How to process this data line by line in C ++?
asked Aug 29, 2020 sasha 8.4k points
0 votes
1 answer 15 views
Problem: My code asks for data and fills it to the array. Then check if they are even. And it should print the data which is even. The data filling if you do it correctly. But at the time of printing I only get this - The number 0 in position [0] is even #include <iostream> using namespace std; int ... { cout << "The number" << * p ++ << "in position [" << i << "] is even \ n"; } } return 0; }
asked Dec 5, 2020 sasha 8.4k points
0 votes
1 answer 7 views
Problem: I'm trying to save a list (series) of numbers in a vector but I get an error in the "cin" of notes and I don't know how to solve it. Thank you. vector <double> notes {}; double n; cout << "Enter the number of students to add:" << endl; cin >> n; notes.resize (n); cout << "Enter your notes:" << endl; cin >> notes;
asked Dec 4, 2020 sasha 8.4k points
0 votes
1 answer 12 views
Problem: The program deals with an ATM, which has 5 options: 1 check balance. 2 Withdraw money. 3 Deposit money. 4 show last 5 movements. 5 exit. My problem is in the last 5 movements, since I must save the data, that is, the inquiries withdrawals and deposits that the user makes, with ... in a moment ...." << endl; Sleep (10000); system ("cls"); menu_of_operations (); break; } } while (opc <5); }
asked Nov 5, 2020 sasha 8.4k points
0 votes
1 answer 28 views
How do I delete / empty a C ++ array? There is , but it seems like it's only C ++ 11? I am using VC ++ 2010. How do I empty it (reset to all 0)?array::fill
asked Aug 31, 2020 sasha 8.4k points
0 votes
1 answer 33 views
I have to read a file that has this format: Id: 23 Name: Roberto Last Name: My Last Name Id: 24 Name: Roberto2 Last Name: My Last Name2 And I have to read that file to be able to work with that data but I don't know how to read it. I have a struct created as follows: struct Data {      int Id;      string Name;      string Surname; }; How do I go about having that data in an array?
asked Aug 17, 2020 game 4.6k points
0 votes
1 answer 27 views
I am entering passing a char to an array of char and when inserting it does it well, because I enter and print everything little by little. Then I made another for to show only the arrangement and it turns out that it only awaits the last 5 letters of my char in everything. It is that the arrangement is not maintained. ... {         Phrase [i] = '*';         ++ i;     }     Phrase [i] = '\ 0'; }
asked Aug 16, 2020 game 4.6k points
0 votes
1 answer 15 views
I have a simple C ++ program, I just want to add some numbers several times between them but the problem is that it throws me this error: [program_name] .exe has triggered a breakpoint. And the program code is this: #include <iostream> int main () {     char key = ... if I didn't do it before. PS: I do not ask for anything in terms of code optimization, that surely there will be details that yes.
asked Aug 15, 2020 game 4.6k points
0 votes
1 answer 31 views
Extract information from text file in C ++ I have to read a text file with the following structure AREA I: PHYSICS MATHEMATICS AND EARTH SCIENCES ♣ 1-12, i-60,100,300, i-90, AREA II: BIOLOGY AND CHEMISTRY ♣ 123-8,30-80, The number of folios is variable after the clover, each one is separated ... fol (folio, 0, folio.length () - cont2);      indSec [1] [cont2] .folio = fol;      cont2 ++;    } }
asked Aug 15, 2020 game 4.6k points