如果变量C++为真或假
if cin true or false from variable C++
请帮我解决这个问题。
int A=10;
cout<<"Answer this Question!";
cout<<"5 + 5 = ";cin>>A;
if(true) {
cout<<"Correct!"<<endl;
} else if (false) {
cout<<"Wrong!"<<endl;
}
getch();
}
的答案是A=10,如果用户从int A=10
输入正确的答案,则通知使用cout"正确!",如果错误是"错误!".
在上面的代码中,
如果(真的)
总是求值为true,所以它总是输出"Correct!"然而,我猜你是在问我们如何检查用户的输入,看看他们是否正确?在这种情况下,对于if语句,您需要
if (A == 10)
而不是else if,因为除了10以外的任何答案都是不正确的。
我建议你查一下编程基础知识。
您可以使用两个独立的变量,一个用于用户的答案,另一个用于正确答案,然后比较它们:
int correctAnswer = 10;
int userAnswer = 0;
cout << "Answer this Question!";
cout << "5 + 5 = ";
cin >> userAnswer;
if (correctAnswer == userAnswer) {
cout << "Correct!" << endl;
} else {
cout << "Wrong!" << endl;
}
getch();
int main(){
int A = 0;
std::cout<<"Answer this question: 5 + 5 = ";
std::cin>>A;
if(A == 10){
std::cout<<"Correct"<<std::endl;
}else{
std::cout<<"Wrong"<<std::endl;
}
return 0;
}
使用如下代码:)
//...
int B;
cin>>B;
if( A == B ) {
cout<<"Correct!"<<endl;
} else {
cout<<"Wrong!"<<endl;
}
相关文章:
- 如何创建一个CMake变量,除非显式重写,否则使用默认值
- 将成员变量添加到共享库中的类中,不会破坏二进制兼容性吗
- 将数组的地址分配给变量并删除
- 为"adjacent"变量赋值时出现问题
- enum是C++中的宏变量还是整数变量
- 在全局变量中保存类的实例以重新创建类(创建"backup")
- 用C++中的一个变量定义一个常量
- 具有奇怪重复模板模式的派生类中的成员变量已损坏
- 你能重载对象变量名本身返回的内容吗
- 内置函数可查看CPP中的成员变量
- 是否可以初始化不可复制类型的成员变量(或基类)
- 尝试通过多个向量访问变量时,向量下标超出范围
- 试图让变量检查数组中的某些内容
- Cpp-Tuple使用带有变量的get
- 将包含C样式数组的对象初始化为成员变量(C++)
- 当vector是tje全局变量时,c++中vector的内存管理
- 通过多个头文件使用常量变量
- std::threads可以从Windows DLL中的全局变量创建/销毁吗?
- 执行函数时导致崩溃的变量
- 变量没有改变?通过向量的函数调用