Hi, i just started C++ 2 days ago and still learning how to code. How to do edit database by using ID number?
For example i have a data text file called sports.txt and below is the data.
ID sport rating
1 baseball 8
2 basketball 9
3 swimming 7
4 jogging 6
5 tennis 5
I have a main menu which goes like this:
Welcome. Please input choice:
1. add sports
2. edit sports
3. delete sports
4. view sports
after user select 2, it will prompt the user to edit sports by ID:
which sports do you want to edit?
1. baseball
2. basketball
3. swimming
4. jogging
5. tennis
so lets say i select 3, the system will prompt user to change sports and input new rating
"Enter sports:" football
"Enter rating:" 10
Once done: "Entry updated."
After entry is inserted, the system will show all sports:
ID Sports Rating
1 baseball 8
2 basketball 9
3 football 10
4 jogging 6
5 tennis 5
For now out of the 4 selection, i only know how to do view sports and add sports.
My current code as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
|
//add, edit, delete, view sports and rating
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
void addSports();
void editSports();
void delSports();
void viewSports();
int main() {
cout <<"Welcome: Please input choice" << endl;
cout <<"1.Add sport" << endl;
cout <<"2.Edit sport" << endl;
cout <<"3.Delete sport" << endl;
cout <<"4.View sport" << endl;
cout <<"5.Exit" << endl;
int choice;
cin >> choice;
switch(choice)
{
case 1:
addSports();
break;
case 2:
editSports();
break;
case 3:
delSports();
break;
case 4:
viewSports();
break;
}//switch
}//main
void addSports()
{
int sportID;
string sportName;
int rating;
cout <<"Enter the ID:" << endl;
cin >> sportID;
cout <<"Enter the Sports:" << endl;
cin >> sportName;
cout <<"Enter the rating:" << endl;
cin >> rating;
ofstream myfile;
myfile.open("sports.txt", ios::app | ios::out);
myfile << endl << sportID;
myfile << sportName;
myfile << rating << endl;
myfile.close();
cout <<"Entry Updated"<< endl;
//HOW DO I STOP THE PROGRAM FROM GOING OUT???
//AFTER I ADDED THE RECORDS IT WILL STOP RUNNING ='(
}//addSports
void editSports()
{
//HOW DO I DO THIS PART?
}//editSports
void delSports()
{
//HOW DO I DO THIS PART?
}//delSports
void viewSports()
{
cout <<"ID Sports Rating" << endl;
string line;
ifstream myFile("sports.txt");
if (myFile.is_open())
{
while(myFile.good())
{
getline(myFile,line);
cout << line << endl;
}//while
myFile.close();
}
}//viewSports
|
Please help me with
1.Edit and
2.Delete,
3.As well as keeping the loop in after i finished adding records.
Thanks in advance!!