新手求助一个C++的小问题,怎么也没搞懂
别看你下面的代码很长,其实需要检验的只是很小的一部分,程序运行后,输入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;
}
#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;
}
...全文
相关推荐
C++ .h和.cpp文件总结
总结了.h文件能做什么,.cpp文件能做什么,总结了.h文件的本质,方便新手在遇到文件包含、类型声明、变量定义时能理清.h与.cpp关系,纯属个人总结文档。
C++项目源代码(适合新手练手及参考)
适合刚涉及C++的新手练手,有源代码(注释详细),答辩PPT。
cpp代码-C++基础
cpp代码-C++基础
33个c++重要知识总结(cpp代码)
c++基础知识复习,包含了33个cpp文件,每个文件包含特定主题的知识回顾,使用特定案例重温某些不常用但是重要的算法或函数的使用方法,含注释。
c++教材—-cpp实训
c++word文档的教材 蛮精简的 里面还有cpp的一些代码 练习 是入门的首选


