简体   繁体   中英

ifstream only printing first line in array



void adding();
void print();
void end();

int counter = 0;


int main()
{
    char choice;

    cout << "What do you want to do?" << endl;
    cout << "a = add a student, p = print the student list, e = exit function" << endl;
    cin >> choice;

    switch (choice) {
    case 'a':
        adding();
        break;
    case 'p':
        print();
        break;
    case 'e':
        end();
        break;
    default:
        cout << "That is not a valid option.";
        break;
    }


}

void adding()
{
    ofstream outFS;
    outFS.open("myoutfile.txt", fstream::app);
    int i = 0;
    string name[100]; string amount[100]; string grades[100];
    for (i = 0; i < 1; i++) {
        cout << "What is the name of the student?";
        cin >> name[i];

        outFS << name[0] << ' ';

        cout << "How many exercises has the student completed?";
        cin >> amount[i];

        outFS << amount[0] << ' ';

        cout << "What grade did the student get?";
        cin >> grades[i];

        outFS << grades[0] << ' ';
        counter++;
    }
    outFS.close();

    main();
}

void print()
{

        ifstream inFS;
        inFS.open("myoutfile.txt");

            string name;
            inFS >> name;

            int amount;
            inFS >> amount;

            int grades;
            inFS >> grades;
            inFS.close();

            cout << name << ' ' << amount << ' ' << grades << endl;

    main();

So when I use the "adding" function it works fine and adds it to the file. When I add the more than one student the file still shows it but when I print within the console it only prints out the first student and their info. How do I get it to show the entire array in the console? I tried to use a counter variable but it is not helping

What you are missing is the loop keywords (the various incarnations of for and while ).

Making your own loop by doing this:

void a()
{
  // Do stuff
  b();
}

void b()
{
  // Do other stuff
  a();
}

Is really not a good idea since it will just throw more and more calls on the stack, and eventually make the stack run out of space (which will crash the program). Replacing one of those functions with main() is even worse, because main() is a special function that you really are not allowed to call yourself.

Instead do something like this:

int main()
{
    char choice;
    bool running = true;

    while (running)
    {
        cout << "What do you want to do?" << endl;
        cout << "a = add a student, p = print the student list, e = exit function" << endl;
        cin >> choice;

        switch (choice) {
        case 'a':
            adding();
            break;
        case 'p':
            print();
            break;
        case 'e':
            // end(); Dunno what this does. Is it just an empty function that will finally end the infinite stack of calls?
            // I am replacing it with setting the loop variable to false, which will stop the loop
            running = false;
            break;
        default:
            cout << "That is not a valid option.";
            break;
        }
    }
}

Then remove all calls to main() from the rest of your code.

As for your print() function, again your problem is the lack of loops. Your current code opens the file, reads the first line, prints the line, then closes the file and calls main. Then the next time print() is called it again opens the file, reads the first line, etc.

What you need is a loop that reads all the lines before closing the file again. Something like this should do the trick:

void print()
{
    ifstream inFS;
    inFS.open("myoutfile.txt");

    string name;
    int amount;
    int grades;
    while (inFS >> name >> amount >> grades)
    {
        cout << name << ' ' << amount << ' ' << grades << endl;
    }

    inFS.close();
}

However, all of this is pretty basic C++ that should have been covered in whatever book you are using to learn C++. And if you do not have a C++ book to learn from, then I strongly recommend that you get one - it will be only get harder to learn if you constantly get stuck on basic stuff.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM