如果回答超过1,为什么不作出反应

if the reply is more than 1 why do not react

本文关键字:为什么不 如果      更新时间:2023-10-16

如果有多个答案键,为什么不工作:

无法使用密钥

如果(resp="打开"

但如果我只用一个字母替换它,那么他就可以工作

如果(resp='y'

或将其替换为数字,然后他可以工作

如果(resp=1

我希望她能写不止一封信

#include <iostream>
using namespace std;
int main(){
    char resp;
    index:
    cout<<endl;
    cout<<"just an example of course"<<endl;
    cout<<"type {open} to return to the indexn";
    cin>>resp;
    if (resp == 'open'){
        goto index;
    }
    else if(resp == 2)
        {
        return EXIT_SUCCESS;
    }

}

使用string而不是char

#include <iostream>
#include <string>
using namespace std;
int main(){
    string resp;
index:
    cout<<endl;
    cout<<"just an example of course"<<endl;
    cout<<"type {open} to return to the indexn";
    cin >> resp;
    if (resp == "open"){
        goto index;
    }
    /*
    else if(jawab == 2)
        {
        return EXIT_SUCCESS;
    }
    */
}

resp属于char类型,因此只能存储一个字符。

  1. 您可以创建字符串的对象,然后直接进行比较。将char resp更改为string resp,然后可以使用if(resp=="open")

这是您的编辑代码:

#include <iostream>
using namespace std;
int main(){
    string resp; //Fix 1
    int jawab;
    index:
    cout<<endl;
    cout<<"just an example of course"<<endl;
    cout<<"type {open} to return to the indexn";
    cin>>resp;
    if (resp =="open"){ //Fix 2
        goto index;
    }
    else if(jawab == 2)
    {
        return EXIT_SUCCESS;
    }
}

resp是一个字符,而不是字符数组。此外,如果要测试两个字符串是否相同,请改用strcmp(a,b)==0

此外,您应该使用"open"而不是'open'。单引号用于字符,双引号用于字符串。

我不确定你的要求是什么。如果你想比较两个字符,你可以这样做:

char resp;
if (resp == 'o'){...}

但如果你想比较两个字符串,你应该:

std:string resp;
if (resp.compare("open") == 0){...} 
相关文章: