我需要 c++ 方面的帮助
I need help in c++
im 创建我喜欢称之为复杂 hello 世界的东西。 我对编码非常陌生,更不用说C ++了,为了开始,我创建了一个Hello World。 我现在知道如何轻松地做到这一点,所以我决定尝试做一些更复杂的事情。我希望我的程序首先询问"你想看你好世界吗?"然后根据用户输入"是"或"否",它将响应"hello world"或关闭程序。我以为我可以为此使用布尔值,但我卡住了。我需要知道如何创建一个代码来读取用户键入的内容,例如"yes",然后输出hello world。
喜欢:
if (the user"s answer) = yes cout << "Hello world!" << endl;
您需要
使用类似 std::cin
的东西来接受用户输入。使用类似std::string
容器的东西来保存字母答案,即是或y。然后对照控件检查输入并有条件地打印 Hello World。
#include <iostream>
#include <string>
int main()
{
std::string ans;
std::cout << "Would you like to see the "Hello World"?n-> ";
std::cin >> ans;
if (ans == "yes" || ans == "y")
{
std::cout << "nHello World!" << std::endl;
}
return 0;
}
我们可以通过使用 c++ 提供的iostream
来做到这一点。有两种方法可以做到这一点,使用 scanf
或 cin
作为输入,但为了简单起见,我们只使用 cin
.
#include <iostream>
using namespace std;
int main() {
cout<<"Would you like to see the hello world?n";
string answer;
cin>>answer;
if(answer=="Yes") cout<<"Hello, world!";
}
说明:我们使用 cout
提示用户,然后使用 string,我们检查输入是否等于 Yes
,如果是,则输出Hello, world!
否则,代码将终止。您看到的n
是用于制作新行,只是为了更清晰的格式。
相关文章:
- 需要帮助设置在C++中使用的Potrace
- 在指针的帮助下,文本文件中单词的频率
- 计算每个节点的树高,帮助我解释这个代码解决方案
- 如何在Qbutton的帮助下更改Q对话框的宽度
- 需要帮助将结构数组传递给函数
- GCC和Clang在与__builtin_constant_p相关的static_assert方面有所不同
- C++需要帮助从用户那里获得一个整数,并确保它在另外两个整数之间
- 需要帮助在 c++ 中将字符串转换为字符 ----错误 "const char *" 类型的值不能用于初始化 "char" 类型的实体
- 有人可以帮助我处理正则表达式吗?
- C++调用具有 *this 属性的单个帮助程序函数
- C++:需要帮助了解运算符重载错误
- 我需要 c++ 结构方面的帮助
- 我需要 c++ 结构方面的帮助
- 我需要 c++ 方面的帮助
- 需要 C++ 模板动态内存分配方面的帮助
- 在结构指针方面需要帮助
- Qt Creator 5.3在理解帮助文档方面需要帮助
- 在获取CPU频率方面需要一些帮助
- 需要c++代码方面的帮助
- 我需要一些货币计划方面的帮助