This question already has answers here:
The program doesn't stop on scanf("%c", &ch) line, why? [duplicate]
(2 answers)
Closed 5 years ago.
the lines after printf("enter nation\n"); to printf("enter m or f\n"); not executing like the codeblock can't see it as it prints the two line together with no chance for me to enter anything in between.. this problem always happen when i use struct
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct emp
{
int salary;
int age;
char name[10];
char m:1;
char nation:1;
};
int main()
{
int i,k;
char g,n;
char name2[10];
struct emp x[3];
for(i=0;i<3;i++)
{
printf("enter name\n");
scanf("%s",&name2);
strcpy(x[i].name,name2);
printf("enter salary\n");
scanf("%d",&x[i].salary);
printf("enter age\n");
scanf("%d",&x[i].age);
printf("enter nation\n");
cant see the next 3 lines and jump to enter m or f
scanf("%c",&n);
if(n=='e'){x[i].nation==0;}
else {x[i].nation==1;}
printf("enter m or f\n");
cant see those lines too
scanf("%c",&g);
if(g=='f'){x[i].m=0;}
else if(g=='m'){&x[i]==1;}
}
for(k=0;k<3;k++)
{
if(x[i].nation=='e')
{
puts(x[i].name);
printf("%d\n",x[i].salary);
printf("%d\n",x[i].age);
if(x[i].m==0)
printf("female\n");
else {printf("male\n");}
printf("egyptian");
}
}
return 0;
}
You have to change
scanf("%c",&n);
to
scanf(" %c",&n); // Skip leading whitespaces
Arrays in C are passed by pointer, so for
char name2[10];
You dont have to use reference operator
scanf("%s",&name2);
just simply do
scanf("%s",name2); // Will pass an arrays address
Also it would be nice to limit input, or buffer overflow may occur and it would lead to undefined behavior
scanf("%9s",name2);
Related
This question already has answers here:
fgets doesn't work after scanf [duplicate]
(7 answers)
Closed 2 years 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 does not read name correctly
#include <stdio.h>
#include <stdlib.h>
#define MAX 15
int new_acc();
int view_list();
int main(){
int one=1, two=2, three=3, four=4, five=5, six=6, seven=7, choice;
int new_account, list;
printf("%d. Create new account\n",one);
printf("Enter you choice: ");
scanf("%d",&choice);
if (choice==one){new_account = new_acc();}
return 0;
}
int new_acc(){
char name[MAX], address, account;
printf("Enter your name: ");
fgets(name, MAX, stdin); /* it is the code */
}
Why is it happening and how do I fix it?
Don't mix scanf() with fgets() in the same code.
scanf("%d",&choice); does not consume all the line - it leaves the trailing '\n' for the later fgets() to consume as an empty line.
scanf() is difficult to use in a secure fashion.
This question already has answers here:
fgets doesn't work after scanf [duplicate]
(7 answers)
Closed 2 years 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 does not read name correctly
#include <stdio.h>
#include <stdlib.h>
#define MAX 15
int new_acc();
int view_list();
int main(){
int one=1, two=2, three=3, four=4, five=5, six=6, seven=7, choice;
int new_account, list;
printf("%d. Create new account\n",one);
printf("Enter you choice: ");
scanf("%d",&choice);
if (choice==one){new_account = new_acc();}
return 0;
}
int new_acc(){
char name[MAX], address, account;
printf("Enter your name: ");
fgets(name, MAX, stdin); /* it is the code */
}
Why is it happening and how do I fix it?
Don't mix scanf() with fgets() in the same code.
scanf("%d",&choice); does not consume all the line - it leaves the trailing '\n' for the later fgets() to consume as an empty line.
scanf() is difficult to use in a secure fashion.
This question already has answers here:
fgets doesn't work after scanf [duplicate]
(7 answers)
Closed 2 years 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 does not read name correctly
#include <stdio.h>
#include <stdlib.h>
#define MAX 15
int new_acc();
int view_list();
int main(){
int one=1, two=2, three=3, four=4, five=5, six=6, seven=7, choice;
int new_account, list;
printf("%d. Create new account\n",one);
printf("Enter you choice: ");
scanf("%d",&choice);
if (choice==one){new_account = new_acc();}
return 0;
}
int new_acc(){
char name[MAX], address, account;
printf("Enter your name: ");
fgets(name, MAX, stdin); /* it is the code */
}
Why is it happening and how do I fix it?
Don't mix scanf() with fgets() in the same code.
scanf("%d",&choice); does not consume all the line - it leaves the trailing '\n' for the later fgets() to consume as an empty line.
scanf() is difficult to use in a secure fashion.
This question already has answers here:
fgets doesn't work after scanf [duplicate]
(7 answers)
Closed 2 years 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 does not read name correctly
#include <stdio.h>
#include <stdlib.h>
#define MAX 15
int new_acc();
int view_list();
int main(){
int one=1, two=2, three=3, four=4, five=5, six=6, seven=7, choice;
int new_account, list;
printf("%d. Create new account\n",one);
printf("Enter you choice: ");
scanf("%d",&choice);
if (choice==one){new_account = new_acc();}
return 0;
}
int new_acc(){
char name[MAX], address, account;
printf("Enter your name: ");
fgets(name, MAX, stdin); /* it is the code */
}
Why is it happening and how do I fix it?
Don't mix scanf() with fgets() in the same code.
scanf("%d",&choice); does not consume all the line - it leaves the trailing '\n' for the later fgets() to consume as an empty line.
scanf() is difficult to use in a secure fashion.
This question already has answers here:
scanf not reading properly because of gets function
(3 answers)
Closed 5 years ago.
gets(edu.classes[i].students[j].name);
I would like to know why the compiler skips this line after debugging and does not input into name? Is it legal to invoke gets function on that way?
Note: Once I use scanf("%s" , ... ) - it works!
scanf("%s",edu.classes[i].students[j].name);
(I know that I did not free the memory allocations and checked if the allocations were not failed - I know that it is necessary! It's only time issue) :)
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define SIZE 20
typedef struct
{
char name[SIZE];
int id;
}Student;
typedef struct
{
Student *students;
int num_students;
char teacher[SIZE];
}Class;
typedef struct
{
Class *classes;
int num_classes;
}Education;
int main()
{
int i, j;
Education edu;
puts("how many classes?");
scanf("%d", &(edu.num_classes));
edu.classes = (Class*)malloc((edu.num_classes) * sizeof(Class));
if (edu.classes == NULL)
{
printf("ERROR allocation\n");
exit(1);
}
for (i = 0; i < edu.num_classes; i++)
{
puts("enter num of students");
scanf("%d", &(edu.classes[i].num_students));
edu.classes[i].students = (Student*)malloc((edu.classes[i].num_students)
* sizeof(Student));
for (j = 0; j < edu.classes[i].num_students; j++)
{
puts("enter student's name");
gets(edu.classes[i].students[j].name); // this is the problematic line
puts("enter id");
scanf("%d", &(edu.classes[i].students[j].id));
}
}
return 0;
}
I think it is seeing the newline character that was produced due to hitting the enter key. gets then stops reading. Try eating those special characters before gets sees them. Or use scanf with %s which eats any leading whitespace as you have seen it works.