小编Gir*_*are的帖子

如何在交换机情况下消除goto的使用

基本上我想接受来自用户的特定字符,然后使用switch case将它与该字符大小写相关的字符串传递给另一个函数.例如.

case i:strcpy(str,"ice-cream");
    other_function(str);
    break;
Run Code Online (Sandbox Code Playgroud)

如果用户打印了任何默认字符,那么它应该打印默认语句并再次从用户获取字符并检查其大小写.我这样做了goto,但是goto在此代码中是否还有其他选项可以避免或替换.

p:  
    {
        cout<<"Choose account type:-\n"<<"Enter\ns :-saving\nc :-current\nf :-fixed\nr :-recurring"<<endl;
        char c;
        cin>>c;
        switch(c)
        {
            char t[20];
        case 's':
             strcpy(t,"saving");
             a[i].setype(t);
             break;
        case 'c':
             strcpy(t,"current");
             a[i].setype(t);
             break;
         case 'f':
             strcpy(t,"fixed");
             a[i].setype(t);
             break;
         case 'r':
             strcpy(t,"reccurring");
             a[i].setype(t);
             break;
         default:
             cout<<"Enter valid account type"<<endl;
             goto p;
         }
     }
Run Code Online (Sandbox Code Playgroud)

c++

3
推荐指数
2
解决办法
197
查看次数

标签 统计

c++ ×1