我一直遇到 if elif 和 C++ 中的其他问题
i keep running into problems with if elif and else in c++
问题:每次我运行程序并输入您是谁时,它都说 a 的错误值与正在发生的事情不匹配?(是的,我是 c++ 的菜鸟)法典:
#include <iostream>
using namespace std;
int main () {
// local variable declaration:
string a;
cin >> a;
// check the boolean condition
if( a == "hello" ) {
// if condition is true then print the following
cout << "hi" << endl;
} else if( a == "who are you" ) {
// if else if condition is true
cout << "a better question is who are you?" << endl;
} else if( a == "what am i doing" ) {
// if else if condition is true
cout << "reading this output " << endl;
}else {
// if none of the conditions is true
cout << "Error Value of a is not matching" << endl;
}
return 0;
}
运算符>>流和字符串输入用空格分隔的单词。您应该使用一个可以一次读取几个单词的功能,直到按下 Enter 键。例如,您可以使用标准函数std::getline
您还需要包含标题<string>
。
给你
#include <iostream>
#include <string>
int main()
{
std::string s;
if ( std::getline( std::cin, s ) )
{
// check the boolean condition
if ( s == "hello" )
{
// if condition is true then print the following
std::cout << "hi" << std::endl;
}
else if ( s == "who are you" )
{
// if else if condition is true
std::cout << "a better question is who are you?" << std::endl;
}
else if ( s == "what am i doing" )
{
// if else if condition is true
std::cout << "reading this output " << std::endl;
}
else
{
// if none of the conditions is true
std::cout << "Error Value of a is not matching" << std::endl;
}
}
return 0;
}
相关文章:
- 从其他类访问类时遇到问题
- 程序没有问 3 个问题,而是将我对第一个问题的回答用于其他两个问题
- C++ 继承向量问题(无限循环+在其他类中使用向量的问题)
- 这种比较是否不一致(或者存在其他问题)?
- 如何将char数组声明为函数参数?或告诉我此代码中还有其他问题?
- C++ 将问题包含在其他文件中?(提升包括)
- 相同的代码,不同的结果?从其他项目包含有什么问题?
- C - 如果语句问题.代码将不会运行其他,否则
- 问题在具有依赖性时从其他路径上加载DLL的DLL
- 当对象被其他对象引用时,关于 const 限定符的微妙问题
- Catch2 迫使我在异常中添加对 std::string 的强制转换,这会产生其他问题吗?
- Cout 不是 STD 的成员,以及有关 C++ 的其他问题
- 有关内存对齐的其他问题
- C++constexpr数组查找:内存开销?其他问题
- 我一直遇到 if elif 和 C++ 中的其他问题
- visual C++:简单的运算符优先级问题或其他问题
- 测试用户自定义头文件和其他问题
- 从类中访问结构和实现图形的其他问题
- 处理由于意外库行为导致的段错误和其他问题
- C++流、函数声明和其他问题