如何在用户提示时重复节目 y/n

How to repeat a program when user prompted y/n

这个程序应该加密和解密字符串“myWord”。收到后,通过两个方法加解密,然后returns到main。我一直在尝试让它提示用户是否要继续。如果他们输入“n”,则循环停止,程序结束。如果他们输入“y”,那么它将重复,并且在再次提示他们输入他们的名字后,他们将能够再次加密。我尝试了很多不同的方法,但在我输入 y:

后,输出继续像这样结束
#include <iostream>
#include <string>

using namespace std;

void encrypt(string);
void decrypt(string);

int main() {
    string myWord;

    char choice = 'y';
    while (choice == 'y') {
        cout << "Enter a name: ";
        getline(cin, myWord);
        encrypt(myWord);
        cout << "Would you like to encrypt again? (y/n): ";
        cin >> choice;
        if(choice == 'n') {
            return 0;
        }
        system("pause");
    }
    return 0;
}

void encrypt(string encrypting) { 
    for (int i = 0; encrypting[i] != '[=11=]'; i++) {
        encrypting[i] = encrypting[i] + 1;
    }
    cout <<"Encrypted: " << encrypting << endl;
    decrypt(encrypting);
}

void decrypt(string decrypting) {
    for (int i = 0; decrypting[i] != '[=11=]'; i++) {
        decrypting[i] = decrypting[i] - 1;
    }
    cout << "Decrypted: " << decrypting << endl;
    return;
}

我的输出

您的问题是在以空格分隔的输入和面向行的输入之间切换。由于 std::cin'\n' 留在流中,下一次对 std::getline 的调用会看到并跳过。

您需要跳到流的末尾:

有关详细信息,请参阅 https://en.cppreference.com/w/cpp/string/basic_string/getline

这应该可以解决您的问题:

#include <limits>
#include <ios>

//...

cout << "Enter a name: ";
getline(cin, myWord);
encrypt(myWord);
cout << "Would you like to encrypt again? (y/n): ";
cin >> choice;
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

混合格式化输入(例如 std::cin >> x)和无格式输入(例如 std::getline)很难正确处理。

在您的示例中,cin >> choice 行将从流中提取一个字符(忽略前导空格)。流中的下一个字符(假设您键入 y 然后输入)将是换行符 \n.

例如,您的输入序列可能如下所示 name\ny\nanother_name

第一个 getline 调用提取 name\n,给你字符串 name 并丢弃 \n(它仍然被提取,只是没有添加到字符串),离开 y\nanother_name.

然后cin >> choice提取y,留下\nanother_name

然后你再次调用 getline,它查看流的开头,看到 \n,并认为它得到一个空行。所以它提取 \n 并丢弃它......你希望它读取 another_name,但它读取一个空行。

最简单的解决方案是在调用 std::getline 之前执行 cin >> std::ws。这从输入流中提取所有前导空格并丢弃它们(包括虚假的空行)

在您的示例中,它将是:

cout << "Would you like to encrypt again? (y/n): ";
cin >> choice;     
cin >> std::ws; // Note the added std::ws here
if(choice == 'n') {
    return 0;
}

好吧,正如这里的人告诉你的那样,混合使用 charstringsgetline()cin 东西是不安全的...

您可能会发现继续使用 C++ 方式并仅使用 getline()string 更简单

例子

#include <iostream>
#include <string>

using namespace std;

void decrypt(string);
void encrypt(string);

int main(void)
{
    string  choice;
    do
    {
        string  myWord; // only exists here
        cout << "Enter a name: ";
        getline( cin, myWord );
        encrypt(myWord);
        cout << "Would you like to encrypt again? (y/n): ";
        getline( cin, choice );
        
    }   while (choice == "y");
    return 0;
}

void encrypt(string encrypting) { 
    for (int i = 0; encrypting[i] != '[=10=]'; i++) {
        encrypting[i] = encrypting[i] + 1;
    }
    cout <<"Encrypted: " << encrypting << endl;
    decrypt(encrypting);
}

void decrypt(string decrypting) {
    for (int i = 0; decrypting[i] != '[=10=]'; i++) {
        decrypting[i] = decrypting[i] - 1;
    }
    cout << "Decrypted: " << decrypting << endl;
    return;
}

这表明

PS C:\test> g++ -o x -Wall x.cpp
PS C:\test> ./x  
Enter a name: A test
Encrypted: B!uftu
Decrypted: A test
Would you like to encrypt again? (y/n): y
Enter a name: Another test
Encrypted: Bopuifs!uftu
Decrypted: Another test
Would you like to encrypt again? (y/n): y
Enter a name: Last One
Encrypted: Mbtu!Pof
Decrypted: Last One
Would you like to encrypt again? (y/n): n
PS C:\test>