不能在回文程序中输入多个字符串
Cannot input more than one string in palindrome program
#include <iostream>
#include <ctype.h>
using namespace std;
void isPalindrome();
int main()
{
char response;
isPalindrome();
cout << "Input another string(y/n)?" << endl;
cin >> response;
response = toupper(response);
if (response == 'Y')
isPalindrome();
return 0;
}
void isPalindrome()
{
char str[80], str2[80];
int strlength;
int j = 0;
int front, back;
bool flag = 1;
cout << "Input a string:" << endl;
cin.getline(str, 80);
strlength = strlen(str);
for (int i = 0; i < strlength; i++)
{
if (islower(str[i]))
str[i] = toupper(str[i]);
}
for (int i = 0; i < strlength; i++)
{
if (isalpha(str[i]))
{
str2[j] = str[i];
j++;
}
}
str2[j] = ' ';
front = 0;
back = strlength - 1;
for (int i = 0; i < j / 2; i++)
{
if (str2[front] != str2[back])
{
flag = 0;
break;
}
}
if (!(flag))
cout << "It is not a palindrome" << endl;
else
cout << "It's a palindrome" << endl;
cout << "str: " << str << " str2: " << str2 << " strlength: " << strlength << " j: " << j << endl;
cout << "front: " << front << " back: " << back << " flag: " << flag << endl;
}
我只是想知道是否有人可以帮助我解释为什么我的代码不工作。
我可以运行它一次,我得到了正确的答案,但是当提示符问我是否要输入另一个字符串,我输入'y'
时,提示符只是跳过输入并自行终止。
我尝试了cin.ginore('n', 80)
,但那只是给了我一堆空行。我在末尾添加了代码位来检查值,它们都转到0
并删除字符串。
也许一个链接到一个适当的解释系统如何处理内存?
编辑:当第二次运行输入序列时,我一直得到同样的问题。输出如下所示:
Input a string:
Radar
It's a palindrome
Input another string(y/n)?
y
_ <- this being my cursor after pressing enter 3 times
我将从头开始重新构建程序,并尝试不使用函数。我仍然希望有一个页面的链接,解释如何使用现代c++处理用户输入。
问题在于:
cin >> response;
这将用户输入的y/n
读入变量response
,但在输入缓冲区中留下一个换行符,由getline
函数和isPalindrome
函数选择。
要解决这个问题,需要在读取用户响应后从输入缓冲区中删除换行符。您可以使用:
cin >> response;
std::cin.ignore(INT_MAX);
使用上面的修复,您可以重试一次回文检查。为了使多次重试成为可能,您需要一个循环。我建议在主程序中使用do-while
循环:
char response;
do {
isPalindrome();
cout << "Input another string(y/n)?" << endl;
cin >> response;
std::cin.ignore(INT_MAX);
response = toupper(response);
} while(response == 'Y');
你需要一个循环。没有代码指示程序返回到顶部。
char response = 'Y';
while (response == 'Y') {
isPalendrome();
cout << "Input another string(y/n)?" << endl;
cin >> response;
}
这不是整个程序,只是while循环所需的关键元素。您应该了解while是如何工作的,并使它为您的程序工作。
在当代c++中,通常使用标准库组件进行字符串处理:
#include <iostream>
#include <string>
int main()
{
std::string line1, line2, response;
do
{
std::cout << "First string: ";
if (!std::getline(std::cin, line1)) { /* error */ }
std::cout << "Second string: ";
if (!std::getline(std::cin, line2)) { /* error */ }
// check line1 and line2 for palindromy
std::cout << "Again (y/n)? ";
std::getline(std::cin, response);
} while (std::cin && (response == "y" || response == "Y"));
}
相关文章:
- 如何使用堆栈将字符串输入拉伸 k 次
- 不确定出了什么问题(字符串输入)
- 如何获取字符串输入n次?
- 使用重定向命令从 stdin 读入的字符串"<"输入文件未正确附加
- isPalindrome不显示输出,isPalindrome函数未使用字符串输入作为字符串参数进行测试
- C++ 对象数组字符串输入在控制台上不起作用
- 为什么 cin 在包含字符串标头后接受字符串输入
- 拆分字符串输入并查找字符是否有效
- 有没有办法将最初作为字符串输入的数字字符数组复制到双向链表中
- 将字符串输入到多维数组中
- 字符串输入后的空格
- 字符串流中的自定义字符串输入
- C 未验证字符串输入
- 总结数字字符串输入的最佳方法
- 如何验证用户将字符串输入到std :: cin中
- 在C++测试家庭作业问题中的字符串输入时无法退出循环(作为数据验证)
- 如何在C++中检测字符串输入中的换行符
- C++将字符串输入拆分为两个整数
- 如何检查字符串输入是否为有效的双精度
- 在读取数据文件时,如何找到用户字符串输入的字谜?