C++ 如何使用enter键创建新行,而不是输入字符串?
我必须编写一个程序,允许用户输入一首多行诗,按enter键在诗中创建一行新诗。这首诗的所有行都需要存储在一个字符串中,我不知道如何将“\n”连接到用户输入。此外,一旦用户完成了这首诗,我不确定接下来如何继续并在程序中执行进一步的代码 以下是我目前掌握的代码:C++ 如何使用enter键创建新行,而不是输入字符串?,c++,C++,我必须编写一个程序,允许用户输入一首多行诗,按enter键在诗中创建一行新诗。这首诗的所有行都需要存储在一个字符串中,我不知道如何将“\n”连接到用户输入。此外,一旦用户完成了这首诗,我不确定接下来如何继续并在程序中执行进一步的代码 以下是我目前掌握的代码: /* Poetry In Motion; Cortez Phenix This program allows the user to make a poem, and the program contains a poem game. Th
/*
Poetry In Motion; Cortez Phenix
This program allows the user to make a poem, and the program contains a
poem game. The most notable features are loops controlled by the user.
*/
#include <string>
#include <iostream>
using namespace std;
void makePoem()
{
string user_input;
cout << "Enter a poem of your own creation, one line at a time.\n";
cout << "Type 'quit' to exit the program.\n\n";
cout << "Type your poem, pressing the enter key to create a new line.\n\n";
cin >> user_input;
if (user_input == "quit")
{
cout << "\nGoodbye! Have a great day.\n";
}
else
{
getline(cin, user_input);
}
}
int main()
{
makePoem();
return 0;
}
/*
运动中的诗;科尔特斯凤凰酒店
该程序允许用户制作一首诗,该程序包含
诗歌游戏。最显著的特性是由用户控制的循环。
*/
#包括
#包括
使用名称空间std;
void makePoem()
{
字符串用户输入;
cout这看起来像是一个家庭作业问题,所以我不会给你实际的代码,但这里有一些线索:
- 你需要一个循环,直到用户输入“退出”为止
- 然后,只需使用+=运算符将用户输入添加到
user\u input
字符串中
- 要向字符串添加换行符,请添加
“\n”
ex:user\u input+=“\n”
您需要在循环中读取用户的输入,将每一行附加到目标字符串中,例如:
/*
运动中的诗;科尔特斯·菲尼克斯
该程序允许用户制作一首诗,该程序包含
诗歌游戏。最显著的特点是由用户控制的循环。
*/
#包括
#包括
使用名称空间std;
void makePoem()
{
弦乐诗;
字符串用户输入;
您是否可以保留一个名为poem
的std::string
,并在用户每次输入一行时在末尾追加user\u input
。