问题是我无法输入'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);
}
答案 0 :(得分:1)
以上代码无法在visual stdio 8编译器下编译,因为显示和输入功能需要返回值。
因此要么将返回类型更改为 void ,要么从函数返回一些int类型的值。
我无法输入'c'的值,它是一个无限循环:
如果要在无限循环中运行,则必须修改开关案例。
你的函数输入也有bug,因为它也是一个无限循环。你可以参考下面的代码并相应修改你的代码....
#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;
}
}
}