I need to write a file(not binary) with students and each ones grades(int note[10] under this form). I do not get any errors but when I try to write the file just the last grade is printed, and I don't know how to print the entire list. For example (int nr_note means how many grades does the student have) if a students grades are 6, 8, 9 it just prints the 9.
Thank you in advance.
#include <stdio.h>
#include <stdlib.h>
struct student{
int nr_matricol;
char nume[10];
int nr_note;
int note[10];
};
void citire_date(struct student *studenti, int n, FILE *f){
int i, j;
for(i=0;i<n;i++){
printf("Studentul %d\n", i+1);
printf("Numarul matricol: "); scanf("%d", &(studenti+i)->nr_matricol);
printf("Numele studentului: "); fflush(stdin); gets((studenti+i)->nume);
printf("Numarul de note: "); scanf("%d", &(studenti+i)->nr_note);
for(j=0;j<((studenti+i)->nr_note);j++){
printf("Nota %d: ", j+1); scanf("%d", &(studenti+i)->note[i]);
}
fprintf(f, "Numar matricol: %d\nNume: %s\nNote: %d \n", ((studenti+i)->nr_matricol) ,((studenti+i)->nume), ((studenti+i)->note[i]));
}
}
int main()
{
struct student studenti[20];
FILE *f;
int n;
f = fopen("studenti.txt", "w");
if(f==NULL){
printf("Nu s-a putut deschide/crea fisierul pentru scriere.");
exit(1);
}
printf("Introduceti numarul de studenti: "); scanf("%d", &n);
citire_date(studenti, n, f);
fclose(f);
return 0;
}
Change citire_date() function like this :
void citire_date(struct student *studenti, int n, FILE *f){
int i, j;
for(i=0;i<n;i++){
printf("Studentul %d\n", i+1);
printf("Numarul matricol: "); scanf("%d", &(studenti+i)->nr_matricol);
printf("Numele studentului: "); fflush(stdin); gets((studenti+i)->nume);
printf("Numarul de note: "); scanf("%d", &(studenti+i)->nr_note);
for(j=0;j<((studenti+i)->nr_note);j++){
printf("Nota %d: ", j+1); scanf("%d", &(studenti+i)->note[j]);
}
fprintf(f, "Numar matricol: %d\nNume: %s\n", ((studenti+i)->nr_matricol) ,((studenti+i)->nume) );
// Printing the notes
for(j=0;j<((studenti+i)->nr_note);j++){
fprintf(f, "Note: %d = %d \n", j+1, ((studenti+i)->note[j]));
}
}
}
You need to use a loop to print all the notes.
Related
Our teacher requires us to use turbo c on our assignments. The program is working fine on other compiler applications but it doesn't with turbo c. Here is the code:
#include<stdio.h>
struct Name
{
char last[100];
char first[100];
};
struct Address
{
char province[100];
int zipCode;
};
struct PBEntry
{
struct Name name;
struct Address address;
int age;
long int mobile;
}pb[10];
void showAll(struct PBEntry *p, int SIZE);
int main()
{
int choice = 1, SIZE=0, c=0, j;
clrscr();
printf("Welcome to Phonebook Application\n");
while(choice != 4 )
{
struct PBEntry *p;
struct PBEntry edit[10];
printMenu();
scanf("%d", &choice);
switch(choice)
{
case 1:
printf("Enter Entry %d\n", SIZE);
printf("Enter full name: ");
gets(pb[SIZE].name.first);
gets(pb[SIZE].name.last);
printf("Enter province: ");
gets(pb[SIZE].address.province);
printf("Enter ZIP code: ");
scanf("%d", &pb[SIZE].address.zipCode);
printf("Enter age: ");
scanf("%d", &pb[SIZE].age);
printf("Enter mobile number: ");
scanf("%ld", &pb[SIZE].mobile);
SIZE++;
break;
case 2:
printf("What entry number to edit?: ");
scanf("%d", &j);
printf("Change Entry %d\n", j);
printf("Change full name: ");
gets(pb[j].name.first);
gets(pb[j].name.last);
printf("Change province: ");
gets(pb[j].address.province);
printf("Change ZIP code: ");
scanf("%d", &pb[j].address.zipCode);
printf("Change age: ");
scanf("%d", &pb[j].age);
printf("Change mobile number: ");
scanf("%ld", &pb[j].mobile);
c++;
case 3:
showAll(pb, SIZE);
break;
case 4:
exit(0);
}
}
getch();
}
void showAll(struct PBEntry *p, int SIZE)
{
struct PBEntry edit[10];
int i=0, c=0;
while(i != SIZE)
{
if(c>0)
{
p = &edit;
printf("Entry %d\n", i);
printf("Name: %s %s\n", p[i].name.first, p[i].name.last);
printf("Province: %s\n", p[i].address.province);
printf("ZIP code: %d\n", p[i].address.zipCode);
printf("Age: %d\n", p[i].age);
printf("Phone number: %ld\n", p[i].mobile);
printf("\n");
}
else
{
p = &pb;
{
printf("Entry %d\n", i);
printf("Name: %s %s\n", p[i].name.first, p[i].name.last);
printf("Province: %s\n", p[i].address.province);
printf("ZIP code: %d\n", p[i].address.zipCode);
printf("Age: %d\n", p[i].age);
printf("Phone number: %ld\n", p[i].mobile);
printf("\n");
}
}
i++;
}
}
int printMenu()
{
printf("\n1. Add Entry\n");
printf("2.Edit Entry\n");
printf("3 Show All\n");
printf("4 Exit\n");
}
Our phone number here has 11 digits, or 12 at maximum. But long int, and even unsigned lont in, does not work on turbo c. I don't know where I went wrong.
I am getting segmentation error?? PLS HElP
#include <stdio.h>
#include <string.h>
void display(char n2[], int x2[], char c1[], int p);
void display(char n2[], int x2[], char c1[], int p)
{
printf("Student Name : %s \n", n2[p]);
printf("Student Roll No : %d \n", x2[p]);
printf("Student Class : %s \n ", c1[p]);
}
int main()
{
char n[50], c[5], n1;
int y, x[8], x1;
int p1 = 0;
printf("Enter the number of students: \n");
scanf("%d", &y);
fflush(stdin);
for (int i = 0; i < y; i++)
{
fflush(stdin);
printf("Enter the Student Name : \n");
scanf("%s", n[i]);
fflush(stdin);
printf("Enter the Student Class : \n");
scanf(" %s", c[i]);
fflush(stdin);
printf("Enter the Student Roll No : \n");
scanf(" %d", &x[i]);
fflush(stdin);
}
fflush(stdin);
printf("Enter the Student Name and Roll Number :\n");
scanf("%s %d", &n1, &x1);
for (int i = 0; i < y; i++)
{
if ((n[i] == n1) && (x[i] == x1))
{
p1 = i;
}
else
{
printf("No Such Entry!!");
}
}
display(n, x, c, p1);
return 0;
}
The error occurs here:
scanf("%s",c[i]);
You are trying to store a string into a char (n[i]).
You should define n and the others as an array of strings instead of a string, for example:
char n[50][ 50 ], c[50][50], n1[50];
Also, you can't compare strings like you do on this line:
if ((n[i]== n1) && (x[i] == x1))
Use strcmp instead to compare strings.
#include<stdio.h>
#include<string.h>
#define MAX 50
struct student
{
int srn;
char stu_name[30];
char course[18];
char addr[50];
};
int main()
{
struct student st[MAX];
int i;
for (i = 0; i < MAX; i++)
{
printf("\nEnter name of the student %d : ", st[i].srn=i+1);
scanf("%s", st[i].stu_name);
printf("\nEnter course of the student %d : ", i+1);
scanf("%s", st[i].course);
printf("\nEnter address of the student %d : ", i+1);
scanf("%s", st[i].addr);
}
for (i = 0; i < MAX; i++)
{
printf("\nname of student %d is %s", i+1, st[i].stu_name);
printf("\ncourse is %s", st[i].course);
printf("\naddr is %s", st[i].addr);
}
return 0;
}
i wrote this code for a school project but codeblocks keeps giving me this error.Anyone know the solution?
main.c|21|error: '(struct student *)&st' is a pointer; did you mean to use '->'?|
You cannot assign values in the same line where you are printing them, thats the whole problem
try this instead might work
st[i+1].srn
instead of
st[i].srn=i+1
The end code should look something like this
int main()
{
struct student st[MAX];
int i;
for (i = 0; i < MAX; i++)
{
st[i].srn = i+1;
printf("\nEnter name of the student %d : ", st[i].srn);
scanf("%s", st[i].stu_name);
printf("\nEnter course of the student %d : ", i+1);
scanf("%s", st[i].course);
printf("\nEnter address of the student %d : ", i+1);
scanf("%s", st[i].addr);
}
for (i = 0; i < MAX; i++)
{
printf("\nname of student %d is %s", i+1, st[i].stu_name);
printf("\nname of student %d is %s", st[i].srn, st[i].stu_name);
printf("\ncourse is %s", st[i].course);
printf("\naddr is %s", st[i].addr);
}
return 0;
}
I tried to make a program in C to list a province and its city, but when I call the struct it only shows the last data I input.
#include <stdio.h>
int main()
{
FILE *f_structure;
int n, i, j, k;
k=3;
struct
{
char province[30], citya [30], cityb [30], cityc [30];
} list;
f_structure = fopen("city List.dat", "wb");
printf("Please input the amount of province: ");
scanf("%d", &n); getchar();
for (i=1; i<=n ;i++)
{
printf("Province : ");
gets(list.province);
printf("city : ");
scanf("%s", &list.citya);
printf("city : ");
scanf("%s", &list.cityb);
printf("city : ");
scanf("%s", &list.cityc);
getchar();
fwrite(&list, sizeof(list), 1, f_structure);
}
f_structure = fopen("city List.dat", "rb");
for(j=0;j<=k-1;j++)
{
printf("Province no %d : %s \n city : %s \n city : %s \n city : %s \n",j+1, list.province, list.citya, list.cityb, list.cityc);
}
fclose(f_structure);
return 0;
}
I am new in c programming.I just start to learn file i/o.So my problem here is after I enter all the data for write file,my program say there is a problem and exited.Which part of my program is wrong?
//THIS PROGRAM WRITE RECORDS IN A TXT.FILE AND THEN READ AND DISPLAY THE RECORDS IN A TXT.FILE
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <conio.h>
struct student
{
char MatricNo[20];
int matric[20];
char CourseNo[20];
int course[20];
char Grade[20];
float Value[20];
}s[10];
int count=0;
int i,j, no,nu;
int write();
int read(int i, int j);
int main()
{
int choice;
printf("To record data to txt type <1>\nTo read data from txt type <2>\nTo end the program type <3>\n\n");
printf("Your Choice: ");
scanf("%i",&choice);
while(choice!=3)
{
switch(choice)
{
case 1:
write();
break;
case 2:
read(i, j);
break;
case 3:
return 0;
break;
default:
break;
}
printf("To record data to txt type <1>\nTo read data from txt type <2>\nTo end the program type <3>\n\n");
printf("Your Choice: ");
scanf("%i",&choice);
}
fflush(stdin);
getchar();
return 0;
}
// for write data to txt file
int write()
{
FILE *fw;
fw=fopen("Register Table.txt","w");
printf("\nEnter number of student :");
scanf("%d",&no);
for(i=0;i<no;i++)
{
printf("\nEnter student MatricNo :");
scanf("%s",&s[i].MatricNo);
printf("\nEnter number of courses :");
scanf("%d",&nu);
for(j=0; j < nu; j++)
{
printf("\nEnter student CourseNo :");
scanf("%s",&s[j].CourseNo);
printf("\nEnter student Grade :");
scanf("%s",&s[j].Grade);
printf("\nEnter student value :");
scanf("%.2f",&s[j].Value);
fflush(stdin);
fprintf(fw,"%5s %5s %5s %.2f\n",s[i].MatricNo,s[j].CourseNo,s[j].Grade,s[j].Value);
fclose(fw);
}
}
}
int read(int i, int j)
{
FILE *fr;
//read data from txt file
fr=fopen("Register Table.txt","r");
if(!fr){
printf("not file");
return 0;
}
else
return 1;
printf("\n\n\t **Register Table **\n");
while(fscanf(fr,"%5s %5s %5s %.2f",&s[i].MatricNo,&s[j].CourseNo,&s[j].Grade,&s[j].Value)==1)
{
//display data from txt file
printf("\n\n MatricNo\t CourseNo\t Grade\t\t Value");
for(i=0;i<no;i++)
{
for(j=0; j < nu; j++)
{
printf("\n\n %5s %5s %5s %.2f",s[i].MatricNo,s[j].CourseNo,s[j].Grade,s[j].Value);
}
}
printf("\n\n");
}
fclose(fr);
}
ok,i edited my program and this is it.But now the problem is after i write the file,it could not be read.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <conio.h>
struct student
{
char MatricNo[20];
int matric[20];
char CourseNo[20];
int course[20];
char Grade[20];
float Value[20];
}s[10];
int count=0;
int i,j, no,nu;
int write();
int read(int no, int nu);
FILE *fw;
int main()
{
int choice;
printf("To record data to txt type <1>\nTo read data from txt type <2>\nTo end the program type <3>\n\n");
printf("Your Choice: ");
scanf("%i",&choice);
while(choice!=3)
{
switch(choice)
{
case 1:
write();
break;
case 2:
read(no, nu);
break;
case 3:
return 0;
break;
default:
break;
}
printf("To record data to txt type <1>\nTo read data from txt type <2>\nTo end the program type <3>\n\n");
printf("Your Choice: ");
scanf("%i", &choice);
}
fflush(stdin);
getchar();
return 0;
}
// for write data to txt file
int write()
{
fw=fopen("Register Table.txt","w");
printf("\nEnter number of student :");
scanf("%d", &no);
for(i=0;i<no;i++)
{
printf("\nEnter student MatricNo :");
scanf("%s", &s[i].MatricNo);
printf("\nEnter number of course :");
scanf("%d", &nu);
for(j=0; j < nu; j++)
{
printf("\nEnter student CourseNo :");
scanf("%s", &s[i].CourseNo[j]);
printf("\nEnter student Grade :");
scanf("%s", &s[i].Grade[j]);
printf("\nEnter student value :");
scanf("%f", &s[i].Value[j]);
}
}
fprintf(fw,"%s %s %s %.2f\n", s[i].MatricNo, s[i].CourseNo[j], s[i].Grade[j], s[i].Value[j]);
fclose(fw);
}
int read(int no, int nu)
{
FILE *fr;
//read data from txt file
fr=fopen("Register Table.txt","r");
printf("\n\n\t **Register Table **\n");
for(i=0;i<no;i++)
{
for(j=0; j < nu; j++)
{
fscanf(fr,"%s %s %s %.2f", &s[i].MatricNo, &s[i].CourseNo[j], &s[i].Grade[j], &s[i].Value[j]);
}
}
//display data from txt file
printf("\n\n MatricNo\t CourseNo\t Grade\t\t Value");
for(i=0;i<no;i++)
{
for(j=0; j < nu; j++)
{
printf("%s %s %s %.2f", s[i].MatricNo, s[i].CourseNo[j], s[i].Grade[j], s[i].Value[j]);
}
}
printf("\n");
fclose(fr);
}
1-you can not write on file because you close the FILE pointer fw in write() function in for loop.
2-You can not read because you exit read() function either file is valid or invalid with this code.
if(!fr){
printf("not file");
return 0;
}
else
return 1;
3- your read() function can work only if you call it after write(). Because code does not know value of no and nu if you do not call write() firstly.
I think this code works for you
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct student
{
char MatricNo[20];
int matric[20];
char CourseNo[20];
int course[20];
char Grade[20];
float Value[20];
}
s[10];
int count=0;
int i,j, no,nu;
int write();
int read(int no, int nu);
FILE *fw;
int main()
{
int choice;
printf("To record data to txt type <1>\nTo read data from txt type <2>\nTo end the program type <3>\n\n");
printf("Your Choice: ");
scanf("%i",&choice);
while(choice!=3)
{
switch(choice)
{
case 1:
write();
break;
case 2:
read(no, nu);
break;
case 3:
return 0;
break;
default:
break;
}
printf("To record data to txt type <1>\nTo read data from txt type <2>\nTo end the program type <3>\n\n");
printf("Your Choice: ");
scanf("%i", &choice);
}
fflush(stdin);
getchar();
return 0;
}
// for write data to txt file
int write()
{
fw=fopen("Register Table.txt","w");
printf("\nEnter number of student :");
scanf("%d", &no);
for(i=0;i<no;i++)
{
printf("\nEnter student MatricNo :");
scanf("%s", &s[i].MatricNo);
printf("\nEnter number of course :");
scanf("%d", &nu);
for(j=0; j < nu; j++)
{
printf("\nEnter student CourseNo :");
scanf("%s", &s[i].CourseNo[j]);
printf("\nEnter student Grade :");
scanf("%s", &s[i].Grade[j]);
printf("\nEnter student value :");
scanf("%f", &s[i].Value[j]);
fprintf(fw,"%s %s %s %.2f\n", &s[i].MatricNo, &s[i].CourseNo[j], &s[i].Grade[j], &s[i].Value[j]);
}
}
fclose(fw);
}
int read(int no, int nu)
{
FILE *fr;
//read data from txt file
fr=fopen("Register Table.txt","r");
printf("\n\n\t **Register Table **\n");
for(i=0;i<no;i++)
{
for(j=0; j < nu; j++)
{
fscanf(fr,"%s %s %s %.2f", &s[i].MatricNo, &s[i].CourseNo[j], &s[i].Grade[j], &s[i].Value[j]);
}
}
//display data from txt file
printf("\n\n MatricNot CourseNot Gradett Value");
for(i=0;i<no;i++)
{
for(j=0; j < nu; j++)
{
printf("\n%s %s %s %.2f", &s[i].MatricNo, &s[i].CourseNo[j], &s[i].Grade[j], &s[i].Value[j]);
}
}
printf("\n");
fclose(fr);
}