我正在尝试扫描读取空白的输入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 ***\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] != '\0'; 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] != '\0'; 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();
}
答案 0 :(得分:2)
当我在do while循环中使用它时,
scanf("%[^\n]s,&msg);
将不会读取如果我按'1'执行无限循环。
您正在告诉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 ***\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;
}