我需要编写一个代码,从用户那里获取输入(句子)并停止在@处
I need to write a code that takes input(sentences) from user and stops at @
正如我所说,我需要将此代码编写为我的hw,但是我需要将其写入while循环中,因为我们不知道用户将写多大的句子或他们将写多少句子。它将是一个段落
string word;
string parag;
while (cin >> word)
{
parag += word;
for ( unsigned int k = 0; k < parag.length(); k++ )
{
if (parag.at(k) == '@')
break;
}
}
我知道这里有问题,但即使我写"@"它也不会停止。我不知道该怎么办,我只是一个初学者。
do
{
parag = "";
cin >> word;
while (word != "@")
{
parag += word + " ";
cin >> word;
}
parag = parag.substr (0, parag.length()-1); //Takes all characters but "@" at the end
ToLower(parag); //This code is in header file that our teacher gave us. Makes all characters lower case in order to make our code "case insensitive".
}while ( ( CheckInput(parag) ) ); // This checks inputs(obviously) if inputs are correctly entered.
这是我在上课并学习 do-while 循环后写的,如果有人感兴趣的话。程序将花费无限的"cin"来创建段落。关于为什么getline(cin,parag(不起作用用户可以写这样的东西
"玫瑰是红色的
紫罗兰是蓝色的。 @">
如您所见,句子不在同一行中,getline 仅将一行作为输入。最好的部分是(请不要判断我是初学者(如果输入错误,我可以说"do"这样的话。"请再次输入您的输入",我可以在不关闭程序的情况下接受输入,直到所有输入都正确。
休息我的作业是关于从用户那里获取段落。将段落划分为句子作为用户输入(带有查找点(。反转所有句子,要求用户编写反转句子并将这些输入与程序进行比较,以查看用户是否正确给出反转句子,如果没有,则说用户犯了多少错误。
我不明白为什么你需要在读取@字符时退出程序......但这就是你读句子的方式,希望这有帮助。
#include <iostream>
#include <string>
using std::cin;
using std::cout;
using std::string;
using std::getline;
//You could do using namespace std, but that uses a lot more methods and functions
//that you don't need.
int main(int argc, char* argv[])
{
string parag;
cout << "Enter your sentence here: ";
getline(cin, parag); //Get the input until the user enters a newline.
//usually via pressing enter.
cout << "nYou entered: " << parag; //The newline escape sequence is n
cin.ignore(); //Pause the program until newline is received.
}
示例输出
$ ./OutputParag.exe
Enter your sentence here: My input string is a sentence.
You entered: My input string is a sentence.
相关文章:
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- 2D数组来自文本输入,中间有空格
- 如何使用 < 和 > 命令获取 c++ 中的输入和输出?
- 检查输入是否不是整数或数字
- 正在尝试了解输入验证循环
- 读取文件并输入到矢量中
- 在输入句子时终止 std::out_of_range
- 如何输入每个单词在字符串数组中的句子,直到用户按Enter键
- 在 c++ 中获取多行句子输入
- 我需要编写一个代码,从用户那里获取输入(句子)并停止在@处
- 在C++中输入一个句子
- 如何从包含多个句子的输入文件中表达单个单词.C
- 从输入的句子中构建单词列表
- 根据这些字符串输入在句子中的位置对这些字符串输入进行排序的更快方法是什么
- 将.txt文件中的完整句子输入到数组中
- 在cpp中,如何使用getline()在没有交互式输入的情况下获得完整的句子
- 输入句子并将其转换为字符串
- 分解输入句子
- 如何将输入的句子中的单词放入数组中?C++
- 如何在c++中与用户输入交换整个句子的字符