Edit data with .txt file using ID

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!!
Last edited on
Hint: figure out how to load the datafile into memory, make all changes in memory, and only when done write all data back to the file.

Also:
94
95
96
97
        while(getline(myFile,line))
        {
            // do something with 'line'
        }


Hint2: You'll probably want a struct to hold the ID, sport name, and rating for each sport you read from file. Use something that can handle a list of sport ID/name/rating structs to hold the file contents.

Hope this helps.

[edit] "when done" can be any time: before your program exits, every two minutes, or every time a change is made. [/edit]
Last edited on
Thanks for getting back to me!

Hint 1: how do i do that?

also when i tried your code i got alot more errors.

Hint 2: i have not seem any guide about structs. what happens when i use it to handle my sport ID, name and rating?

i dont understand what does your "when done" means.

i am really sorry i am really new to c++ or even programming. Currently i am on my 3rd day of c++. Its interesting but complicated.

@ aeroscout Since you are really new to both C++ and programming itself, you might want to put together something a little simpler to avoid having to learn twenty things at once.

What I'd suggest is maybe have an input file which looks like this:
sport
baseball
basketball
swimming
jogging
tennis


Then write a program to read that file into a vector of strings. You could add code to display, add, delete or rename an item.

When the session is complete (you don't want to change anything more), save the contents of the vector back to the text file and end the program.

That gives you an outline to work from, which you can test and get working properly.

After that, you can learn about how to use a struct, and produce a modified version of your program which will be able to handle your original text file containing "ID sport rating" etc.
Last edited on
In the third day of class you should have already been taught to use struct, but unfortunately that is often not the case.

It is okay to use three arrays/vectors/whatever:

Using three arrays:
1
2
3
vector<int>    sportIDs;
vector<string> sportName;
vector<int>    sportRanking;

Using a struct:
1
2
3
4
5
6
7
struct sport
{
  int    ID;
  string name;
  int    rating;
};
vector<sport> sports;

You can read a line from file easily:
1
2
3
4
5
6
7
  ifstream f("sports.txt");
  f.ignore( numeric_limits<streamsize>::max(), '\n' );  // skip the first line
  int id;
  string name;
  int rating;
  while (f >> id >> name >> rating)
  {
8
9
10
11
    sportIDs.push_back(id);
    sportNames.push_back(name);
    sportRatings.push_back(rating);
  }
if using three arrays/vectors/whatever
8
9
    sports.push_back( sport{id, name, rating} );
  }
if using a struct



The "when done" has an antecedent:
Hint: figure out how to load the datafile into memory, make all changes in memory, and only when done [making changes] write all data back to the file.



Also, as the code I suggested to you has fewer function calls and fewer lines, but introduces no different calls than already found in your code, so I have no idea how you could have gotten more errors by simply replacing the given lines with the code I suggested.

In other words, you did something wrong. Try again and you'll eventually get it.


Programming has a steep learning curve. Keep trying. You'll be frustrated enough to wanna quit a few times over before it starts coming together, but this is just part of getting your brain to make sense of it all.
closed account (48T7M4Gy)
This is one way of programming the continuous menu so you don't get thrown out every time.

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
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>


using namespace std;

void addSports();
void editSports();
void delSports();
void viewSports();

int main() {
    // PREPARE THE MENU AS A SINGLE STRING
    string
    line1 = "Welcome: Please input choice\n",
    line2 = "1. Add sport\n",
    line3 = "2. Edit sport\n",
    line4 = "3. Delete sport\n",
    line5 = "4. View sport\n",
    line6 = "5. Exit\n",
    menu = line1 + line2 + line3 + line4 + line5 + line6;
    
    // USE CHARACTER INSTEAD OF INTEGER
    char choice = '0';
    
    while(cout << menu && cin >> choice and choice != '5')
    {
        switch(choice)
        {
            case '1':
                cout << "Option 1 selected\n";
                //addSports();
                break;
                
            case '2':
                cout << "Option 2 selected\n";
                //editSports();
                break;
                
            case '3':
                cout << "Option 3 selected\n";
                //delSports();
                break;
                
            case '4':
                cout << "Option 4 selected\n";
                //viewSports();
                break;
                
            default:
                cout << "Invalid choice\n";
                choice = 0;
        }//switch
    }
    
}//main 
closed account (48T7M4Gy)
the addSports() function is on the right track but there are a few simple improvement to make life easier, especially when you go to read the data file with the other options.

The change I'm suggesting is to put spaces between data items.

Later on you might need to consider if the user enters invalid data (eg a non integer for the ID) or where a sport name is not a single string (eg bush walking) in which case you would use getline instead of cin. But that can be put to one side for a while. File errors are another consideration for later refinement. This tutorial has some useful samples under 'Text files' http://www.cplusplus.com/doc/tutorial/files/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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 << sportID << ' ' << sportName << ' ' << rating << '\n'; // <--
    myfile.close();
    
    cout <<"Entry Updated"<< endl;}//addSports 
Topic archived. No new replies allowed.