如何在c++中读取do-while循环中的空白
How to read white spaces in a do while loop in c++
我正在尝试扫描读取空白的输入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;
}
相关文章:
- 如何循环打印顶点结构
- 如何在C++中从两个单独的for循环中添加两个数组
- C++我的数学有什么问题,为什么我的代码不能正确循环
- 正在尝试了解输入验证循环
- std::map<struct,struct>::find 找不到匹配项,但是如果我循环通过 begin() 到 end(),我在那里看到匹配项
- 循环后如何继续阅读
- Ardunio UNO解决了多个重叠的定时器循环
- Eigen如何在容器循环中干净地附加矩阵
- 在某些循环内使用vector.push_back时出现分段错误
- 无法通过空白将文本文件行分隔为矢量
- 我正在使用嵌套的while循环来解析具有多行的文本文件,但由于某种原因,它只通过第一行,我不知道为什么
- 为什么我的for循环不能正确获取argv
- 如何声明特征矩阵,然后通过嵌套循环初始化它
- while循环中while循环的时间复杂度是多少
- C++中的高效循环缓冲区,它将被传递给C样式数组函数参数
- 为什么在这个代码结束循环中没有得到结束
- 如何在c++中读取do-while循环中的空白
- 如何使我的循环搜索空白空间
- 如果给定空白regex表达式,则regex_replace中的C++Mac OS无限循环
- 在c++ 11基于范围的for循环中,通过迭代器删除字符串中的空白