I am trying to read two strings from the keyboard and print them.
Why does printf("read 1st\n") run after the second scanf()?
#include <stdio.h>
int main(void)
{
char str[20];
char str2[20];
scanf("%s", str);
printf("read 1st\n");
scanf("%s", str2);
printf("read 2nd\n");
printf("str: %s\nstr2: %s\n", str, str2);
return 0;
}
Expected:
foo
read 1st
bar
read 2nd
str: foo
str2: bar
Actual:
foo
bar
read 1st
read 2nd
str:foo
str2:bar
I could not reproduce your problem, but adding a fflush(stdout); should take care of your problem.
scanf("%s", str);
printf("read 1st\n");
fflush(stdout); // Ensures that the above is printed before scanf is executed
scanf("%s", str2);
printf("read 2nd\n");
I would also change the scans to scanf("%19s", str);. Otherwise, bad things could happen if you enter a string longer than str can hold.
Related
I run the following c program
char str[80] = "String";
printf("hello %s\n", str);
scanf("%s", str);
printf("%s\n", str);
scanf("%[ABCDEF]", str);
printf("hello %s\n", str);
return 0;
For some reason on line 5 when it is suppose to input from Pattern %[ABCDEF], the console simply prints previous string (input from line 3). Why is that so?
Thats because the first scanf call doesn't read the newline character and the second call to scanf simply reads that newline character. To avoid this start the format string with a space like this:
#include <stdio.h>
int main(void)
{
char str[80] = "String";
printf("hello %s\n", str);
scanf("%s", str);
printf("%s\n", str);
scanf(" %[ABCDEF]", str);
printf("hello %s\n", str);
return 0;
}
However, you also need to make sure that str doesn't overflow if the user inputs a string longer than 79 characters.
I've been struggling to get two lines seperated with space and my scanf() is not working properly
This is the code
char *str1, *str2;
while(1) {
printf("Enter string:\n");
scanf(" %s", str1);
printf("Then;");
scanf(" %s", str2);
if(strcmp(str1, "exit") == 0){break;}
printf("Output:\n %s %s\n", str1, str2);
}
But my output seems like this:
Enter string:
ok
Then;hello
Output:
ok (null)
Enter string:
Then;
What would cause this problem? And it prints first output as the loop turns second time.
Enter string:
ok
Then;hello
Output:
ok (null)
Enter string:
Then;well
Output:
hello (null)
Enter string:
Then;no
Output:
well (null)
Enter string:
"%s" in scanf(" %s", str1); expects str1 to point to valid memory to store a string. str1 in not initialized - it does not point to valid memory.
Instead provide valid memory and limit input width.
char str[100];
scanf("%99s", str1);
The array str is converted to a pointer to the beginning of the array when passed to scanf("%99s", str1);
The leading space in " %s" is not needed as "%s" itself consumes leading white-space,
just like " ".
I have this code:
char temp;
scanf("%c",&temp);
fgets(variable1,50,stdin);
strtok(variable1, "\n");
printf("%s ", variable1);
This gets a string with the possibility of having spaces and assigns it to the variable variable1. Later, I can print the string with no problem.
The problem comes when I add to code other fgetsfor gets other string in other variable.
if (1) {
scanf("%c",&temp);
fgets(variable2,50,stdin);
strtok(variable2, "\n");
printf("%s ", variable2);
}
The result complete is:
char temp;
scanf("%c",&temp);
fgets(variable1,50,stdin);
strtok(variable1, "\n");
printf("%s ", variable1);
if (1) {
scanf("%c",&temp);
fgets(variable2,50,stdin);
strtok(variable2, "\n");
printf("%s ", variable2);
}
variable1 always works correctly. But I try print in some phrases with %s variable2 but the result does not get the first character, only in second scanf. If I put HELLO, variable2 is ELLO.
I have tested using another temp variable, another data, etc. But always get the same error.
Why is this happening?
UPDATE
For more information. I use scanf because, if I do not use it, the program does not pause while waiting for the string. I use strtok(variable1, "\n"); to remove the line break.
This program is inside a while and in switch case. I put the complete code:
case 4: printf( "Put equipo: ");
scanf("%c",&temp);
fgets(equipo,50,stdin);
strtok(equipo, "\n");
if (Exists(equipo)) {
printf("Put Piloto ");
scanf("%c",&temp);
fgets(piloto,50,stdin);
strtok(piloto, "\n");
printf("You said %s and %s", equipo, piloto);
}
break;
If I introduce like Equipo HELLO and like Piloto FRIEND, the output is:
You said HELLO and RIEND
Re-written based on OP latest edits to post, and comments...
You describe the need to simply obtain two strings from user input, remove the newlines from each, then package both into a message to stdout. If this description actually matches what you need, change this code section:
...
scanf("%c",&temp);
fgets(equipo,50,stdin);
strtok(equipo, "\n");
if (Exists(equipo)) {
printf("Put Piloto ");
scanf("%c",&temp);
fgets(piloto,50,stdin);
strtok(piloto, "\n");
printf("You said %s and %s", equipo, piloto);
...
To this:
...
printf("enter equipo: ");
if(fgets(equipo, sizeof(equipo), stdin))
{
equipo[strcspn(equipo, "\n")] = 0; //remove newline
printf("enter piloto: ");
if(fgets(piloto, sizeof(piloto), stdin))
{
piloto[strcspn(piloto, "\n")] = 0;
printf("You said %s and %s", equipo, piloto);
}
}
...
Note: strtok(piloto, "\n"); works, but has problems if user just hits <return>
By the way, here are some other interesting ways to clear the newline
I do not understand your example code. Could you update the question with your actual code?
I assume scanf should not be there?
This code works fine for me:
char var1[50], var2[50];
fgets(var1, 50, stdin);
strtok(var1, "\n");
printf("Var1: %s\n", var1);
if (1) {
fgets(var2, 50, stdin);
strtok(var2, "\n");
printf("Var2 %s\n", var2);
}
Output:
Test1
Var1: Test1
Test2
Var2 Test2
just having a little issue with strtok and strcmp.
I'm trying to compare the input of a user via fgets to some predetermined string:
char *token[100];
fgets(s, sizeof(s), stdin)
token[0] = strtok(s, " "); // Get first word
printf("tok: %s", token[0]);
printf("cmp: %d\n", strcmp(token[0], "/bin/echo");
Obviously it's not all the code but this shows my problem - if I enter "/bin/echo ..." (or anything for that matter), it will be put into token[0], and get printed. It prints correctly but when it prints the cmp value it's never 0. For /bin/echo, the cmp value is 1 for some reason.
Thanks.
EDIT to clear up confusion about s and token:
char s[1024];
char *token[100];
EDIT 2 - Added some other test cases:
I entered "/bin/echo hello world" to stdin
token[0] = strtok(s, " \n\0"); // Get first word
printf("token[0] is: %s", token[0]);
printf("cmp: %d\n", strcmp(token[0], "/bin/echo"));
Output:
token[0] is: /bin/echo
cmp: 1
And then I tried hardcoding the tokened string:
char str[] = "/bin/echo hello world";
token[0] = strtok(str, " ");
printf("token[0] is: %s", token[0]);
printf("cmp: %d\n", strcmp(token[0], "/bin/echo"));
Output:
token[0] is: /bin/echo
cmp: 0
here i have made small program
#include<string.h>
#include<stdio.h>
int main()
{
char str[] ="/bin/echo this is something";
char * token[100];
token[0] = strtok (str," ");
token[0] = strtok(str, " "); // Get first word
printf("cmp: %d\n", strcmp(token[0], "/bin/echo"));
return 0;
}
Here i have statically stored input string instead of fgets()
That works fine.
see http://codepad.org/IrGAXT8f
Use
char token[1000];
strcpy(token,strtok(s," "));
string's can't be assigned directly like this in c :)
also, include string.h
One needs to allocate memory dynamically for copying strings. Read about dynamic memory management first (malloc, calloc, etc...)
EDIT:
http://ideone.com/0UxEwO - works for me
#include <stdio.h>
#include <string.h>
int main()
{
char s[1024];
char *token[100];
fgets(s, sizeof(s), stdin);
token[0] = strtok(s, " \n\0");
printf("token[0] is: %s", token[0]);
printf("cmp: %d\n", strcmp(token[0], "/bin/echo"));
}
I have the following code:
char *s1, *s2;
char str[10];
printf("Type a string: ");
scanf("%s", str);
s1 = &str[0];
s2 = &str[2];
printf("%s\n", s1);
printf("%s\n", s2);
When I run the code, and enter the input "A 1" as follow:
Type a string: A 1
I got the following result:
A
�<�
I'm trying to read the first character as a string and the third character as an integer, and then print those out on the screen. The first character always works, but the screen would just display random stuffs after that.... How should I fix it?
You're on the right track. Here's a corrected version:
char str[10];
int n;
printf("type a string: ");
scanf("%s %d", str, &n);
printf("%s\n", str);
printf("%d\n", n);
Let's talk through the changes:
allocate an int (n) to store your number in
tell scanf to read in first a string and then a number (%d means number, as you already knew from your printf
That's pretty much all there is to it. Your code is a little bit dangerous, still, because any user input that's longer than 9 characters will overflow str and start trampling your stack.
scanf("%s",str) scans only until it finds a whitespace character. With the input "A 1", it will scan only the first character, hence s2 points at the garbage that happened to be in str, since that array wasn't initialised.
Try this code my friend...
#include<stdio.h>
int main(){
char *s1, *s2;
char str[10];
printf("type a string: ");
scanf("%s", str);
s1 = &str[0];
s2 = &str[2];
printf("%c\n", *s1); //use %c instead of %s and *s1 which is the content of position 1
printf("%c\n", *s2); //use %c instead of %s and *s3 which is the content of position 1
return 0;
}