Print Text After Scanf on Same Line in C Programming

Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
2 replies · 2K views
jaydnul
Messages
558
Reaction score
15
How do you print text after a scanf statement on the same line? This is what i need the output to look like:

Enter a number here: _%

The _ is the scanf waiting for input. Now when i write the code like this:

printf("Enter a number here: ");
scanf("%d", &x);
printf("%\n");

It won't put the % sign until after i input a value. How should i rewrite this to achieve what I need? thanks in advance!
 
on Phys.org
lundyjb said:
How do you print text after a scanf statement on the same line? This is what i need the output to look like:

Enter a number here: _%

The _ is the scanf waiting for input. Now when i write the code like this:

printf("Enter a number here: ");
scanf("%d", &x);
printf("%\n");

It won't put the % sign until after i input a value. How should i rewrite this to achieve what I need? thanks in advance!

You can only do that if you use ansi screen codes to reposition the cursor after you've written the prompt line:

http://courseweb.stthomas.edu/tpsturm/private/notes/qm300/ANSI.html

These work only if your terminal window supports them and if its been enabled. I've used them in my awk scripts a lot.
 
Alright. Thanks man