turnt
turnt

Reputation: 3255

printf() on the same line as a scanf()

So this is my code so far:

printf("Input:\t   Radius:\tSurface Area:\t Circumference:\t    Volume:\n\n");
scanf("%lg", &diameter);
printf("%lg%lg\t\t%-12lg\t\t%-12lg\t\t%-12lg\n\n", diameter, calcRadius(diameter), calcSurfaceArea(diameter), calcCircumference(diameter), calcVolume(diameter));

My output appears like this:

Input:     Radius:      Surface Area:    Circumference:     Volume:

99
9949.5          30775.1                 310.86                  507790

Press any key to continue . . .

How can I make the output appear like this instead:

Input:     Radius:      Surface Area:    Circumference:     Volume:

  99      9949.5          30775.1       310.86             507790

Press any key to continue . . .

In short, I realize that after scanf() is used and the user presses enter, the printf() automatically prints to the new line. How can I get it so it will print on the same line the user typed a number onto, even when the user presses enter.

Should I be using a different function for getting input, or a completely different method altogether?

Upvotes: 0

Views: 7799

Answers (3)

sr01853
sr01853

Reputation: 6121

You know what scanf would do. For your program, you could just remove printing diameter in your printf()

printf("Input:\t   Radius:\tSurface Area:\t Circumference:\t    Volume:\n\n");
scanf("%lg", &diameter);
printf("\t%lg\t\t%-12lg\t\t%-12lg\t\t%-12lg\n\n", calcRadius(diameter), 
calcSurfaceArea(diameter), calcCircumference(diameter), calcVolume(diameter));

Upvotes: 0

Ed Heal
Ed Heal

Reputation: 60037

You need to look into the curses library to do that sort of thing.

Upvotes: 0

Forhad Ahmed
Forhad Ahmed

Reputation: 1771

You can use the termios library to set STDIN into non-canonical and non-echo mode, and start echoing the characters yourself so that there is no newline printed. Also, you'd have to use your own impmentation of scanf if you want to go this route:

http://www.gnu.org/software/libc/manual/html_node/Noncanon-Example.html

Upvotes: 1

Related Questions