每日一个C++小程序(六)--基本菜单程序

来源:互联网 发布:燃气灶烤箱一体机 知乎 编辑:程序博客网 时间:2024/06/05 17:34
第一种方案:使用if……else和break,continue控制程序流程
#include<iostream>#include<cstdlib>using namespace std;int main(){     char choose,ch;     while(1)     {        cout<<"Menu: A(dd) D(elete) S(ort) Q(uit),Select one: ";        cin>>ch;        choose=toupper(ch);        if(choose=='A')        {           cout<<"数据已被增加!"<<endl;           continue;        }        else if(choose=='D')        {           cout<<"数据已被减少!"<<endl;           continue;        }        else if(choose=='S')        {           cout<<"数据已经排序!"<<endl;           continue;        }        else if(choose=='Q')           break;      }      return(1);}


第二种方案:使用switch语句

#include<iostream>#include<cstdlib>using namespace std;int main(){     char choose;     while(1)     {        cout<<"Menu: A(dd) D(elete) S(ort) Q(uit),Select one: ";        cin>>choose;        switch(toupper(choose))        {           case 'A':                    cout<<"数据已被增加!"<<endl;                    break;           case 'D':                    cout<<"数据已被删除!"<<endl;                    break;           case 'S':                    cout<<"数据已经排序!"<<endl;                    break;           case 'Q':                    exit(0);                    break;           default:                    cout<<"选择错误,请重新选择!"<<endl;        }     }     return(0);}

—————————————————————————————————

本文原创自Sliencecsdn技术博客。

本博客所有原创文章请以链接形式注明出处。

欢迎关注本技术博客,本博客的文章会不定期更新。


大多数人想要改造这个世界,但却罕有人想改造自己。

世上没有绝望的处境,只有对处境绝望的人。

                                              ————By slience

—————————————————————————————————


0 0
原创粉丝点击