Why is scanf causing infinite forlool [duplicate] - c

This question already has answers here:
scanf causing infinite loop in C
(2 answers)
Closed 2 years ago.
im just starting C and i dont know why this is happening. When i excuted the program it only stored 1 value in the service_code array. For some reason, the scanf() is keeping the loop counter ias 0. I couldn't find a solution. The scanf() is causing the forloop to to run infinitely. Does anyone know how to fix this?
#include <stdio.h>
#include <string.h>
int main() {
char name [100];
char str [10];
int discount = 0 ;
int age;
char service_codes[4][6];
printf("Welcome to Nelson Lanka Hospital \n");
printf("Our Services : \n SV105 - Doctor Channeling \n SV156 - Pharmacy \n SV128 - Laboratory \n SV100 - OPD \n \n");
printf("Enter your Details \n Name : ");
scanf("%[^\n]",name);
printf(" Enter age : ");
scanf("%d",&age);
printf(" Enter the Sevice code for the service you need : ");
scanf("%s", str);
strcpy(service_codes[0], str);
for(int i = 1; i<4; i++){
char yn [2] = "y";
printf("Do you need any other sevices? (y/n) : ");
gets(yn);
if (strcmp(yn, "n")==0){
break;
}
printf(" Enter the Sevice code for the service you need : ");
scanf("%s", str);
strcpy(service_codes[i], str);
printf("%s \t %s \t %d \n",service_codes[i],str,i);
}
for (int x = 0; x<4; x++){
printf("%s \n",service_codes[x]);
}
}

For some reason, the scanf() is keeping the loop counter i as 0.
You're probably having a buffer overflow that is altering the variables in the stack (such as the variable i). I see at least two points in your program where a buffer overflow may occur:
scanf("%s", str);: the array str only has room for 9 characters (plus 1 end null character used as a string terminator). If you type in a string longer than 9 characters (including the newline and carriage return characters that are appended when you hit ENTER) then scanf will corrupt the stack.
strcpy(service_codes[i], str);: each element in the array service_codes is defined to have 6 bytes each (room for 5 characters plus 1 end null terminator). By copying a string like this, in which str may be longer than service_code, you'll run into a buffer overflow.
C is a powerful language that allows you to do anything, even shooting at your own feet. You must be careful at every line you write in your code!

Related

Read user input twice in a for-loop using fgets and scanf [duplicate]

This question already has answers here:
fgets doesn't work after scanf [duplicate]
(7 answers)
Closed 1 year ago.
CODE:
#include<stdio.h>
#include<stdlib.h>
int main(){
struct mobile{
char N[10];
int ram, pixel, price;
}B[5];
int min;
char trash;
for(int i = 0; i < 5; i++){
printf("Enter Mobile Name: ");
fgets(B[i].N, sizeof(B[i].N), stdin);
printf("Enter features (ram/camera pixels/price): ");
scanf("%d%d%d", &B[i].ram, &B[i].pixel, &B[i].price);
printf("\n");
}
}
The program is not accepting value for name of mobile second time. It prints Enter mobile name but don't take value then print Enter features and ask for value. I tried adding a second scanf above printf("\n"); but didn't work. Help please. Thanks.
Remove \n from buffer
scanf leaves a newline in the buffer, which is then read by fgets. The other problem is, that you aren't dividing the user input using a delimiter so I would put a space or a slash between the type specifiers %d:
scanf("%d/%d/%d\n", &B[i].ram, &B[i].pixel, &B[i].price);
The input should then be something like this:
Enter specs (ram/pixels/price): 8/12/500
The trailing character \n is now being read, but it isn't stored in any variable.
Remove \n from fgets() input
This doesn't cause your problem, but I would also remove the trailing \n from the fgets() input, because it's probably not supposed to be part of the phone's name.
#include <string.h>
fgets(B[i].N, sizeof(B[i].N), stdin);
B[i].N[strcspn(B[i].N, "\n")] = '\0';

Why isn't this simple code waiting for student name (entered by user)? [duplicate]

This question already has answers here:
scanf() leaves the newline character in the buffer
(7 answers)
Closed 1 year ago.
This C code is not waiting for student name. It directly prints total student number. But when I comment out the first printf-scanf statement(or enter number of students:), then code is waiting for the user to enter student name.
#include <stdio.h>
int main()
{
char name[10];
int count;
printf("ENTER NUMBER OF STUDENTS:\n");
scanf("%d", &count);
printf("ENTER STUDENT NAME:\n");
scanf("%[^\n]%*c", &name);
printf("Total_Students: %d\n", count);
printf("NAME: %s\n", name);
return (0);
}
The second scanf is skipped because the newline character is being interpreted from the first scanf.
For instance, if you entered 2 for number of student, what is being entered is 2\n. The first scanf reads the number 2 and leaves the \n in the buffer which is being interpreted by the 2nd scanf.
You can simply add a space in the second scanf to get past this issue
scanf(" %[^\n]%*c", &name);
change the second scanf() argument to %s and it directly makes the last entry as NULL
.

What is the exact work, that the getchar() is doing? [duplicate]

This question already has answers here:
scanf() leaves the newline character in the buffer
(7 answers)
fgets doesn't work after scanf [duplicate]
(7 answers)
Closed 1 year ago.
Here, if I don't use getchar() the output is misbehaving:
What exactly is the getchar() doing here ?
And what is it holding until another input gets invoked ?
And how will the next input be invoked here in this case ?
My code:
//To take 3 students data using stucture which is user defined.
#include<stdio.h>
#include<stdlib.h>
//#include<conio.h>
struct student
{
char reg_number[200];
char name[200];
int marks_obt;
};
int main()
{
struct student stud1,stud2,stud3;
printf("Enter registration number of student 1 : ");
gets(stud1.reg_number);
printf("Enter name of student 1 : ");
gets(stud1.name);
printf("Enter marks of student 1 : ");
scanf("%d",&stud1.marks_obt);
system("cls");
//getchar();
printf("Enter registration number of student 2 : ");
gets(stud2.reg_number);
printf("Enter name of student 2 : ");
gets(stud2.name);
printf("Enter marks of student 2 : ");
scanf("%d",&stud2.marks_obt);
system("cls");
//getchar();
printf("Enter registration number of student 3 : ");
gets(stud3.reg_number);
printf("Enter name of student 3 : ");
gets(stud3.name);
printf("Enter marks of student 3 : ");
scanf("%d",&stud3.marks_obt);
system("cls");
printf("ID of student 1 is %s \n Name of student 1 is %s \n Marks obtained by stdent 1 is %d",stud1.reg_number,stud1.name,stud1.marks_obt);
printf("\n\n");
printf("ID of student 2 is %s \n Name of student 2 is %s \n Marks obtained by stdent 2 is %d",stud2.reg_number,stud2.name,stud2.marks_obt);
printf("\n\n");
printf("ID of student 3 is %s \n Name of student 3 is %s \n Marks obtained by stdent 3 is %d",stud3.reg_number,stud3.name,stud3.marks_obt);
//getch();
}
First of all, never use the gets(). It is deprecated from the C and C++ standards. The safe way would be to apply fgets():
char input[128];
if (fgets(input, sizeof input, stdin) == NULL) {
// Oops... input was incorrectly given, handle the error
return EXIT_FAILURE; // To quit
}
// Removing the trailing newline character
input[strcspn(input, "\n")] = 0;
You're facing an overlap when asking for another input from the user in the next gets() call because you hit the Return key as well when you finish typing with scanf(). A newline key is added at the end.
The getchar() takes the newline, so it is discarded and does the job you expect.
There are already comments and a correct answer that should be enough, but judging by your last comment it seems that you didn't yet understand what's going on.
This line:
scanf("%d",&stud1.marks_obt);
Parses an integer, for that you press Enter, when you do that a \n is added to the stdin buffer, and remains there, it is not parsed by scanf.
After that:
gets(stud2.reg_number);
Will parse that \n character and store it in stud2.reg_number, and the program moves on.
When you use getchar(), it parses the \n and leaves the stdin buffer empty again so the gets has nothing to parse and waits for your input, correcting the problem, it's still a flimsy solution, but it works in this particular case.
Moral of the story, don't mix scanf with gets, fgets at least, since, as stated, gets was removed from the standard in C11. The reason why some compilers still support it is beyond me.

Reading 2 characters in C [duplicate]

This question already has answers here:
Scanf skips every other while loop in C
(10 answers)
Closed 5 years ago.
#include<stdio.h>
int main()
{
char a,b;
printf("Enter Character 1 ");
scanf("%c",&a);
printf("Enter Character 2");
scanf("%c",&b);
printf("%c%c",a,b);
}
find output of program. It is not taking 2 characters.
When you enter the first character;
The character Ascii value is placed in the variable A .
Then you press enter and the ascii value of enter thats 13 is placed in the second variable.
So B should be 13.
In order to run this program use flushall() library function..
int main() {
char a,b;
printf("Enter Character 1 ");
scanf("%c",&a);
flushall();
printf("Enter Character 2");
scanf("%c",&b);
printf("%c%c",a,b);
return 0;
}
You could also use the standard Library function scanf()
Write something like:
Scanf(" %c",&variablename);
The space before the %c tells the compiler to skip any whitespace (if any).

why %s does not print the string after it encounters a space character? [duplicate]

This question already has answers here:
How do you allow spaces to be entered using scanf?
(11 answers)
Closed 6 years ago.
#include <stdio.h>
int main()
{
char name[20];
printf("Enter name: ");
scanf("%s", name);
printf("Your name is %s.", name);
return 0;
}
Output:
Enter name: Dennis Ritchie
Your name is Dennis.
So far I haven't found any specific valid reason for this question. Can anyone help me out?
scanf only read till it gets to space that is why it is not storing after the first space , so your printf function is not faulty , it is the scanf that is not storing the complete string , stopping on encountering first space.
One should never use gets() , unless they completely know what they are doing , because it does not have buffer overflow protection , it continue to read after the buffer ends until it finds a new line or encounter a EOF. You can read more about that here.Please Check This Why is the gets function so dangerous that it should not be used?
You should instead use fgets().
#include <stdio.h>
int main(){
char name[20];
printf("Enter name: ");
fgets(name,20,stdin);
printf("Your name is %s.", name);
return 0;
}
Remember fgets() also reads newline character(the one you get when you press enter) so you should manually remove that.
Also I highly Recommend this answer for using fgets() to its full potential and avoiding common pitfalls.
This answer tells about using scanf to read string.What it says is the following:
int main(){
char string[100], c;
int i;
printf("Enter the string: ");
scanf("%s", string);
i = strlen(string); // length of user input till first space
do{
scanf("%c", &c);
string[i++] = c; // reading characters after first space (including it)
}while (c != '\n'); // until user hits Enter
string[i - 1] = 0; // string terminating
return 0;
}
How this works? When user inputs characters from standard input, they will be stored in string variable until first blank space. After that, rest of entry will remain in input stream, and wait for next scanf. Next, we have a for loop that takes char by char from input stream (till \n) and appends them to end of string variable, thus forming a complete string same as user input from keyboard.

Resources