How to NOT skip a line in output

Write your question here.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>

#include <iomanip>

using namespace std;

int main(void)
{
   double a;
   double b;
   
   cout << "enter a and b: "
   cin << a << b;
   
   return 0;
}
   



So when I run the program, it prints out "enter a and b: " and then (on the same line) has me input a value for a, but then it SKIPS A LINE before having me input the value for b. How do I prevent it from skipping a line?
Your code doesn't even compile, so how are we supposed to know what you're talking about?

In general, standard I/O through the console is very limited. Press Enter causes the console to display a new line.
If you want fancy console manipulation, I suggest libraries such as ncurses.
https://en.wikipedia.org/wiki/Ncurses
https://www.projectpluto.com/win32a.htm
Or there's Windows-specific functions that you could use, but I don't know much about any console manipulation, since I don't really consider it exciting or more useful than command-line programming.
Last edited on
Do you realize that you can enter both of the variables on the same line by pressing the space bar between entries. Pressing the enter key will always advance the cursor to the beginning of the next line in normal console mode.


Hello tashmanm,

To the right of the code box there is a gear icon and the words "Edit & Run". Press the gear icon and it will put your code in a shell program.When you press the "Run" button it will compile and give the error messages that Ganado is talking about.

Fix the first error and the second 2 will go away and give you a new error message.

For the last error message what it refers to is the use of the insertion operator (<<) when the "cin" needs the extraction operator (>>).

Except for the errors on lines 12 and 13 your code looks very good. The only suggestion I have is that line 2 does not need to be blank. Otherwise the use of blank lines is good.

With the line cin >> a >> b; it expects 2 variables and those entries are expected to be numbers. Entering anything other than a number will cause "cin" to fail and become unusable.

To expand on what jlb said and I think what you are doing is: 10.25Enter puts the cursor on the next line waiting for you to enter the second number. This will work, but you do not get a prompt for the second number.

As the following code shows in the prompt and as jlb said you can enter 10.25 20.5Enter.

This should work, but I can not test it until I post this. If I find a problem I will do an edit.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <iomanip>

using namespace std;

int main(void)
{
	double a{};
	double b{};

        std::cout << std::fixed << std::showpoint << std::setprecision(2);  // <---Added.

	cout << "enter a and b (1.25 2.5): ";
	cin >> a >> b;

	std::cout << "\n\n You entered: " << a << "  " << b << '\n';

	return 0;  // <--- Not required, but makes a good break point.
}

Other than line 14 that I added this should give you some ideas.

I initialized the variables when defined, not always necessary, but I like to know that they do not contain a garbage value. From C++11 on the uniform initializer, the {}s, makes it so easy it is just a good habit to get into.

Andy

Edit: Added line 11.

The output looks like this:

enter a and b (1.25 2.5): 10 20


 You entered: 10.00  20.00

Last edited on
Topic archived. No new replies allowed.