如何在 C++ 的 do while 循环中读取空格

How to read white spaces in a do while loop in c++

我正在尝试扫描读取空格的输入 msg。我试过 scanf("%[^\n]s,&msg); 它在我不使用循环时有效。但是当我在 do while 循环中使用它时,scanf("%[^\n]s,&msg); 将不会读取如果我按“1”来执行无限循环。在 do while 循环中是否有任何替代过程来读取带空格的字符串?

#include <stdio.h>
#include <conio.h>
#include <iostream>
#include <string>
using namespace std;

int main()
 {
/* Variable declaration */
char msg[100];
 char countch;
 int key = 0;
 int i = 0;
 int error = 0;

 printf("*** Caesar Cipher ***\n\n");

do{

/* Input Plain Text */
printf("\nEnter Plain Text:");
scanf("%s",&msg);

/* Input key */
printf("Enter Key:");
scanf("%d",&key);

/* Encryption */
/* Traverse Text */
for(i = 0;msg[i] != '[=10=]'; i++)
{
countch = msg[i];

/* apply Encryption lowercase letters */
if(countch >= 'a' && countch <= 'z')
{
    countch = countch + key;

    if(countch > 'z')
    {

    countch = countch - 'z' + 'a' - 1;
    }
    msg[i] = countch;
}
/* apply Encryption Uppercase letters */
else if(countch >= 'A' && countch <= 'Z')
{
    countch = countch + key;

    if(countch > 'Z')
    {
    countch = countch - 'Z' + 'A' - 1;
    }
    msg[i] = countch;
}
}

//printf("\n===================================");
/* print encryption result */
printf("Encrypted Message:%s",msg);


/* Decryption */
for(i = 0;msg[i] != '[=10=]'; i++)
{
countch = msg[i];

/* apply decryption lowercase letters */
if(countch >= 'a' && countch <= 'z')
{
    countch = countch - key;

    if(countch > 'z')
    {

    countch = countch + 'z' - 'a' + 1;
    }
    msg[i] = countch;
}
/* apply decryption Uppercase letters */
else if(countch >= 'A' && countch <= 'Z')
{
    countch = countch - key;

    if(countch > 'Z')
    {
        countch = countch + 'Z' - 'A' + 1;
    }
    msg[i] = countch;
}
}

/* print decryption result */
printf("\nDecrypted Message:%s",msg);
//printf("\n");

printf("\nDo you want to continue '1' or '0':");
scanf("%d",&error);

  }while(error != 0);
 exit(0);


 getch();
 }

when I use it inside a do while loop, the scanf("%[^\n]s,&msg); will not read If I press '1' to execute infinite loop. 

您告诉 scanf() 不要读取换行符,因此它们保留在输入缓冲区中。您永远不会单独读取它们以将它们从缓冲区中删除,这会影响后续读取。

 Is there any alternate process to read the string with spaces in do while loop?

printf()scanf() 是 C 风格的 I/O 函数。您应该改用 C++ 风格的 I/O 流,它们会为您省去很多麻烦:

#include <iostream>
#include <string>
#include <limits>
#include <iomanip>
using namespace std;

bool prompt(const char *prompt, string &value)
{
    cout << prompt << ": ";
    return !getline(cin, value).fail();
}

bool prompt(const char *prompt, int &value)
{
    do
    {
        cout << prompt << ": ";
        if (cin >> value)
            break;

        if (cin.eof())
            return false;

        cout << "Invalid input!\n";
        cin.clear();
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
    }
    while (true);

    cin.ignore(numeric_limits<streamsize>::max(), '\n');
    return true;
}

int main()
{
    /* Variable declaration */
    string msg;
    char ch;
    int key;
    size_t i;
    int choice;

    cout << "*** Caesar Cipher ***\n\n";

    do{

        /* Input Plain Text */
        if (!prompt("\nEnter Plain Text", msg))
            break;

        /* Input key */
        if (!prompt("Enter Key", key))
            break;

        /* Encryption */

        /* Traverse Text */
        for (i = 0; i < msg.size(); ++i)
        {
            ch = msg[i];

            /* apply Encryption lowercase letters */
            if (ch >= 'a' && ch <= 'z')
            {
                ch += key;
                if (ch > 'z')
                {
                    ch = ch - 'z' + 'a' - 1;
                }
                msg[i] = ch;
            }
            /* apply Encryption Uppercase letters */
            else if (ch >= 'A' && ch <= 'Z')
            {
                ch += key;
                if (ch > 'Z')
                {
                    ch = ch - 'Z' + 'A' - 1;
                }
                msg[i] = ch;
            }
        }

        // cout << "===================================\n";
        /* print encryption result */
        cout << "Encrypted Message: " << msg << "\n";

        /* Decryption */
        for (i = 0; i < msg.size(); ++i)
        {
            ch = msg[i];

            /* apply decryption lowercase letters */
            if (ch >= 'a' && ch <= 'z')
            {
                ch -= key;
                if (ch > 'z')
                {
                    ch = ch + 'z' - 'a' + 1;
                }
                msg[i] = ch;
            }
            /* apply decryption Uppercase letters */
            else if (ch >= 'A' && ch <= 'Z')
            {
                ch -= key;
                if (ch > 'Z')
                {
                    ch = ch + 'Z' - 'A' + 1;
                }
                msg[i] = ch;
            }
        }

        /* print decryption result */
        cout << "Decrypted Message: " << msg << "\n";
        //cout << "\n";

        cout << "\nDo you want to continue? ";
    }
    while (prompt("1 or 0", choice) && (choice != 0));

    cin.get();

    return 0;
}