1

问题是我无法输入“c”的值,它是一个无限循环。我看不出我做错了什么。我对 C++ 很陌生。

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

class manu
{
    private:
        int sqdno;
        string name,speciality,take;
        ofstream fileip;
        ifstream fileop;
    public:
        manu()
        {
            fileip.open("Manchester United.txt");
            fileop.open("Manchester United.txt");
        }    
    public:
        int input()
        {

            while(cin>>sqdno>>name>>speciality)
            {
              fileip<<sqdno<<' '<<name<<' '<<speciality<<endl;
            }
        }
    public:
        int display()
        {

            fileop>>take;
            while(fileop.good())
            {
                cout<<take<<endl;
                fileop>>take;
            }
        }   
};

int main()
{

    int c;
    manu m;
    cout<<"Enter squad details(press 'Ctrl+z' to exit on input):\n";

    do
    {
    cout<<"Select a choice:"<<endl;
    cout<<"1.Input to file"<<endl;
    cout<<"2.Display from file"<<endl;
    cout<<"3.Exit"<<endl;
    cin>>c;
    switch(c)
    {
        case 1:
            cout<<"Input squad_no,name and speciality of players:";
            m.input();
            break;
        case 2:
            m.display();
            break;
        default:
            cout<<"enter a valid choice";
    }
    }while(c<3);
}
4

1 回答 1

1

上面的代码在visual stdio 8编译器下无法编译,因为显示输入函数需要一个返回值。

因此,要么将返回类型更改为void,要么从函数返回一些 int 类型的值。

我无法输入“c”的值,它是一个无限循环:

如果你想在无限循环中运行,你必须修改你的 switch case。

而且你的函数输入也有错误,因为它也是一个无限循环。你可以参考下面的代码并相应地修改你的代码......

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

class manu
{
    private:
        int sqdno;
        string name,speciality,take;
        ofstream fileip;
        ifstream fileop;
    public:
        manu()
        {
            fileip.open("Manchester United.txt");
            fileop.open("Manchester United.txt");
        }    
    public:
        void input()
        {

            cin>>sqdno>>name>>speciality;

            fileip<<sqdno<<' '<<name<<' '<<speciality<<endl;

        }
    public:
        void display()
        {

            fileop>>take;
            while(fileop.good())
            {
                cout<<take<<endl;
                fileop>>take;
            }
        }   
};

int main()
{

    int c;
    manu m;
    cout<<"Enter squad details(press 'Ctrl+z' to exit on input):\n";

    while (1)
    {
        cout<<"Select a choice:"<<endl;
        cout<<"1.Input to file"<<endl;
        cout<<"2.Display from file"<<endl;
        cout<<"3.Exit"<<endl;
        cin>>c;
        switch(c)
        {
            case 1:
                cout<<"Input squad_no,name and speciality of players:";
                m.input();
                break;
            case 2:
                m.display();
                break;
            case 3:
            exit(1);
            default:
                cout<<"enter a valid choice";
                break;
        }
    }
}
于 2013-04-14T05:57:44.903 回答