Search code examples
c++fstreamfile-handlingiostream

Display the whole file


I have an assessment about asking the user to enter his/her name and their desired score and display it. I have made progress but when I try to display all the text in the save file (I called scores.txt) it didn't print all out but instead just print the first line of the text file.

Here the code:

#include <iostream>
#include <fstream>
#include <string>
#include <stdlib.h>

using namespace std;

int score;
int highscore;
string name;

void menu() {
    cout << "1.Enter a score" << endl;
    cout << "2.Display scores" << endl;
    cout << "3.Exit" << endl;
}
void enterScore() {
    cout << "Enter your desired score: ";
    cin >> score;
    cout << "Please enter your name and seperate by a (_): ";
    cin.ignore();
    getline(cin, name);
}
void displayScore() {

    cout << "Display scores: " << endl;
}
void exit() {

}
int main()
{
    ofstream outFile("scores.txt", ios::app);
    ifstream inFile("scores.txt", ios::in);
    int choice;
    char yesOrNo;

    do
    {
        menu();
        cout << "Enter your choice: ";
        cin >> choice;

        switch (choice)
        {
        case 1: enterScore();
            outFile << name << ": " << score << endl;
            break;
        case 2: displayScore();
            inFile >> name;
            inFile >> score;

            cout << name << " " << score << endl;
            break;

        case 3: exit();
            break;
        default:
            cout << "Error!!!" << endl;
            break;
        }
        cout << "Would you like to try again? (y/n): ";
        cin >> yesOrNo;
        system("cls");
    } while (yesOrNo == 'y');
    return 0;
}

How can I display all the text in the file and is there a way to sort the score like a highscore board. Please help and I'm very thankful.


Solution

  • Try it like this:

    #include <iostream>
    #include <fstream>
    #include <string>
    #include <stdlib.h>
    
    using namespace std;
    
    int score;
    int highscore;
    string name;
    
    void menu() {
        cout << "1.Enter a score" << endl;
        cout << "2.Display scores" << endl;
        cout << "3.Exit" << endl;
    }
    void enterScore() {
        cout << "Enter your desired score: ";
        cin >> score;
        cout << "Please enter your name and seperate by a (_): ";
        cin.ignore();
        getline(cin, name);
    }
    void displayScore() {
    
        cout << "Display scores: " << endl;
    }
    void exit() {
    
    }
    int main()
    {
        ofstream outFile("scores.txt", ios::app);
        ifstream inFile("scores.txt", ios::in);
        int choice;
        char yesOrNo;
    
        do
        {
            menu();
            cout << "Enter your choice: ";
            cin >> choice;
    
            switch (choice)
            {
            case 1: enterScore();
                outFile << name << ": " << score << endl;
                break;
            case 2: displayScore();
            while(true){
                inFile >> name;
                inFile >> score;
    
                cout << name << " " << score << endl;
                if(inFile.eof())
                    break;
            }
                break;
    
            case 3: exit();
                break;
            default:
                cout << "Error!!!" << endl;
                break;
            }
            cout << "Would you like to try again? (y/n): ";
            cin >> yesOrNo;
            system("cls");
        } while (yesOrNo == 'y');
        return 0;
    }