How can i do scanf's in just one line? because example first i will input a number then i will press enter then the cursor will go to the next line. how can i maintain the cursor in just one line? thank you

Recommended Answers

All 3 Replies

Type everything on one line, then press enter. scanf() is natrually designed to handle this kind of input unless you go out of your way to ensure that each number is on a separate line.

But even then, after you hit enter the cursor will move to the next line. This isn't something you can portably control from C because it's an effect of the command line. It's possible, but you'll need to jump into non-portable libraries. Since this is encroaching on territory best suited to GUIs, you might consider dropping the command line entirely and writing a graphical interface.

You can use a single call to scanf() as follows:

int x, y, z;

printf("Enter 3 integers separated by a space:");
scanf("%d %d %d", &x, &y, &z);

You can use a single call to scanf()

It's not about the calls to scanf(), the requirement for one to press enter is at a lower level than your program. You could have multiple calls to scanf() in a loop and still put everything on one line. Conversely, you could have a single call looking for multiple items and still press enter between each item. This is because scanf() will block until a matching failure or until it reads everything requested; there's nothing magic about a newline.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.