C++ 检查输入是否为字母数字时出现逻辑错误
该程序的目的是检查用户输入的字符是否为字母数字。一旦void函数确认了正确的输入,它就会被传递到string test以输出消息。我知道这不是很好的编码,但必须这样做 我总是遇到逻辑错误&我不知道为什么?有人能帮忙吗C++ 检查输入是否为字母数字时出现逻辑错误,c++,alphanumeric,C++,Alphanumeric,该程序的目的是检查用户输入的字符是否为字母数字。一旦void函数确认了正确的输入,它就会被传递到string test以输出消息。我知道这不是很好的编码,但必须这样做 我总是遇到逻辑错误&我不知道为什么?有人能帮忙吗 #include <iostream> #include <string> #include <cctype> using namespace std; void get_option(char& input); /** Takes
#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 "";
}
#包括
#包括
#包括
使用名称空间std;
void get_选项(字符和输入);
/**
接受用户输入的字符并循环直到正确答案
@用户输入的参数y字符
@收到有效条目后返回main()
*/
字符串测试(char);
/**
检查用户输入的字符并循环直到正确答案
@用户输入的参数y字母数字字符
@收到有效条目后返回main()
*/
int main()
{
chary;
//调用get_选项以提示输入
获取_选项(y);
//用户输入有效后的调用测试
试验(y);
返回0;
}
void get_选项(char&x)
{
cout>x;
而(!(isdigit(x)| | islower(x)| | isupper(x)))
{
cout>x;
}
}
字符串测试(字符y)
{
如果(isupper(y))
{
cout当我在我的设置(g++6.4,cygwin)中尝试它时,我没有得到任何输出。当我添加时,我通过更改测试(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;
}
#包括
#包括
#包括
使用名称空间std;
void get_选项(字符和输入);
/**
接受用户输入的字符并循环直到正确答案
@用户输入的参数y字符
@收到有效条目后返回main()
*/
int test(char);//从字符串更改为int
/**
检查用户输入的字符并循环直到正确答案
@用户输入的参数y字母数字字符
@收到有效条目后返回main()
*/
int main()
{
chary;
//调用get_选项以提示输入
获取_选项(y);
//用户输入有效后的调用测试
试验(y);
返回0;
}
void get_选项(char&x)
{
cout>x;
而(!(isdigit(x)| | islower(x)| | isupper(x)))
{
cout>x;
}
}
int test(char y)//也从字符串更改为int
{
如果(isupper(y))
{
为什么要声明test
为返回一个string
,然后从中返回0
(不是字符串),然后完全忽略它的返回值呢?还有一种称为isalpha()
的函数
如果使用char
作为输入,则从技术上讲,它具有未定义的行为,因为它需要unsigned char
s。您应该首先将其强制转换为unsigned char。更好的是,检查字母数字字符。
return "";
#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;
}