scanf

Is there a way of limiting scanf in C?

◇◆丶佛笑我妖孽 提交于 2021-01-29 05:37:14
问题 I am trying to write a correct console application for linked list usage, so i need to scan a number of a command in a infinite loop and do something due to switch case option. So i am using scanf for this but the problem is when the next line doesnt contain number it loops and starts printing not even default value. `while(1) { printf("Enter a number of a command.\n"); scanf("%d",&command); switch(command) { case -1: .... default: printf("Reenter command.\n"); break; } } It seems like when i

Why does my code keep printing twice? I don't understand the problem [duplicate]

那年仲夏 提交于 2021-01-28 21:13:13
问题 This question already has answers here : How do you allow spaces to be entered using scanf? (11 answers) Closed last year . #include <stdio.h> #define MAX_STRING_LENGTH 1024 int main(){ char input_name_string[MAX_STRING_LENGTH+1],motive_string[MAX_STRING_LENGTH+1]; printf("What is your name?\n"); scanf("%1024s",input_name_string); printf("your name is %s \n", input_name_string); printf("What is your motive?\n"); scanf(" %1024s",motive_string); printf("your motive is %s \n", motive_string);

Why does my code keep printing twice? I don't understand the problem [duplicate]

流过昼夜 提交于 2021-01-28 20:38:54
问题 This question already has answers here : How do you allow spaces to be entered using scanf? (11 answers) Closed last year . #include <stdio.h> #define MAX_STRING_LENGTH 1024 int main(){ char input_name_string[MAX_STRING_LENGTH+1],motive_string[MAX_STRING_LENGTH+1]; printf("What is your name?\n"); scanf("%1024s",input_name_string); printf("your name is %s \n", input_name_string); printf("What is your motive?\n"); scanf(" %1024s",motive_string); printf("your motive is %s \n", motive_string);

sscanf_s doesn't return first character of string

无人久伴 提交于 2021-01-28 18:38:42
问题 I'm trying to find the first string (max 4 characters) in a comma-separated list of strings inside a C char-array. I'm trying to achieve this by using sscanf_s (under Windows) and the format-control string %[^,] : char mystring[] = "STR1,STR2"; char temp[5]; if (sscanf_s(mystring, "%[^,]", temp, 5) != 0) { if (strcmp(temp, "STR1") == 0) { return 0; } else if (strcmp(temp, "STR2") == 0) { return 1; } else { return -1; } } After calling sscanf_s the content of temp is not STR1 but \0TR1 ( \0

What is the use of scanf() == 1?

六月ゝ 毕业季﹏ 提交于 2021-01-28 12:01:07
问题 My Code: while(scanf("%f", &number) && number > 0) while(scanf("%f", &number) == 1 && number > 0) What does this == 1 and is this necessary? 回答1: As, Weather Vane said in the comments, the ==1 is important if scanf() returns EOF (End Of File or -1) which would be evaluated to true and since the value in number remains unchanged from the previous iteration the condition of the while loop would be evaluated to true . This is not appropriate since we have an error at the scan process then and

What can I use for input conversion instead of scanf?

偶尔善良 提交于 2021-01-28 09:18:23
问题 I have very frequently seen people discouraging others from using scanf and saying that there are better alternatives. However, all I end up seeing is either "don't use scanf " or "here's a correct format string" , and never any examples of the "better alternatives" mentioned. For example, let's take this snippet of code: scanf("%c", &c); This reads the whitespace that was left in the input stream after the last conversion. The usual suggested solution to this is to use: scanf(" %c", &c); or

C program skips fgets [duplicate]

倾然丶 夕夏残阳落幕 提交于 2021-01-28 06:28:45
问题 This question already has answers here : fgets() skipped [duplicate] (2 answers) Closed 7 years ago . My program compiles ok but it when it calls the getinput() function it never prompts for input. Edited to show more code, I added fflush but it still skips it for some reason. #include <stdio.h> #include <string.h> #include <stdlib.h> main(){ char mystring[] = "It's equal to it. "; int k = 32; int e; printf("Enter a number: "); scanf("%d",&e); if(e == k){ printf("\n\n%s\n",mystring); } else

Does Scanf function ignore the Enter key when looking for %d match?

拟墨画扇 提交于 2021-01-27 17:42:38
问题 I'm new to to C language and I'm studying it on a book by Kim N. King. It say that the scanf() looks for number pattern ignoring the whitespaces but I think it also skips on Enter key. While if it looks for characters it obviously takes the whitespace also. Therefore in this example code I have to use getchar() to clear the stream before the second scanf() , otherwise the second one would be executed without waiting for user's input. printf("Enter a char: "); scanf("%c", &ch1); getchar();

Scanf not working in while loop when nonmatching string is entered

不打扰是莪最后的温柔 提交于 2021-01-27 06:24:06
问题 I'm using a function called checkType to check whether the user has entered a valid input of integer type. For example, if the user enters 15 it will print valid and 15c will print not valid . However, if the user enters a string input only like ccccc , it results in an infinite loop and the program crashes. I have added some screenshots below to show the outputs. int checkType(int input, char *c) { if (input == 2 and *c == '\n') { return 1; } else { return 0; }; } int main(void) { int faces

Using fgets after scanf [duplicate]

寵の児 提交于 2021-01-10 03:35:23
问题 This question already has answers here : fgets doesn't work after scanf [duplicate] (9 answers) Closed 18 days ago . I want to get rid of buffer overflow and I am using fgets instead of scanf to limit the characters. But whenever I enter something with scanf before fgets , it doesn't work correctly anymore. This code works correctly #include <stdio.h> int main() { char name[10]; printf("Who are you? \n"); fgets(name,10,stdin); printf("Good to meet you, %s.\n",name); return(0); } This code