我需要编写一个代码,从用户那里获取输入(句子)并停止在@处

I need to write a code that takes input(sentences) from user and stops at @

本文关键字:句子 输入 那里 获取 用户 代码 一个      更新时间:2023-10-16

正如我所说,我需要将此代码编写为我的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.