新手求助一个C++的小问题,怎么也没搞懂

NewUserFF 2011-01-01 09:48:25
别看你下面的代码很长,其实需要检验的只是很小的一部分,程序运行后,输入2,原理上应该显示“您还没有图书,请创建或添加图书,按回车键继续”,然后程序会等我按回车键,可是现在,输入2后,上面的哪一行字会一闪而过,为什么?有人说是输入选项后有个回车没吸收,我觉得应该不是这么回事吧,cin默认不是带回车一块儿吸收了吗?我把cin.get ( )改为cin.get(a),发现a的值为10,这是什么?10是回车吗?小弟是小白一个,正在学C++,高手帮帮忙啊,十分感谢!

#include <iostream>
#include <string>
using namespace std ;
//定义类book
class book
{
public :
int num ;
double price ;
book* next ;
} ;
//声明全局变量
book* head = NULL ;
//声明函数
void creat (book* head) ;
void display (book* head) ;
void insert (book* head) ;
void del (book *head) ;
char dis_func ( ) ;
void choi (char choice) ;
int number (book *head) ;
bool check ( ) ;
//主函数main ( )
int main ( )
{
char choice = dis_func ( ) ;
choi (choice) ;
}
//函数定义
char dis_func ( )
{
char choice ;
system ("cls") ;
cout << "1.重建图书 2.显示图书 3.插入图书 4.删除图书 5.图书数目 q.退出" << endl ;
cout << "请输入你的选择:" ;
cin >> choice ;
return choice ;
}
void choi (char choice)
{
switch (choice)
{
case '1' :creat (head) ;
break ;
case '2' :display (head) ;
break ;
case '3' :insert (head) ;
break ;
case '4' :del (head) ;
break ;
case '5' :cout << "图书数目:" << number (head) << endl ;
choi(dis_func ( ) ) ;
break ;
case 'q' :exit (0) ;
break ;
default :cout << "请输入正确的选项!" << endl ;
cout << "请输入选项:" ;
cin >> choice ;
choi (choice) ;
}
}
bool check (string num)
{
for (int i = 0 ;i < num.length ( ) ;i ++)
if ( (num.c_str ( ) [i] < '0' || num.c_str ( ) [i] > '9') && (num.c_str ( ) [i] != '.') )
return false ;
return true ;
}
void creat (book* head)
{
}
void display (book* head)
{
if (!head)
{
cout << "你还没有图书,请先创建或添加图书!按回车键继续" << endl ;
cin.get ( ) ;
choi(dis_func ( )) ;
}
else
while (head)
{
cout << "编号:" << head -> num << " 价钱:" << head -> price << endl ;
head = head -> next ;
}
cout << "按回车键继续......" ;
cin.get ( ) ;
choi(dis_func ( )) ;
}
void insert (book* head)
{
}
void del (book *head)
{
}
int number (book *head)
{
return 1;
}
...全文
89 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
無_1024 2011-01-01
  • 打赏
  • 举报
回复

#include <iostream>
#include <string>
#include <cstdlib>
using namespace std ;
//定义类book
class book
{
public :
int num ;
double price ;
book* next ;
} ;
//声明全局变量
book* head = NULL ;
//声明函数
void creat (book* head) ;
void display (book* head) ;
void insert (book* head) ;
void del (book *head) ;
char dis_func ( ) ;
void choi (char choice);
int number (book *head) ;
bool check ( ) ;
//主函数main ( )
int main ( )
{
char choice = dis_func ( ) ;
cout << choice;
choi (choice) ;
}
//函数定义
char dis_func ( )
{
char choice ;
system ("cls") ;//------------------他把你的结果清屏了
cout << "1.重建图书 2.显示图书 3.插入图书 4.删除图书 5.图书数目 q.退出" << endl ;
cout << "请输入你的选择:" ;
cin >> choice;
cin.ignore(1024,'\n');
return choice;
}
void choi (char choice)
{
switch (choice)
{
case '1' :creat (head) ;
break ;
case '2' :display (head);
system("pause");
break ;
case '3' :insert (head) ;
break ;
case '4' :del (head) ;
break ;
case '5' :cout << "图书数目:" << number (head) << endl ;
choi(dis_func ( ) ) ;
break ;
case 'q' :exit (0) ;
break ;
default :
cout << "请输入正确的选项!" << endl ;
cout << "请输入选项:" ;
cin >> choice ;
cin.ignore(1024,'\n');
choi (choice) ;
break;
}
}
bool check (string num)
{
for (int i = 0 ;i < num.length ( ) ;i ++)
if ( (num.c_str ( ) [i] < '0' || num.c_str ( ) [i] > '9') && (num.c_str ( ) [i] != '.') )
return false ;
return true ;
}
void creat (book* head)
{}
void display (book* head)
{
if (!head)
{
cin.ignore(1024,'\n');//-------清楚缓冲区的状态
cout << "你还没有图书,请先创建或添加图书!按回车键继续" << endl ;
cin.get ( ) ;//-----这个就是吸收回车的
choi(dis_func ( )) ;
}
else
while (head)
{
cout << "编号:" << head -> num << " 价钱:" << head -> price << endl ;
head = head -> next ;
}
cin.ignore(1024,'\n');//-------清楚缓冲区的状态
cout << "按回车键继续......" ;
cin.get ( ) ;
choi(dis_func ( )) ;
}
void insert (book* head)
{
}
void del (book *head)
{
}
int number (book *head)
{
return 1;
}
無_1024 2011-01-01
  • 打赏
  • 举报
回复
另外的方法就是在你每次cin.get()之前用一个cin.ignore(1024,'\n');清掉缓冲区的东西
这样就可以避免当你输入 23的时候会读取多次
楼上的做法只是用于缓冲区存在一个字符的时候
JiangXiang 2011-01-01
  • 打赏
  • 举报
回复
只需在cin()前清理缓冲区就行了

#include <iostream>
#include <string>
using namespace std ;
//定义类book
class book
{
public :
int num ;
double price ;
book* next ;
} ;
//声明全局变量
book* head = NULL ;
//声明函数
void creat (book* head) ;
void display (book* head) ;
void insert (book* head) ;
void del (book *head) ;
char dis_func ( ) ;
void choi (char choice) ;
int number (book *head) ;
bool check ( ) ;
//主函数main ( )
int main ( )
{
char choice = dis_func ( ) ;
choi (choice) ;
}
//函数定义
char dis_func ( )
{
char choice ;
system ("cls") ;
cout << "1.重建图书 2.显示图书 3.插入图书 4.删除图书 5.图书数目 q.退出" << endl ;
cout << "请输入你的选择:" ;
cin >> choice ;
return choice ;
}
void choi (char choice)
{
switch (choice)
{
case '1' :creat (head) ;
break ;
case '2' :display (head) ;
break ;
case '3' :insert (head) ;
break ;
case '4' :del (head) ;
break ;
case '5' :cout << "图书数目:" << number (head) << endl ;
choi(dis_func ( ) ) ;
break ;
case 'q' :exit (0) ;
break ;
default :cout << "请输入正确的选项!" << endl ;
cout << "请输入选项:" ;
cin >> choice ;
choi (choice) ;
}
}
bool check (string num)
{
for (int i = 0 ;i < num.length ( ) ;i ++)
if ( (num.c_str ( ) [i] < '0' || num.c_str ( ) [i] > '9') && (num.c_str ( ) [i] != '.') )
return false ;
return true ;
}
void creat (book* head)
{
}
void display (book* head)
{
if (!head)
{
cout << "你还没有图书,请先创建或添加图书!按回车键继续" << endl ;

fflush(stdin);//只需在cin()前清理缓冲区就行了

cin.get ( ) ;
choi(dis_func ( )) ;
}
else
while (head)
{
cout << "编号:" << head -> num << " 价钱:" << head -> price << endl ;
head = head -> next ;
}
cout << "按回车键继续......" ;
cin.get ( ) ;
choi(dis_func ( )) ;
}
void insert (book* head)
{
}
void del (book *head)
{
}
int number (book *head)
{
return 1;
}
無_1024 2011-01-01
  • 打赏
  • 举报
回复

char dis_func ( )
{
char choice ;
system ("cls") ;//------------------他把你的结果清屏了
cout << "1.重建图书 2.显示图书 3.插入图书 4.删除图书 5.图书数目 q.退出" << endl ;
cout << "请输入你的选择:" ;
cin >> choice;
cin.ignore(1024,'\n');//另外一种修改的方法就是在你输入的时候就清理掉 '\n'
return choice;
}
無_1024 2011-01-01
  • 打赏
  • 举报
回复
cin.get()是从缓冲区读取一个字符 当前缓冲去还有一个字符

if (!head)
{
cin.ignore(1024,'\n');//-------清楚缓冲区的状态
cout << "你还没有图书,请先创建或添加图书!按回车键继续" << endl ;
cin.get ( ) ;//-----这个就是吸收回车的
//这是我一般的做法
無_1024 2011-01-01
  • 打赏
  • 举报
回复
嗯是的
也是啊 没看到你的那个的作用
return_torres 2011-01-01
  • 打赏
  • 举报
回复
回楼上,楼主确实目的要清屏,但是是在按下回车后清屏
無_1024 2011-01-01
  • 打赏
  • 举报
回复

char dis_func ( )
{
char choice ;
system ("cls") ;//------------------他把你的结果清屏了
cout << "1.重建图书 2.显示图书 3.插入图书 4.删除图书 5.图书数目 q.退出" << endl ;
cout << "请输入你的选择:" ;
cin >> choice;
return choice;
}
return_torres 2011-01-01
  • 打赏
  • 举报
回复

void display (book* head)
{
if (!head)
{
cout << "你还没有图书,请先创建或添加图书!按回车键继续" << endl ;
cin.get ( ) ;//第一个接受到实际是最初漏下去的那个回车符
cin.get(); //这个才停住
choi(dis_func ( )) ;
}


無_1024 2011-01-01
  • 打赏
  • 举报
回复
因为你的那个清屏函数知道不
return_torres 2011-01-01
  • 打赏
  • 举报
回复
确实是因为回车符被漏下去了,导致后面那个cin.get()吸收的是回车符,直接跳过去了。
LZ你不信用连续2个cin.get()就会看得见了。
hxs91 2011-01-01
  • 打赏
  • 举报
回复
我不知道是不是这个原因,但是有可能。我说一下。
当你输入回车的时候 实际上是两个字符:换行以及回车 但是 cin只是读一个字符 也就是当你按一下回车后 你的输入流里边实际上是有两个字符的,你认为在次cin后这两个字符都被提取了,实际上并不是。当你下一次输入回车的时候,前边还有一个你上次的字符,所以会有微妙的错误。

64,683

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧