C++ 请指教,这个程序怎么停不下来?
#include<iomanip>
#include<string>
using namespace std;
char ch; static int n=0;
class Student
{
public:
void get_information();
void output();
private:
string number,name;
char sex;
};
int main()
{
for(int i ;;i++){
Student i ;
i.get_information();
cout<<"if you have input all the information, please enter 'y' ";
cin>>ch;
if(ch=='y')break;
}
for(;i<=n;i++)
{
Student i ;
i.output();
}
cout<<"the program of the information's input and output about students has executed "<<n<<" times\n";
return 0;
}
void Student::get_information()
{
cout<<"please input your name:";
cin>>name;
cout<<"please input your student number:";
cin>>number;
cout<<"please choose your sex. female(f) or male(m):";
cin>>sex;
++n;
}
void Student::output()
{
cout<<"name:"<<name<<"\t"<<"sex:"<<sex<<"\t"<<"number:"<<number<<endl;
} 展开
#include<iostream>
#include<iomanip>
#include<string>
using namespace std;
char ch; static int n=0;
string number,name;
char sex;//定义全局变量,后面函数才能读入
class Student
{
public:
void get_information();
void output();
};
int main()
{
for(int i=1 ;;i++){//初始化变量i;
Student j;//对象名不能和循环内的变量同名
j.get_information();
cout<<"if you have input all the information, please enter 'y' ";
cin>>ch;
if(ch=='y')break;
}
for(int i=1;i<=n;i++)
{
Student j;
j.output();
}
cout<<"the program of the information's input and output about students has executed "<<n<<" times\n";
return 0;
}
void Student::get_information()
{
cout<<"please input your name:";
cin>>name;
cout<<"please input your student number:";
cin>>number;
cout<<"please choose your sex. female(f) or male(m):";
cin>>sex;
++n;
}
void Student::output()
{
cout<<"name:"<<name<<"\t"<<"sex:"<<sex<<"\t"<<"number:"<<number<<endl;
}
还有你的程序逻辑有错误,若是要输入多个学生信息,应该用数组或是链表保存信息,否则会造成前面的信息丢失,另外变量名不要用相同的。有什么问题再留言吧
再试一试应该没什么问题了吧