• Some users have recently had their accounts hijacked. It seems that the now defunct EVGA forums might have compromised your password there and seems many are using the same PW here. We would suggest you UPDATE YOUR PASSWORD and TURN ON 2FA for your account here to further secure it. None of the compromised accounts had 2FA turned on.
    Once you have enabled 2FA, your account will be updated soon to show a badge, letting other members know that you use 2FA to protect your account. This should be beneficial for everyone that uses FSFT.

[C Question] scanf without newline?

Imprez04

Limp Gawd
Joined
Jul 27, 2008
Messages
206
Is there anyway to use scanf without it going to a new line when printf is invoked after it? Or if something other than printf that would work?

I want to be able to output information on the same line that the scanf function just used

Example:

Code:
char *str;
str = malloc(sizeof(char) * STRLENGTH);
printf("say Hello.\n");

scanf("%s", str);
		
if (strcmp(str, "Hello") == 0)
{
printf(" y helo thar\n");
}

The screen when you run this will be the following:
Code:
say Hello.
Hello
 y helo thar

is there anyway to manipulate the scanf function so it is like this instead

Code:
say Hello.
Hello y helo thar

I know there is gets, but i'd rather not use it as it isnt the safest in the world. (and produces annoying warnings on GCC)

thanks.
 
You're going to have to use a function like getchar() and grab each character as it is typed.
 
scanf() reads the line until the newline occurs. gets() is just bad. there are methods to have scanf() ignore the trailing \n character but then the '\n' is left in the input buffer to possibly be encountered later. rather than manipulate the scanf() function why not manipulate the string and pull the \n from it instead?

Edit: I believe I just realized what you are asking... and no. scanf() is waiting for you to hit the <enter> key before reading in the entire string, thereby placing the unavoidable \cr character at the end of the line. You input "Hello<enter>" thereby moving the cursor down the additional line. If this is what you're asking then no, not going to happen.
 
Back
Top