首页 > 解决方案 > ifstream 只打印数组中的第一行

问题描述



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();

因此,当我使用“添加”功能时,它可以正常工作并将其添加到文件中。当我添加多个学生时,文件仍然显示它,但是当我在控制台中打印时,它只打印出第一个学生及其信息。如何让它在控制台中显示整个数组?我试图使用一个计数器变量,但它没有帮助

标签: c++

解决方案


您缺少的是循环关键字(forand的各种化身while)。

通过这样做来制作自己的循环:

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

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

这真的不是一个好主意,因为它只会在堆栈上抛出越来越多的调用,并最终使堆栈空间不足(这会使程序崩溃)。将其中一个函数替换main()为更糟糕的是,因为main()它是一个特殊函数,您确实不允许自己调用它。

而是做这样的事情:

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;
        }
    }
}

main()然后从其余代码中删除所有调用。

至于您的print()功能,您的问题再次是缺少循环。您当前的代码打开文件,读取第一行,打印该行,然后关闭文件并调用 main。然后下一次print()调用它再次打开文件,读取第一行,等等。

您需要的是一个在再次关闭文件之前读取所有行的循环。这样的事情应该可以解决问题:

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();
}

然而,所有这些都是非常基本的 C++,应该在你用来学习 C++ 的任何书中都有介绍。如果你没有一本 C++ 书可以学习,那么我强烈建议你去买一本——如果你经常卡在基本的东西上,它只会变得更难学习。


推荐阅读