我在ILIF和C++中继续遇到问题
问:每次我运行程序并输入“你是谁”时,都会显示a的错误值与发生的情况不匹配?是的,我是C++的NOOB 代码:我在ILIF和C++中继续遇到问题,c++,C++,问:每次我运行程序并输入“你是谁”时,都会显示a的错误值与发生的情况不匹配?是的,我是C++的NOOB 代码: 流和字符串的运算符>>输入由空格分隔的单词。您应该使用一种功能,可以一次读取多个单词,直到按下Enter键为止。例如,您可以使用标准函数std::getline 还需要包括标题 给你 #include <iostream> #include <string> int main() { std::string s; if ( std::get
流和字符串的运算符>>输入由空格分隔的单词。您应该使用一种功能,可以一次读取多个单词,直到按下Enter键为止。例如,您可以使用标准函数std::getline 还需要包括标题 给你
#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;
}
cin>>a只能读一个单词。它怎么能等于你是谁?使用std::getLine读取字符串如果你尝试打印一个字符串,你会立即看到问题。
#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;
}