最近看c++时,遇到一个关于缓存影响的有关问题,希望会的人,帮帮忙•••
最近看c++时,遇到一个关于缓存影响的问题,希望会的人,帮帮忙•••
代码:
#include<iostream>
using namespace std;
class NameTel
{
public:
NameTel() {}
void InputInformation();
void ModifyInformation();
void Display();
private:
struct Information
{
// char * p = new char [20];
char p[20]; //储存名字数组
int num; //号码
};
Information info[20];
static int i; //记录现在的已经输入的学生信息个数
int j;
//char * name = new char 15;
char name[20]; //修改时输入的名字
int ModNum; //修改时输入的号码
};
int NameTel::i = 0;
void NameTel::InputInformation()
{
cout << "请输入学生信息" << endl;
cout << "姓名:" << '\t';
cin.getline (info[i].p, sizeof(info[i].p)); //可以接收带空格的字符串
cout << "号码:" << '\t' ;
cin >> info[i].num;
i ++;
cout << endl;
}
void NameTel::ModifyInformation()
{
cout << "请输入要修改的原始姓名:" << '\t';
cin.getline (name, sizeof(name));
cout << endl;
//问题1:此处要写两次函数,才能显示一次的输入 ?
cout << "请输入要修改的原始姓名:" << '\t';
cin.getline (name, sizeof(name));
cout << endl;
for (j = 0; j < i; j ++)
{
if (strcmp(name, info[j].p) == 0)
break;
}
cout << "请输入新的名字:" << '\t' ;
cin.getline (name, sizeof(name));
strcpy(info[j].p, name);
cout << "请输入新的号码:" << '\t' ;
cin >> ModNum;
info[j].num = ModNum;
cout << endl;
}
void NameTel:: Display()
{
cout << "姓名" << '\t' << "号码" << endl;
for (j = 0; j < i; j++)
{
cout << info[j].p << '\t' << info[j].num;
}
cout << endl;
}
void main()
{
char pd;
NameTel student;
do
{
student.InputInformation ();
cout << "是否继续?y or n" << endl;
fflush(stdin);
cin >> pd;
}while (pd == 'y'); //问题2:做多次输入时,输入学生信息就有问题 ?
student.ModifyInformation ();
student.Display ();
}
问题1:中间有两个请输入要修改的原始姓名,但是却只能做一个输入
问题2:多次输入时,”请输入学生信息“的地方就只能输入号码了
/
------解决方案--------------------
do
{
student.InputInformation ();
cout << "是否继续?y or n" << endl;
fflush(stdin);
cin >> pd; //也许是这里遗留下回车符的问题 下面加个getchar();试试
}while (pd == 'y');
------解决方案--------------------
由于输入y按换行而产生的一个换行字符
do
{
student.InputInformation ();
cout << "是否继续?y or n" << endl;
fflush(stdin);
cin >> pd;
getchar(); //新添加的一行
}while (pd == 'y');
这么改两个问题都可以解决了,其实两个问题是一个问题
------解决方案--------------------
可以cin>>pd后调用cin.clear()清除缓存,这个方便些!至于你后面一个问题,你可以将Person这个类定义为虚基类,将老师和学生共同的信息函数定义为virtual,然后定义一个类,包含一个基类指针数组Person** pPerson;
在构造函数中pPerson = new Person*[N];这样就可以通过调用pPerson[0]->TeaMessage(); pPerson[0]->StuMessage();思路就是这样的。。。
代码:
#include<iostream>
using namespace std;
class NameTel
{
public:
NameTel() {}
void InputInformation();
void ModifyInformation();
void Display();
private:
struct Information
{
// char * p = new char [20];
char p[20]; //储存名字数组
int num; //号码
};
Information info[20];
static int i; //记录现在的已经输入的学生信息个数
int j;
//char * name = new char 15;
char name[20]; //修改时输入的名字
int ModNum; //修改时输入的号码
};
int NameTel::i = 0;
void NameTel::InputInformation()
{
cout << "请输入学生信息" << endl;
cout << "姓名:" << '\t';
cin.getline (info[i].p, sizeof(info[i].p)); //可以接收带空格的字符串
cout << "号码:" << '\t' ;
cin >> info[i].num;
i ++;
cout << endl;
}
void NameTel::ModifyInformation()
{
cout << "请输入要修改的原始姓名:" << '\t';
cin.getline (name, sizeof(name));
cout << endl;
//问题1:此处要写两次函数,才能显示一次的输入 ?
cout << "请输入要修改的原始姓名:" << '\t';
cin.getline (name, sizeof(name));
cout << endl;
for (j = 0; j < i; j ++)
{
if (strcmp(name, info[j].p) == 0)
break;
}
cout << "请输入新的名字:" << '\t' ;
cin.getline (name, sizeof(name));
strcpy(info[j].p, name);
cout << "请输入新的号码:" << '\t' ;
cin >> ModNum;
info[j].num = ModNum;
cout << endl;
}
void NameTel:: Display()
{
cout << "姓名" << '\t' << "号码" << endl;
for (j = 0; j < i; j++)
{
cout << info[j].p << '\t' << info[j].num;
}
cout << endl;
}
void main()
{
char pd;
NameTel student;
do
{
student.InputInformation ();
cout << "是否继续?y or n" << endl;
fflush(stdin);
cin >> pd;
}while (pd == 'y'); //问题2:做多次输入时,输入学生信息就有问题 ?
student.ModifyInformation ();
student.Display ();
}
问题1:中间有两个请输入要修改的原始姓名,但是却只能做一个输入
问题2:多次输入时,”请输入学生信息“的地方就只能输入号码了
/
------解决方案--------------------
do
{
student.InputInformation ();
cout << "是否继续?y or n" << endl;
fflush(stdin);
cin >> pd; //也许是这里遗留下回车符的问题 下面加个getchar();试试
}while (pd == 'y');
------解决方案--------------------
由于输入y按换行而产生的一个换行字符
do
{
student.InputInformation ();
cout << "是否继续?y or n" << endl;
fflush(stdin);
cin >> pd;
getchar(); //新添加的一行
}while (pd == 'y');
这么改两个问题都可以解决了,其实两个问题是一个问题
------解决方案--------------------
可以cin>>pd后调用cin.clear()清除缓存,这个方便些!至于你后面一个问题,你可以将Person这个类定义为虚基类,将老师和学生共同的信息函数定义为virtual,然后定义一个类,包含一个基类指针数组Person** pPerson;
在构造函数中pPerson = new Person*[N];这样就可以通过调用pPerson[0]->TeaMessage(); pPerson[0]->StuMessage();思路就是这样的。。。