如果回答超过1,为什么不作出反应
if the reply is more than 1 why do not react
如果有多个答案键,为什么不工作:
无法使用密钥
如果(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类型,因此只能存储一个字符。
- 您可以创建字符串的对象,然后直接进行比较。将
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){...}
相关文章:
- 为什么不;名字在地图上是按顺序排列的吗
- 为什么不能修改对象中的值?另外,我如何改进此链表?
- 为什么不调用移动构造函数?(默认情况下只有构造器,没有别的)
- C++ 基本 CTOR 说明 - 为什么不调用赋值/复制构造函数
- 在 C++ 中声明 const 对象需要用户定义的默认构造函数.如果我有一个可变成员变量,为什么不呢?
- C++:如果括号为空,为什么不抛出错误对象?
- 在C++中,如果"int a = 3; int* p = &a;",那么为什么不允许"const int* &pp = p",但允许"const int* const &pp = p"?
- 如果make_shared/make_unique可以投掷bad_alloc,为什么不为它设置一个尝试捕捉块?
- 如果有条件使用String.length()的条件,为什么不一致地评估这一点
- 如果明确给出多维数组,为什么不可能char [] [] [] = {..},{..}}
- 如果(a)b=c,d=e,return;,为什么不编译
- 如果操作符delete没有实现,为什么不编译它
- 如果存在非常量限定的私有方法,为什么不能在非常量对象上调用常量限定的方法
- 如果回答超过1,为什么不作出反应
- 如果引发的异常始终是异常对象的副本,为什么不调用此复制构造函数?
- 如果函数参数类型是ABC,为什么不能按值传递?
- 如果std::endl是一个函数,为什么不使用括号?
- msvcr80.dll有真正的64位版本吗?如果没有,为什么不呢
- C++11是否添加了C99限制说明符?如果没有,为什么不呢
- 在我的C++代码中,如果我在循环外打印sum,它会给出正确的答案,但在任何循环中都不会,为什么不呢