如何在c++中读取do-while循环中的空白

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

本文关键字:循环 空白 do-while 读取 c++      更新时间:2023-10-16

我正在尝试扫描读取空白的输入msg。我试过scanf("%[^n]s,&msg);,它在我不使用循环的情况下工作。但是当我在do while循环中使用它时,如果我按"1"执行无限循环,scanf("%[^n]s,&msg);将不会读取。在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 ***nn");
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] != ''; 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] != ''; 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();
}

当我在do while循环中使用它时,如果我按"1"执行无限循环,scanf("%[^n]s,&msg);将不会读取。

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

是否有其他进程可以读取do while循环中带有空格的字符串?

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 ***nn";
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;
}