can anyone plzzz help me with this....basically i have to rewrite the program so that the data will store permanently in a file and it can be retrieved for adding, editing and deleting......
how do i do this.....
this is the code....
# include
<iostream>
# include
<string>
# include
<cctype>
# include
<iomanip>
using
namespace std;
struct
student // the structure of the students marks
{
// storage
string id;
float phy;
float chem;float peng;
float math;
}marks [200];
void
menu (); // function for menu
void
help (); // function for help
void
add (); // function for new
void
edit (); // function for edit
void
del (); // function for delete
void
display (); // function for display
void
exit (); // function for exitint z = 0; // storage that will set the integer value so that when run in the main program it wont turn 0
// function main begins program execution
int main ()
{
// storagechar choice;int n=0;
while(1)
{
menu();
// call menu function
cin >> choice;
choice = toupper(choice);
// change input to uppercaseif(choice == '?')
{
system ("cls");
help ();
// call help function
}
else if(choice == '+')
{
system ("cls");
add ();
// call add function
}
else if(choice == '#')
{
system ("cls");
edit ();
// call edit function
}
else if(choice == '-')
{
system ("cls");
del ();
// call delete function
}
else if (choice == '=')
{
system ("cls");
display ();
// call display function
}
else if (choice == 'X')
{
system("cls");
exit ();
// call the exit functionreturn 0;
}
else
{
cout << "Incorrect sign." << endl;
system (
"pause");system("cls");
cout << endl;
}
};
return 0; // indicate that program end successfully}// end function main
// function menu begins program execution
void menu()
{
// outputcout << "----------------------------MENU-----------------------------" << endl;
cout << setw(10)<< left <<
"1.HELP" << setw(5) << left << " -->" << " ?" << endl;cout << setw(10)<< left << "2.ADD" << setw(5) << left << " -->" << " +" << endl;
cout << setw(10)<< left <<
"3.EDIT" << setw(5) << left << " -->" << " #" << endl;cout << setw(10)<< left << "4.DELETE" << setw(5) << left << " -->" << " -" << endl;
cout << setw(10)<< left <<
"5.DISPLAY" << setw(5) << left << " -->" << " =" << endl;cout << setw(10)<< left << "6.EXIT" << setw(5) << left << " -->" << " X" << endl << endl << endl;
cout <<
"P/S : If u have not entered any data, please do so before proceed to '#' and '='. Enter '+' to add new data." << endl << endl;cout << "Please enter your choice : ";
}
// end function menu
// function help begins program execution
void help()
{
// output
cout << endl;
cout << "This program basically will ask user to enter the students ID's and their exam marks for certain subjects. ";
cout <<
"User can either add a new set of data by entering '+' sign, ";cout << "edit or make a correction in the data by entering '#' sign, ";
cout <<
"delete a data by entering the '-' sign, and also display ";cout << "the data on the screen by entering '=' sign. To exit ";cout << "program, user should enter the letter 'X'.";
cout << endl << endl;
system(
"pause");}// end function help
// function add begins program execution
void add ()
{
char ans;
int n;
// call the goto loop
loop:
n=z;
cout << "Enter ID [Eg : 07UEB01234] : ";
fflush (stdin);
// so that the program can read properly
getline(cin,marks.id);
// the for loop is to change the whole string to uppercasefor (int j=0; j<((marks.id).length()); ++j)
{
(marks.id)[j]=toupper((marks.id)[j]);
}
a:
cout << "Enter Physic's marks [Eg : 50] : ";
cin >> marks.phy;
if (marks.phy < 0 || marks.phy > 100)
{
cout << "Please enter between 0 - 100 only." << endl << endl;goto a;
}
b:
cout << "Enter Chemistry's marks [Eg : 50] : ";
cin >> marks.chem;
if (marks.chem < 0 || marks.chem > 100)
{
cout << "Please enter between 0 - 100 only." << endl << endl;goto b;
}
c:
cout << "Enter Pengajian Am's marks [Eg : 50] : ";
cin >> marks.peng;
if (marks.peng < 0 || marks.peng > 100)
{
cout << "Please enter between 0 - 100 only." << endl << endl;goto c;
}
d:
cout << "Enter Math's marks [Eg : 50]: ";
cin >> marks.math;
if (marks.math < 0 || marks.math > 100)
{
cout << "Please enter between 0 - 100 only." << endl << endl;goto d;
}
z++;
// call the goto loop
aaa:
cout << "Continue?. [Y(to add more data)/N(menu)] : ";
cin >> ans;
ans = toupper(ans);
cout << endl << endl;
if (ans == 'Y')
goto loop;else if (ans != 'N')
{
cout << "Please enter Y or N only." << endl <<endl;goto aaa;
}
system(
"cls");}// end function add
// function edit begins program execution
void edit ()
{
// storage
string id2;
string ans;
cout << "Please enter your ID no to edit : ";
fflush (stdin);
getline(cin,id2);
// change the string to uppercase for (int j=0; j<(id2.length()); ++j)
{
id2[j]=toupper(id2[j]);
}
for(int n=0;n<=z;n++)
{
if(marks.id == id2)
{
// call the goto loop
loop:
cout << "FOUND!!. Which one would you like to edit [ID / PHYSIC / CHEMISTRY / PENG AM / MATHS ]: ";
fflush (stdin);
getline(cin,ans);
// change the string to uppercasefor (int j=0; j<(ans.length()); ++j)
{
ans[j]=toupper(ans[j]);
}
if(ans == "ID")
{
marks.id=" ";cout << "Please enter new ID number : ";
fflush (stdin);
getline(cin,marks.id);
for (int j=0; j<((marks.id).length()); ++j)
{
(marks.id)[j]=toupper((marks.id)[j]);
}
}
else if(ans == "PHYSIC")
{
marks.phy=NULL;
cout << "Please enter new Physic's marks : ";
cin >> marks.phy;
}
else if(ans == "CHEMISTRY")
{
marks.chem=NULL;
cout << "Please enter new Chemistry's marks : ";
cin >> marks.chem;
}
else if(ans == "PENG AM")
{
marks.peng=NULL;
cout << "Please enter new Peng Am's marks : ";
cin >> marks.peng;
}
else if(ans =="MATHS")
{
marks.math=NULL;
cout << "Please enter new Maths's marks : ";
cin >> marks.math;
}
else
{
cout << "Please enter the correct name [ID / PHYSIC / CHEMISTRY / PENG AM / MATHS ]" << endl << endl;goto loop;
}
system(
"cls");break;
}
if(marks.id != id2 && n == z)
{
cout << "Sorry, data not found" << endl;system ("pause");
cout << endl;
system ("cls");
}
}
}// end function edit
// function delete begins program execution
void del ()
{
string id3;
cout << "Please enter the ID number to delete the whole data : ";
fflush (stdin);
getline(cin,id3);
for (int j=0; j<(id3.length()); ++j)
{
id3[j]=toupper(id3[j]);
}
for(int n=0;n<=z;n++)
{
if(marks.id == id3)
{
marks.id = " ";
marks.phy = marks.chem = marks.peng = marks.math = NULL;
cout << "Data found and has been deleted." << endl;
system (
"pause");system("cls");break;
}
if(marks.id != id3 && n == z)
{
cout <<
"Sorry, data not found. " << endl;system("pause");system("cls");
}
}
}// end function del
// function dislay begins program execution
void display ()
{
cout << setw(15) << left << "ID" << setw(10) << left << "PHY" << setw(10) << left << "CHEM" << setw(10) << left << "PENG" << setw(10) << left << "MATH";
cout << endl << endl;
//for loop that is use to display the structure up to interger zfor (int n=0; n<z; n++)
{
// if is use to only diplay structure that is not emptyif(marks.id != " " && marks.phy != NULL && marks.chem != NULL && marks.peng != NULL && marks.math != NULL)
cout << setw(15) << left << marks.id << setw(10) << left << marks.phy << setw(10) << left << marks.chem << setw(10) << left << marks.peng << setw(10) << left << marks.math ;
cout << endl << endl;
}
cout << endl;
system("pause");
system (
"cls");} // end function display
// function exit begins program execution
void exit ()
{
//outputcout << "Thank you for using this program." << endl;
}
// end function exit
Enter your message below
Sign in or Join us (it's free).