while循环中的交叉初始化错误(c ++程序)

时间:2016-04-20 07:31:35

标签: c++

我在这里做了很多错事。 Plz帮助。

#include <iostream>
#include <fstream>

using namespace std;

class student {
    int rno;
    char name[15];
    float marks;

public:
    void getdata()
    {
        cout << "\nEnter Roll No. : ";
        cin >> rno;

        cout << "\nEnter Name : ";
        cin >> name;

        cout << "\nEnter Marks : ";
        cin >> marks;
    };

    void writedata()
    {
        fstream input;
        input.open("stu.dat", ios::out | ios::app);
        input.write((char*)this, sizeof(student));
        input.close();
    };

    void readdata()
    {
        int temp;
        cout << "\nEnter roll no: ";
        cin >> temp;

        fstream output;
        output.open("stu.dat", ios::in);
        output.seekg(0, ios::beg);

        while (output.read((char*)this, sizeof(student))) {
            if (rno == temp) {
                cout << "\nRoll no. : " << rno
                     << "\nName : " << name
                     << "\nMarks : " << marks;
            }
            else {
                cout << "\nWrong Roll no entered";
            };
        };
    };
};

int main()
{
    student s[25];
    student* st;

    char ans = 'y', ans2 = 'y', ans3 = 'y';

    int o, i = 0, j = 0;

    do {
        cout << "\nEnter your choice\n1.Write\n2.Read";
        cin >> o;

        switch (o) {
        case 1:
            while (ans2 == 'y' || ans2 == 'Y') {    
                s[i].getdata();
                s[i].writedata();

                cout << "\nDo you want to continue?";    
                cin >> ans2;

                i++;
            };
            break;
        case 2:
            while (ans3 == 'y' || ans3 == 'Y') {
                st->readdata();
                cout << "\nDo you want to continue?";
                cin >> ans3;

                j++;
            };

            break;
        };
    } while (ans == 'y' || ans == 'Y');

    return 0;
}

1 个答案:

答案 0 :(得分:0)

直到第82行:

 st->readdata();

其中变量st未初始化但已使用。