Quantcast

Jump to content


Photo

C++ Help?

c++

  • Please log in to reply
No replies to this topic

#1 FinalBlast

FinalBlast
  • 1 posts

Posted 11 December 2013 - 03:07 PM

So I'm supposed to do this:

"For a group of students there is a given Name, Personal Identification Number (ex. 9209153460),grades from physics,math,programming. Create a program that contains information in a file and let's you choose the following:

-create an empty file.

-add information about students

-change information about a student by PIN

-extract/save text file of student's PIN, name and faculty number which have to show up on makeup exam(when you repeat exam)"

 

So far this is where I am, but can't quite figure out what to do? (Using DevC++)

 

#include <stdio.h> 
#include <string.h> 
#include <stdlib.h> 
struct LICE { //Type of information at hand
 char ime[31]; char EGN[11]; 
}; 


FILE *fl; 
char imefl[30]; 
LICE lice; 
//wut
void readLice(LICE *lc){ 
 printf("%20sType the name of: ",""); 
 gets(lc->ime); 
 printf("%20sType EGN: ",""); 
 gets(lc->EGN); 
} 
//wut
void writeLice(LICE lc){ 
 printf("Name: %-21s EGN: %s\n", 
 lc.ime,lc.EGN); 
} 
int exist(char *ime){ 
 FILE *f = fopen(ime,"r");fclose(f); 
 return f!=NULL; 
} 
//wut


void szdPrz(){ 
char ch; 
 if (exist(imefl)) { 
 printf("File Exists "); 
 printf("Should I overwrite?"); 
 ch=getchar(); 
 if (ch == 'n') return; 
 } 
 fl=fopen(imefl,"w"); 
 fclose(fl); 
 printf("\n File created.\n"); 
} 
//wut
void dobaviane(){ 
 char ch; 
 if (!exist(imefl)) { 
 printf("File not created!\n");return; 
 } 
 fl=fopen(imefl,"a+b"); 
 do{ 
 readLice(&lice); 
 fwrite(&lice,sizeof(lice),1,fl); 
 printf("Will you continue writting? "); 
 ch=getchar();getchar(); 
 }while (ch != 'n'); 
 fclose(fl); 
} 
void iztrivane(){ 
 FILE *delFl; //wut 
 char EGN[11]; //wut
 if (!exist(imefl)) { 
 printf("Nope, file not created!\n");return; 
 } 
 printf("Type EGN: ");gets(EGN); 
 fl=fopen(imefl,"rb");delFl=fopen("Del","wb"); 
 fread(&lice,sizeof(lice),1,fl); 
 while (!feof(fl)){ 
 if (strcmp(lice.EGN,EGN)!=0) 
 fwrite(&lice,sizeof(lice),1,delFl); 
 else printf("Info EGN %s Deleted.\n",EGN); 
 fread(&lice,sizeof(lice),1,fl); 
 } 
 fclose(fl); fclose(delFl); 
 remove(imefl); rename("Del",imefl); 
} 
void zamiana(){ 
 char EGN[11];// wut
 if (!exist(imefl)) 
 { printf("File not created!\n");return; } 
 printf("Type EGN: ");gets(EGN); 
 fl=fopen(imefl,"r+b"); 
 fread(&lice,sizeof(lice),1,fl); 
 while (!feof(fl)){ 
 if (strcmp(lice.EGN,EGN)==0){ 
 readLice(&lice); 
 fseek(fl,-(long)sizeof(lice),SEEK_CUR); 
 fwrite(&lice,sizeof(lice),1,fl); 
 printf("Info for EGN %s Saved.\n",EGN); 
 break; 
 } 
 fread(&lice,sizeof(lice),1,fl); 
 } 
 fclose(fl); printf("\n"); 
} 


void spisGod(){ 
 int k,godTrs,godLice; 
 char gg[3]; 
 if (!exist(imefl)) 
 { printf("File not created!\n");return; } 
 printf("Type the birth year: "); 
 scanf("%d",&godTrs);getchar(); 
 fl=fopen(imefl,"rb"); k=0; 
 fread(&lice,sizeof(lice),1,fl); 
 while (!feof(fl)){ 
 strncpy(gg,lice.EGN,2); gg[2]=0; godLice=atoi(gg); 
 if (lice.EGN[2]>'3') godLice+=2000; 
 else godLice+=1900; 
 if (godLice==godTrs) {k++;writeLice(lice);} 
 fread(&lice,sizeof(lice),1,fl); 
 } 
 if (k==0) 
 printf("Nobody born in %d ã.\n",godTrs); 
 fclose(fl); 
} 


void obSpis(){ 
 int k; 
 if (!exist(imefl)) { 
 printf("File not created!\n"); 
 return; 
 } 
 fl=fopen(imefl,"rb"); k=0; 
 fread(&lice,sizeof(lice),1,fl); 
 while (!feof(fl)){ 
 k++; writeLice(lice); 
 fread(&lice,sizeof(lice),1,fl); 
 } 
 if (k==0) printf("File is empty\n"); 
 fclose(fl); 
} 


int main()
{ 
int c; 
 printf("File name: ");gets(imefl); 
 do { 
 printf("%20sMENU FOR OPERATIONS:\n",""); 
 printf("%26s1. create a new file\n",""); 
 printf("%26s2. add components\n",""); 
 printf("%26s3. del components\n",""); 
 printf("%26s4. replace components\n",""); 
 printf("%26s5. born in\n",""); 
 printf("%26s6. list of all\n",""); 
 printf("%20sSelect or 0 for end: ",""); 
 scanf("%d",&c);getchar(); 
 switch (c){ 
 case 1:szdPrz();break; 
 case 2:dobaviane();break; 
 case 3:iztrivane();break; 
 case 4:zamiana();break; 
 case 5:spisGod();break; 
 case 6:obSpis(); 
 } 
 }while (c!=0); 
}

 




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users