c++ cin一个整数 输入其它字符产生死循环错误的解决方法。
来源:互联网 发布:java的多态 编辑:程序博客网 时间:2024/06/05 10:47
#include <iostream>#include <string>using namespace std;typedef struct node{ string str; node* next;}node;node *top = NULL; void create(){if (top != NULL) {cout << "stack is existing" << endl;return;} top=new node; top->str="HeadNode"; top->next=NULL; cout<<"stack intialize successfully"<<endl;}bool isempty(){if (top == NULL){cout << "stack is not existing,please create one" << endl;return true;} if(top->next==NULL) { cout<<"stack is empty"<<endl; return true; } cout<<"not empty"<<endl; return false;}void push(string ele){if (top == NULL){cout << "stack is not existing,please create one" << endl;return;} node *tmp=new node; tmp->next=top->next; tmp->str=ele; top->next=tmp;}string pop(){if (top == NULL){cout << "stack is not existing,please create one" << endl;return "";} if(isempty()) { return ""; }node *pItem = top->next; string tmp=pItem->str; top->next=pItem->next;delete pItem; return tmp;}void print(node*t){if (top == NULL){cout << "stack is not existing,please create one" << endl;return;} while(t!=NULL) { cout<<t->str<<endl; t=t->next; }} int main(){ int n = -1; string ele="";cout << "1: create" << endl;cout << "2: isempty" << endl;cout << "3: push a element" << endl;cout << "4: pop a element" << endl;cout << "5: print all element" << endl; while(true) { ele="";cout << "Please enter the command number: "; cin>>n; if (cin.fail())// when you enter a English character,it will appear endless loop,Code below will get it right!{cin.sync();cin.clear(); n = -1;cout<<"illegal charactor"<<endl;continue;} switch(n) { case 1: create(); break; case 2: isempty(); break; case 3:if (top == NULL){cout << "stack is not existing,please create one" << endl;break;}cout << "Please enter a string: "; cin>>ele; push(ele); break; case 4: ele=pop(); if(ele.length()==0) break; cout<<"pop "<<ele<<endl; break; case 5: print(top); break; case 0: return 0; default: cout<<"illegal charactor"<<endl; break; } } // while(true)while(!top) //when the application exit,the memory we allocated will return to system,seem we don't need free memory.{ //but in order to develop good habits use delete correspond to new.node *tmp = top;top = top->next;delete tmp;} return 0;}
0 0
- c++ cin一个整数 输入其它字符产生死循环错误的解决方法。
- 用 cin 输入整数死循环问题
- 解决cin因为输入错误的数据类型进入死循环的问题
- C++—— 恢复状态标志和清理缓存(以标准库输入流std::cin输入整数时死循环为例)
- 关于C++中给int变量输入字符引起的死循环解决方法
- C字符串与cin.ignore()一起产生的一个好玩的错误
- 【C++】 cin错误导致的while()死循分析
- scanf和cin输入类型不匹配时造成的死循环问题
- 死循环中的cin
- 读取数字的循环,用cin表达式的值判断错误输入并清除cin流接受新的输入
- <C语言>输入某个大写字母,产生一个金字塔图案。(字符与数字间的转换)
- 关于内存溢出产生的死循环
- cin读取失败 死循环
- 解决int型变量cin输入字符无限循环问题
- cin和cin.get()在输入字符时的区别
- 在循环里面cin>>a输入不合法数据导致死循环
- 解决scanf或者cin造成的死循环问题
- C++ cin.clear()的使用:防止出现死循环
- 4 Median of Two Sorted Arrays
- HDU Moving Tables (贪心)
- 1、标准IO和文件IO说明
- mysql 命令行基本操作
- 合作之心灵洗礼
- c++ cin一个整数 输入其它字符产生死循环错误的解决方法。
- Java学习笔记(九):Map.keyset()
- hackerrank maxsum mod
- Android Scroller简单用法
- HDOJ 1022 Train Problem I
- XML解析类CMarkup的使用方法
- HDU Fire Net (贪心)
- Mysql常用命令
- 冒泡排序—do-while语句实现