Cout的字符串是给错误和困难的时候一些洞察力帮助pls? [英] Cout of a string is giving an error and a hard time some insight help pls?
本文介绍了Cout的字符串是给错误和困难的时候一些洞察力帮助pls?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我无法找到这段代码中的错误,任何人都可以洞察我吗?我运行调试,但错误是不可理解的。
I cant find the error in this piece of code could anyone please insight me? I ran the debugging but the errors are un-understandable..
#include "stdafx.h"
#include <iostream>
using namespace std;
int main()
{
string name;
cout << "Input your name please?" << endl;
cin >> name;
if
{
(name == "Bart Simpson")
cout << "You have been very naughty" << endl;
}
return 0;
}
推荐答案
问题:
- 您有一些缺少的
#include
,这可能导致您的初始编译器错误。 - 您的
如果
语句有一个简单的语法错误。 - 使用流提取运算符绝不会生成一个包含空格的字符串。
- You have some missing
#include
s, which probably caused your initial compiler errors. - You have a simple syntax error with your
if
statement. - Using the stream extraction operator will never yield a string with whitespace inside of it.
以下内容应符合您的期望:
The following should work as you expect:
#include "stdafx.h"
#include <iostream>
#include <ostream>
#include <string>
using namespace std;
int main()
{
cout << "Input your name please?" << endl;
string name;
getline(cin, name);
if (name == "Bart Simpson")
{
cout << "You have been very naughty" << endl;
}
return 0;
}
(您需要包含 string
std :: string
和 std :: getline
和 ostream
std :: endl
。)
(You need to include string
for std::string
and std::getline
, and ostream
for std::endl
.)
这篇关于Cout的字符串是给错误和困难的时候一些洞察力帮助pls?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文