使用If语句中的结果执行交换机案例?

时间:2018-05-14 18:00:19

标签: c++ if-statement switch-statement

我不确定这是否有效。我假设它应该,我只是无法弄清楚如何。

我正在尝试从If语句的结果中执行Switch案例。 例如,如果If的结果为1,则第一个案例执行

这就是我的想法:

#include <iostream>
#include <ctype.h>
using namespace std;

int main(){
    cout<<"\tDetermine what has been inputted"<<endl<<endl;
    char value,ch;
    int option;

    do{

        cout<<"Input something: ";
        value = islower(value);
        cin>>value;
        if(value>='0' && value<='0'){
            option == 1;
        }
        else if (value=='a' || value=='e' || value=='i' ||value=='o' || 
value=='u') {
            option ==2;
        }

    switch(option){
            case 1:
                cout<<"You entered "<<value <<" a digit."<<endl;

                cout<<"Do you wish to test again? (Y/N): "<<endl;

                cout<<"Do you wish to test again? (Y/N): "<<endl;
                cin>>ch;
                break;
        case 2:
               cout<<"You have entered"<<value<<" a vowel"<<endl;

               cout<<"Do you wish to test again? (Y/N): "<<endl;
               cin>>ch;
               break;
       default:
             cout<<"That input is not valid"<<endl;
      }


   } while (ch == 'y' || ch =='Y');


    return 0;
}

我在想什么?

1 个答案:

答案 0 :(得分:1)

初始化变量以避免UB:

char value,ch = 0;
int option = 0;

仅当value为'0'时才是这样:

if(value>='0' && value<='0')

您有两个错别字:==而不是=

option = 1;

option = 2;

如果输出无效,您可能想将ch设置为'Y'?

   default:
         cout<<"That input is not valid"<<endl;