This code is about structures and includes basic input from the user but somehow that is not working properly with numeric values such as age, salary etc. I have tried fflush() but got no luck from using that and I can't find any other solution on the internet, everything except the garbage value works like a charm in this code
#include<stdio.h>
#include<conio.h>
#include<string.h>
struct employee
{
unsigned int id;
char name[30];
int age;
char address[50];
char department[30];
unsigned int salary;
};
void main()
{
int i,num;
struct employee emp[50];
printf("\nEnter number of employees whose data you want to enter : ");
scanf("%d", &num);
for(i=0;i<num;i++)
{
printf("\n||| EMPLOYEE NUMBER %d |||\n",i+1);
printf("\nEnter your employee ID (5 Digits) : ");
fflush(stdin);
scanf("%u", &emp[i].id);
printf("\nEnter your name : ");
fflush(stdin);
gets(emp[i].name);
printf("\nEnter your age : ");
fflush(stdin);
scanf("%d", &emp[i].age);
printf("\nEnter your address : ");
fflush(stdin);
gets(emp[i].address);
printf("\nEnter your department : ");
fflush(stdin);
gets(emp[i].department);
printf("\nEnter your salary : ");
fflush(stdin);
scanf("%u", &emp[i].salary);
}
for(i=0;i<num;i++)
{
printf("\n||| EMPLOYEE %d DETAILS ARE |||", i+1);
printf("\nID : %u", &emp[i].id);
printf("\nNAME : %s", emp[i].name);
printf("\nAGE : %d", &emp[i].age);
printf("\nADDRESS : %s", emp[i].address);
printf("\nDEPARTMENT : %s", emp[i].department);
printf("\nSALARY : %u", &emp[i].salary);
}
getch();
}
USER INPUT
||| EMPLOYEE NUMBER 1 |||
Enter your employee ID (5 Digits) : 12345
Enter your name : The Crow
Enter your age : 24
Enter your address : 25/45 New York
Enter your department : HR
Enter your salary : 23000
OUTPUT
||| EMPLOYEE 1 DETAILS ARE |||
ID : 6612448
NAME : The Crow
AGE : 6612484
ADDRESS : 25/45 New York
DEPARTMENT : HR
SALARY : 6612568
Input:
Output:
You are printing the address of id, age, and salary. Delete the & in front of them in your printf statements to print their values.
printf("\n||| EMPLOYEE %d DETAILS ARE |||", i+1);
printf("\nID : %u", emp[i].id);
printf("\nNAME : %s", emp[i].name);
printf("\nAGE : %d", emp[i].age);
printf("\nADDRESS : %s", emp[i].address);
printf("\nDEPARTMENT : %s", emp[i].department);
printf("\nSALARY : %u", emp[i].salary);
On another note, main has return type int so you should define it like this
int main(int argc, char** argv) {
// your code goes here
return 0;
}
As #pm100 mentioned in the comments:
Never use gets. Use fgets.
Read the outputs of the compiler carefully. They are often very informative.
Related
This question already has answers here:
scanf() leaves the newline character in the buffer
(7 answers)
How to read / parse input in C? The FAQ
(1 answer)
Closed 1 year ago.
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
struct date
{
int d, m, y;
};
struct student
{
char name[50];
int rollno;
char divi[50];
float marks;
char mobile_no[50];
};
struct st2
{
char name2[40];
int rollno_1;
char divi_1[40];
float marks_1;
char mobile_no_1[40];
};
struct date_1
{
int d, m, y;
};
int main()
{
struct student s;
printf("Enter the first student information: ");
printf("\nEnter the name of the student: ");
scanf("%[^\n]", s.name);
printf("Enter the roll numner of the student: ");
scanf("%d", &s.rollno);
printf("Enter the division of the student: ");
scanf("%s", s.divi);
printf("Enter the marks of the student: ");
scanf("%f", &s.marks);
printf("Enter the mobile number of the student: ");
scanf("%s", s.mobile_no);
struct date d;
printf("enter the student Date Of Birth: \n");
printf("DAY: ");
scanf("%d", &d.d);
printf("MONTH: ");
scanf("%d", &d.m);
printf("YEAR: ");
scanf("%d", &d.y);
printf("----------[%s] racord----------\n", s.name);
printf("| student name is: %s\n", s.name);
printf("| student roll no is: %d\n", s.rollno);
printf("| student division is: %s\n", s.divi);
printf("| student marks is: %f\n", s.marks);
printf("| student nmobile number is: %s\n", s.mobile_no);
printf("| student birth date is: %d/%d/%d\n", d.d, d.m, d.y);
struct st2 f;
printf("Enter the Second student information: ");
printf("\nEnter the name of the student: ");
scanf(" %[^\n]",f.name2); // added space before %[^\n]
printf("Enter the roll number of the student: ");
scanf("%d", &f.rollno_1);
printf("Enter the division of the student: ");
scanf("%s", f.divi_1);
printf("Enter the marks of the student: ");
scanf("%f", &f.marks_1);
printf("Enter the mobile number of the student: ");
scanf("%s", f.mobile_no_1);
struct date_1 b;
printf("enter the student Date Of Birth: \n");
printf("DAY: ");
scanf("%d", &b.d);
printf("MONTH: ");
scanf("%d", &b.m);
printf("YEAR: ");
scanf("%d", &b.y);
printf("----------[] racord----------\n");
printf("| student name is: %s\n", f.name2);
printf("| student roll no is: %d\n", f.rollno_1);
printf("| student division is: %s\n", f.divi_1);
printf("| student marks is: %f\n", f.marks_1);
printf("| student nmobile number is: %s\n", f.mobile_no_1);
printf("| student birth date is: %d/%d/%d", b.d, b.m, b.y);
return 0;
}
Output:
Enter the first student information:
Enter the name of the student: can yaman
Enter the roll numner of the student: 72
Enter the division of the student: a
Enter the marks of the student: 98
Enter the mobile number of the student: 6244738358
enter the student Date Of Birth:
DAY: 31
MONTH: 5
YEAR: 1993
----------[can yaman] racord----------
| student name is: can yaman
| student roll no is: 72
| student division is: a
| student marks is: 98.000000
| student nmobile number is: 6244738358
| student birth date is: 31/5/1993
Enter the Second student information:
Enter the name of the student: Enter the roll number of the student:
QUESTION:
WAP to enter id, name, age and basic salary of n number of employees. Calculate the gross salary of all the employees and display it along with all other details in a tabular form, using pointer to structure.
--->
#include <stdio.h>
#include <conio.h>
struct employee {
char name[100];
int id, age;
int salary;
};
int main(){
struct employee emp, *ptr;
int i,n;
printf("Enter the no of employees\n");
scanf("%d",&n);
printf("****************INPUT EMPLOYEE DETAILS******************\n");
for(i=0;i<n;i++)
{
printf("\nEnter employee id of employee %d : ",i+1);
scanf("%d", &emp.id);
printf("Enter name of employee %d : ", i+1);
scanf("%s", &emp.name);
printf("Enter age of employee %d : ", i+1);
scanf("%d", &emp.age);
printf("Enter salary of employee %d : ", i+1);
scanf("%d", &emp.salary);
}
printf("\n");
printf(" DISPLAYING EMPLOYEE DETAILS \n");
printf("*********************************************************************\n");
ptr = &emp;
printf("\nEMP_ID\t\tEMP_NAME\tEMP_AGE\t\tGROSS_SAL\n");
for(i=0;i<n;i++){
printf("%d\t\t%s\t\t%d\t\t%d\n",ptr->id, ptr->name, ptr->age, ptr->salary);
}
return 0;
}
First of all you need to save each of your employees in variable so you can refer them later. Second because your variable is structure and you want loop through it your best choice is to use array.
I changed your code and highlighted them it worked for me i hope it will help you.
#include <stdio.h>
#include <conio.h>
struct employee {
char name[100];
int id, age;
int salary;
};
int main(){
struct employee *ptr; //One change is here
int i,n;
printf("Enter the no of employees\n");
scanf("%d",&n);
struct employee emp[n];
printf("****************INPUT EMPLOYEE DETAILS******************\n");
for(i=0;i<n;i++)
{
printf("\nEnter employee id of employee %d : ",i+1);
scanf("%d", &emp[i].id);
printf("Enter name of employee %d : ", i+1);
scanf("%s", emp[i].name); // Another change is here "%s" doesn't need '&'
printf("Enter age of employee %d : ", i+1);
scanf("%d", &emp[i].age);
printf("Enter salary of employee %d : ", i+1);
scanf("%d", &emp[i].salary);
}
printf("\n");
printf(" DISPLAYING EMPLOYEE DETAILS \n");
printf("*********************************************************************\n");
printf("\nEMP_ID\t\tEMP_NAME\tEMP_AGE\t\tGROSS_SAL\n");
for(i=0;i<n;i++){
ptr = &emp[i]; // This yet another change i grab "ptr" down here and assign it to "i" in each loop
printf("%d\t\t%s\t\t%d\t\t%d\n",ptr->id, ptr->name, ptr->age, ptr->salary);
}
return 0;
}
`#include <stdio.h>
int main(){
char firstInitial , lastInitial;
int age , favourite_number;
printf("Enter Initial character of your name : \n");
scanf(" %c" ,&firstInitial );
printf("Enter initial character of your surname : \n");
scanf(" %c" , &lastInitial);
printf("Enter your age : \n");
scanf(" %d",&age);
printf("Enter your favourite number : \n");
scanf(" %d",&favourite_number);
printf("Your name is %c.%c. and your age is %d.\n", firstInitial , lastInitial ,age);
printf("Your favourite number is %d \n", favourite_number);
return 0;
}`There is an error mentioned in the problem section even though I have put the semicolon and the program runs.
Why are the variables not highlighted?
I want to read the information that I saved in txt file in the program I created. but when I want to do the reading process only the first line of student information by reading, for example, class 10 people are writing the same student information 10 times within the program. I'm doing the read operation in the for loop.
struct student{
char Name[30];
char Surname[30];
int StudentNumber;
int MathematicScore;
int EnglishScore;
int HistoryScore;
int BiologyScore;
int LiteratureScore;
int PhysicsScore;
int classNum;
int total;
float average;
};
/* Printing process */
FILE *fout;
fout = fopen("Mathematic Class.txt","w");
printf("Please enter student number of class : ");
scanf("%d", &x.classNum);
fprintf(fout, "Class Number : %d\n",x.classNum);
for(i=0; i<x.classNum; i++){
printf("Please Enter %d.Student Name : ",i+1);
scanf("%s",&x.Name);
printf("Please Enter %d.Student Surname : ",i+1);
scanf("%s",&x.Surname);
printf("Please Enter %d.Student Number : ",i+1);
scanf("%d",&x.StudentNumber);
printf("Please Enter %d.Student Score : ",i+1);
scanf("%d",&x.MathematicScore);
x.total = x.total + x.MathematicScore;
fprintf(fout, "\nStudent Name : %s %s Student Number : %d Student Score : %d\n", x.Name, x.Surname, x.StudentNumber, x.MathematicScore);
}
x.average = x.total / x.classNum;
fprintf(fout, "\n\nClass Average is : %f", x.average);
fclose(fout);
/* Reading process */
FILE *fin;
fin = fopen("Mathematic Class.txt","r");
fscanf(fin, "Class Number : %d\n\n%s %s %d %d\n",&x.classNum, &x.Name, &x.Surname, &x.StudentNumber, &x.MathematicScore, &x.average);
fclose(fin);
printf("Class Number : %d\n", x.classNum);
for(i=0; i<x.classNum; i++){
printf("Student Name : %s %s Student Number : %d Student Score : %d\n", x.Name, x.Surname, x.StudentNumber, x.MathematicScore);
}
printf("\n\nClass Average is : %f", x.average);
Here are the problems that I see upon first look:
You only call fscanf() one time in the reading process. If you want to read in data for multiple students, like it appears you're doing in the writing process, you need to call it once for each student to read that student's line from the file.
In your reading/printing process, you are printing the same data x.classNum times.
#include <stdio.h>
int main()
{
int comp,loop=1,tcomp=0;
char cont;
char name[50];
float donate=0,total,gtotal;
printf("\nGot representative? [Y to continue]: ");
scanf("%s", &cont);
while(cont=='y'){
printf("\nRepresentative name : ");
scanf("%s", &name);
printf("How many companies? : ");
scanf("%d", &comp);
tcomp+=comp;
do{
printf("Enter amount of donation : ");
scanf("%f", &donate); loop++;
total+=donate;}
while(loop<=comp);
printf("%s : %.2f\n", name, total);
printf("\nGot representative? [Y to continue]: ");
scanf("%s", &cont);}
printf("\nTotal Representative : %d", tcomp);
gtotal+=total;
printf("\nTotal Donations : %.2f\n", gtotal);
}
Current output :
Got representative? [Y to continue]: y
Representative name : ABC
How many companies? : 3
Enter amount of donation : 1
Enter amount of donation : 2
Enter amount of donation : 3
ABC : 6.00
Got representative? [Y to continue]: y
Representative name : ZXC
How many companies? : 3
Enter amount of donation : 1
ZXC : 7.00
As you can see here, the 2nd loop did not reset and it's summing the numbers of first loop. How do I rectify this? How do I make it that the loop starts fresh each time? p/s : I was asked specifically to use while and do while loop.
Below your corrected code.
int main()
{
int comp,loop=1,tcomp=0;
char cont;
char name[50];
float donate=0,total=0,gtotal=0;
printf("\nGot representative? [Y to continue]: ");
scanf("%c", &cont);
while(cont=='y')
{
printf("\nRepresentative name : ");
scanf("%s", name);
printf("How many companies? : ");
scanf("%d", &comp);
tcomp+=comp;
loop=0;
total=0;
do
{
printf("Enter amount of donation : ");
scanf("%f", &donate); loop++;
total+=donate;
}
while(loop<comp);
printf("%s : %.2f\n", name, total);
printf("\nGot representative? [Y to continue]: ");
scanf("%c", &cont);
gtotal+=total;
}
printf("\nTotal Representative : %d", tcomp);
printf("\nTotal Donations : %.2f\n", gtotal);
return 0;
}
As you can see:
You must reset loop variable before each loop where amount are asked.
You must reset total before that loop
You must save to gtotal for each iteration of first while loop.
You must use %c instead of %s to get a single char: scanf("%c", &cont);