Grading System Progaramming

I have trouble making my program my only problem rn is delete section and putting file handling in my program THis is my code down below.:

#include<iostream>
#include<fstream>
#include<cctype>
#define n 30
#include<iomanip>
#include<cstring>
using namespace std;

struct studRecord{
int id;
char name[50];
char sub1[50];
char sub2[50];
char sub3[50];
char sub4[50];
char sub5[50];
int subg1, subg2, subg3, subg4, subg5;
int finalgrade;
}s[n];

void newLine(){
char s;
do{cin.get(s);
}while(s!='\n');
}

void AddSubject(int &num){


}

void AddRecord(int &num,int &j, char choice){

do{

cout<<"\t\t\t-------------- ADDING A NEW RECORD --------------"<<endl;
cout<<"\t\t\tEnter Student ID: ";
cin>>s[num].id;
newLine();
cout<<"\t\t\tEnter Student Name: ";
cin.getline(s[num].name,49);
cout<<"\t\t\t1. Enter "<<s[j].sub1<<" Grade: ";
cin>>s[num].subg1;
cout<<"\t\t\t2. Enter "<<s[j].sub2<<" Grade: ";
cin>>s[num].subg2;
cout<<"\t\t\t3. Enter "<<s[j].sub3<<" Grade: ";
cin>>s[num].subg3;
cout<<"\t\t\t4. Enter "<<s[j].sub4<<" Grade: ";
cin>>s[num].subg4;
cout<<"\t\t\t5. Enter "<<s[j].sub5<<" Grade: ";
cin>>s[num].subg5;
num++;

cout<<endl;
cout<<endl;
cout<<"\t\t\tDo you want to Add another record? (Y/N)?";
cin>>choice;
system("cls");
}while (toupper (choice)== 'Y');
system("cls");


}

void ShowRecord(int &num, int passgrade){

int i=0;
int j=0;
if(num == 0){
cout<<"-------------- There is no record to show --------------\n \n \n";
}else{
cout<<"\t\t\t-------------- STUDENT RECORD LIST --------------\n\n\n"<<endl;


for(int i=0;i<num;i++){
cout<<"\t\t\tID: "<<s[i].id<<endl;
cout<<"\t\t\tName: "<<s[i].name<<endl;
cout<<"\t\t\t"<<s[j].sub1<<" Grades: "<<s[i].subg1<<endl;
cout<<"\t\t\t"<<s[j].sub2<<" Grades: "<<s[i].subg2<<endl;
cout<<"\t\t\t"<<s[j].sub3<<" Grades: "<<s[i].subg3<<endl;
cout<<"\t\t\t"<<s[j].sub4<<" Grades: "<<s[i].subg4<<endl;
cout<<"\t\t\t"<<s[j].sub5<<" Grades: "<<s[i].subg5<<endl;
s[i].finalgrade = (s[i].subg1 + s[i].subg2 + s[i].subg3 + s[i].subg4 + s[i].subg5) / 5;
cout<<"\t\t\tAverage: "<<s[i].finalgrade<<endl;
cout<<"\t\t\tRemarks: ";
if(s[i].finalgrade >= passgrade){
cout<<"PASSED";
}else{
cout<<"FAILED";
}
cout<<endl;
cout<<"\t\t\t--------------------------------------------------";
cout<<endl;
cout<<endl;
}
}
cout<<endl;
cout<<endl;
cout<<"\t\t\tPress Any Key to End Viewing....";
system("pause > nul");
system("cls");
}
void FindRecord(int num, int passgrade){
bool found=false;
int search, j=0;


cout<<"\t\t\t-------------- SEARCH STUDENT --------------\n\n\n"<<endl;
cout<<"\t\t\tEnter ID you want to search: ";
cin>>search;

for(int i=0; i<num; i++)
if(search == s[i].id){
cout<<endl;
cout<<"\t\t\tID: "<<s[i].id<<endl;
cout<<"\t\t\tName: "<<s[i].name<<endl;
cout<<"\t\t\t"<<s[j].sub1<<" Grades: "<<s[i].subg1<<endl;
cout<<"\t\t\t"<<s[j].sub2<<" Grades: "<<s[i].subg2<<endl;
cout<<"\t\t\t"<<s[j].sub3<<" Grades: "<<s[i].subg3<<endl;
cout<<"\t\t\t"<<s[j].sub4<<" Grades: "<<s[i].subg4<<endl;
cout<<"\t\t\t"<<s[j].sub5<<" Grades: "<<s[i].subg5<<endl;
s[i].finalgrade = (s[i].subg1 + s[i].subg2 + s[i].subg3 + s[i].subg4 + s[i].subg5) / 5;
cout<<"\t\t\tAverage: "<<s[i].finalgrade<<endl;
cout<<"\t\t\tRemarks: ";
if(s[i].finalgrade >= passgrade){
cout<<"PASSED"<<endl;
}else{
cout<<"FAILED"<<endl;
}
found = true;
}if(!found){
cout<<"\t\t\tStudent Doesn't Exist...";
}

cout<<endl;
cout<<endl;
cout<<"\t\t\tPress Any Key to Quit Searching....";
system("pause > nul");
system("cls");

}

void DeleteRecord(int num, int passgrade ){

int i=0;
int j=0;
int k,index,del;

cout<<"\t\t\t-------------- STUDENT RECORD LIST --------------\n\n\n"<<endl;


for(int i=0;i<num;i++){
cout<<"\t\t\tID: "<<s[i].id<<endl;
cout<<"\t\t\tName: "<<s[i].name<<endl;
cout<<"\t\t\t"<<s[j].sub1<<" Grades: "<<s[i].subg1<<endl;
cout<<"\t\t\t"<<s[j].sub2<<" Grades: "<<s[i].subg2<<endl;
cout<<"\t\t\t"<<s[j].sub3<<" Grades: "<<s[i].subg3<<endl;
cout<<"\t\t\t"<<s[j].sub4<<" Grades: "<<s[i].subg4<<endl;
cout<<"\t\t\t"<<s[j].sub5<<" Grades: "<<s[i].subg5<<endl;
s[i].finalgrade = (s[i].subg1 + s[i].subg2 + s[i].subg3 + s[i].subg4 + s[i].subg5) / 5;
cout<<"\t\t\tAverage: "<<s[i].finalgrade<<endl;
cout<<"\t\t\tRemarks: ";
if(s[i].finalgrade >= passgrade){
cout<<"PASSED";
}else{
cout<<"FAILED";
}
cout<<endl;
cout<<"\t\t\t--------------------------------------------------";
cout<<endl;
cout<<endl;
}


cout<<"\t\t\tWho do you want to delete? ";
cin>>del;

if(num==0){
cout<<"\nNo data to be deleted\n\n";}
else{

for( k=0; k<num; k++ )
{
if( s[k].id==del ){
while( k != num ){
s[k].id=s[k+1].id;
s[k].subg1=s[k+1].subg1;
s[k].subg2=s[k+1].subg2;
s[k].subg3=s[k+1].subg3;
s[k].subg4=s[k+1].subg4;
s[k].subg5=s[k+1].subg5;
strcpy( s[k].name, s[k+1].name );
strcpy( s[k].sub1, s[k+1].sub1 );
strcpy( s[k].sub2, s[k+1].sub2 );
strcpy( s[k].sub3, s[k+1].sub3 );
strcpy( s[k].sub4, s[k+1].sub4 );
strcpy( s[k].sub5, s[k+1].sub5 );
k++;
}
break;
}

}
num--;
cout<<"\t\t\t The updated record"<<endl;
for(int i=0;i<num;i++){
cout<<"\t\t\tID: "<<s[i].id<<endl;
cout<<"\t\t\tName: "<<s[i].name<<endl;
cout<<"\t\t\t"<<s[j].sub1<<" Grades: "<<s[i].subg1<<endl;
cout<<"\t\t\t"<<s[j].sub2<<" Grades: "<<s[i].subg2<<endl;
cout<<"\t\t\t"<<s[j].sub3<<" Grades: "<<s[i].subg3<<endl;
cout<<"\t\t\t"<<s[j].sub4<<" Grades: "<<s[i].subg4<<endl;
cout<<"\t\t\t"<<s[j].sub5<<" Grades: "<<s[i].subg5<<endl;
s[i].finalgrade = (s[i].subg1 + s[i].subg2 + s[i].subg3 + s[i].subg4 + s[i].subg5) / 5;
cout<<"\t\t\tAverage: "<<s[i].finalgrade<<endl;
cout<<"\t\t\tRemarks: ";
if(s[i].finalgrade >= passgrade){
cout<<"PASSED";
}else{
cout<<"FAILED";
}
cout<<endl;
cout<<"\t\t\t--------------------------------------------------";
cout<<endl;
cout<<endl;
}



}
cout<<endl;
cout<<endl;
cout<<"\t\t\tPress Any Key to End Viewing....";
system("pause > nul");
system("cls");









}

int main(){
int choice, no = 0, i=0, j=0, passgrade;

cout<<"\t\t\t============ WELCOME TO GRADING SYSTEM ============\n"<<endl;
cout<<"\t\t\t\tHi! Welcome to Grading System Program\n"<<endl;
cout<<"\t\t\t\tFirst We Need You to Enter 5 Subjects\n"<<endl;
cout<<"\t\t\t\tSecond We Need You to Enter a Passing Grade\n"<<endl;
cout<<"\t\t\t===================================================\n"<<endl;

cout<<"\t\t\t\tPress Any Key to Continue on Adding a subjects";
system("pause > nul");
system("cls");

cout<<"\t\t\t==================================================="<<endl;
cout<<"\t\t\t\tNow Please Enter ( 5 ) Subjects"<<endl;
cout<<endl;
cout<<"\t\t\t\t1. Enter Subject: ";
cin>>s[i].sub1;
cout<<"\t\t\t\t2. Enter Subject: ";
cin>>s[i].sub2;
cout<<"\t\t\t\t3. Enter Subject: ";
cin>>s[i].sub3;
cout<<"\t\t\t\t4. Enter Subject: ";
cin>>s[i].sub4;
cout<<"\t\t\t\t5. Enter Subject: ";
cin>>s[i].sub5;
cout<<endl;
cout<<"\n\t\t\t\tNow Please Enter a Passing Grade: ";
cin>>passgrade;
system("cls");

do{
cout<<"\t\t\t============ WELCOME TO GRADING SYSTEM ============"<<endl;
cout<<"\t\t\t\t[1] ADD STUDENT RECORD "<<endl;
cout<<"\t\t\t\t[2] SHOW STUDENT RECORD "<<endl;
cout<<"\t\t\t\t[3] FIND STUDENT RECORD "<<endl;
cout<<"\t\t\t\t[4] DELETE STUEDNT RECORD "<<endl;
cout<<"\t\t\t\t[5] EXIT PROGRAM "<<endl;
cout<<"\t\t\t==================================================="<<endl;
cout<<"\t\t\t\tEnter your choice: ";
cin>>choice;
system("cls");
cout<<"\n \n";

switch(choice){
case 1:
AddRecord(no, j, choice);
break;
case 2:
ShowRecord(no,passgrade);
break;
case 3:
FindRecord(no,passgrade);
break;
case 4:
DeleteRecord(no,passgrade);

break;
}
}while(choice!=5);
{
cout<<"Thank you for using our program";
system("pause > nul");
}
}
Last edited on
I have trouble making my program my only problem rn is delete section and putting file handling in my program

First of all I would say that you have more problems than just in the delete section and file handling. IMO, you have way too much code duplication in which the leading cause is probably your structure definition. I would recommend starting with something more like:

1
2
3
4
5
6
7
8
9
10
11
12
13
struct Subject
{
    std::string name;
    int grade;
};

struct StudentRecord
{
    int id;
    std::string name;
    std::vector<Subject> subjects;
    int finalgrade;
};   // Notice not using that horrible global variable. 


Note: If you're not allowed to use std::string then you can use the horribly dangerous C-strings instead. And if you're not allowed to use std::vector then you can complicate things by using arrays. Using std::vector and std::string will greatly simplify your code with the added benefit of being much safer.

Any time you start tacking numbers onto variables usually means you should be using vectors or arrays instead.

Also try to avoid mixing the extraction operator>> and getline() when ever possible, but if you must then the easiest way of avoiding problems is with one of the following:

1
2
3
4
5
// Discard any whitespace after the number. 
std::cin >> SomeIntVariable >> std::ws;

// or Discard all leading whitespace before trying to retrieve the string.
getline(std::cin >> ws, SomeStringVariable);


Edit: Also in future please use code tags when posting code.

Last edited on
Topic archived. No new replies allowed.