检查输入是否为字母数字时的逻辑错误
Logic error when checking if input is alphanumeric
该程序的目的是检查用户输入的字符是否为字母数字。一旦Void函数确认了正确的条目,然后将其传递给字符串测试以输出消息。我知道这不是很好的编码,但必须这样做。
我不断遇到逻辑错误&我不知道为什么?有人可以帮忙吗?
#include <iostream>
#include <string>
#include <cctype>
using namespace std;
void get_option(char& input);
/**
Takes character entered user input and loops until correct answer
@param y character entered by user
@return to main() once valid entry received
*/
string test(char);
/**
Takes checks character entered user input and loops until correct answer
@param y alphanumeric character entered by user
@return to main() once valid entry received
*/
int main()
{
char y;
//call get_option to prompt for input
get_option(y);
//call test after user input is valid
test(y);
return 0;
}
void get_option(char &x)
{
cout << "Please enter an alphanumeric character: ";
cin >> x;
while (!(isdigit(x)||islower(x)||isupper(x)))
{
cout << "Please enter an alphanumeric character: ";
cin >> x;
}
}
string test(char y)
{
if (isupper(y))
{
cout << "An upper case letter is entered!";
} else if (islower(y)) {
cout << "A lower case letter is entered!";
} else if (isdigit(y)) {
cout << "A digit is entered!";
}
return "";
}
我通过更改test(char)
函数的返回类型来完美工作:
#include <iostream>
#include <string>
#include <cctype>
using namespace std;
void get_option(char& input);
/**
Takes character entered user input and loops until correct answer
@param y character entered by user
@return to main() once valid entry received
*/
int test(char); //Changed from string to int
/**
Takes checks character entered user input and loops until correct answer
@param y alphanumeric character entered by user
@return to main() once valid entry received
*/
int main()
{
char y;
//call get_option to prompt for input
get_option(y);
//call test after user input is valid
test(y);
return 0;
}
void get_option(char &x)
{
cout << "Please enter an alphanumeric character: ";
cin >> x;
while (!(isdigit(x)||islower(x)||isupper(x)))
{
cout << "Please enter an alphanumeric character: ";
cin >> x;
}
}
int test(char y) //Also changed from string to int
{
if (isupper(y))
{
cout << "An upper case letter is entered!";
} else if (islower(y)) {
cout << "A lower case letter is entered!";
} else if (isdigit(y)) {
cout << "A digit is entered!";
}
return 0;
}
(使用C 14编译器在JDoodle上进行测试。((另外,使用Xcode进行了测试。仍然有效(
在我的设置(G 6.4,cygwin(中尝试时,我没有得到任何输出。当我向输出线添加<< endl
时,输出显示。
我怀疑您遇到了同样的问题。
string test(char y)
{
if (isupper(y))
{
cout << "An upper case letter is entered!" << endl; // Add endl
}
else if (islower(y))
{
cout << "A lower case letter is entered!" << endl;
}
else if (isdigit(y))
{
cout << "A digit is entered!" << endl;
}
// This does not make sense but it is syntactically valid.
return 0;
}
Jivedadson是对的。问题是return 0
线。它会导致不确定的行为。将该行更改为
return "";
解决输出问题,endl
与否。拥有endl
更好,但不是必需的。修复return
语句是最重要的任务。
相关文章:
- 输出错误,问题是找到总和5000位数字cpp
- 在使用堆栈为下一个最大数字编写代码时面临 SIGSEGV(分段错误)
- 错误含义;以二进制形式打印数字
- C++ 分段错误:11 错误,同时编码将两个数字相加的链接列表
- 我如何在一个 if 语句中声明所有数字我尝试通过其他方式声明所有数字,如果一个接一个,但似乎代码有逻辑错误
- 分布MPI散点错误的数字
- 涉及模运算符优先级的错误以及C++中具有大数字的括号
- Arduino IDE 错误 - 无法找到数字文字运算符"运算符""f900ff"
- 为什么C++显示错误的数字?
- 错误:数字常量 #define BOOT_PROTOCOL 0x00 之前的预期'>'
- 我的代码似乎在查找最大数字时存在语法错误
- Fibbonaci 递归代码返回错误值,始终返回下一个数字
- C++:按数字排序链表错误
- isdigit() 和 isalnum() 给出错误,因为输入是一个常量字符并且无法转换。其他可能查看输入是否为数字的方法?
- 一个数字的提升精神解析器的分段错误
- 错误:数字常数之前的预期无限制ID:std :: array和std ::向量大小分配
- C++数组中的错误数字
- 构造函数错误:错误:数字常量之前的预期“”,“”或“..”
- [错误]数字常量之前的预期标识符.-setlocale
- 错误:“数字”之前的预期类型说明符