验证用户输入是否有效 [从 'char' 到 'char*' 的转换无效]
Validating user input is valid number or not [invalid conversion from 'char' to 'char*']
好的,所以我是c/c ++的初学者,我正在创建这个小程序,用于检查用户提供的输入是否有效,如果是,则打印" 这是一个数字",否则它会打印"它是一个字符串"
一些示例输出
1 - is a number
-1.1 - is a number
1......1 - is a character string
three - is a character string
.12 is a character string
+0.12 is a number
ABC123ABC - is a character string
我在代码中收到此错误。如果有人能帮我解决这个问题,我将不胜感激。蒂亚
CPP:52:23:错误:从"字符"到"字符*"的转换无效 [-允许]
if (!isNum(c[i]))
{ ~~~^task1.cpp:5:19:注意:初始化'bool isNum(char*)'的参数1 bool isNum(char * p){
我的代码
#include <iostream>
bool isNum(char * p){
if (NULL == p || *p == ' '){
return false;
}
int dot = 0;
int plus = 0;
int minus = 0;
while(*p){
char a = *p;
switch (a)
{
//Only allows 1 dot
case '.':
if (++dot > 1){
return false;
}
break;
//only allows 1 plus sign
case '+':
if (++plus > 1){
return false;
}
//only allows 1 minus sign
case '-':
if (++minus > 1){
return false;
}
//Only allows 0-9
default:
if (a < '0' || a > '9'){
return false;
}
}
p++;
}
return true;
}
int main(){
//char array of size 1024
char c[1024];
std::cout << "Enter something: ";
std::cin >> c;
for(int i = 0; i < sizeof(c); i++){
if (!isNum(c[i])){
std::cout << c << " is a character string";
}
else {
std::cout << c << " is a number";
}
}
}
如果你想练习复杂的算法,解析数字是一个很好的练习。但是,如果您的目标是编写有用,简单的程序,那么您就走错了路。在C++中,许多常见任务已经由C++标准库解决,您只需要使用它们即可。
#include <iostream>
#include <sstream>
#include <string>
int main() {
std::string line;
if (!std::getline(std::cin, line)) {
std::cerr << "error reading the linen";
return 1;
}
std::istringstream in{line};
double num;
if (in >> num && in.peek() == EOF) {
std::cout << "it's a number, " << num << "n";
} else {
std::cout << "it's not a numbern";
}
}
上面的代码比你的代码读起来更高级。最重要的是,它可以处理任意的长行而不会使程序崩溃。
我对C++标题不太熟悉,所以我可能忘记了包含其他一些标题。但是其余的代码应该没问题,即使我没有测试它。
以下
函数isNumber
适合您。
在这里,我使用动态字符序列
std::string
它使我们能够输入任何小于std::string::max_size
的大小字符串。我们可以通过以下方式检查给定字符是否为数字
std::isdigit
.没有额外的副本和对象创建将显示出良好的性能。
输入字符串的左侧和右侧不允许使用空格字符。
我还编写了迭代器的显式类型并避免使用auto
,因为您要标记C++98
:
#include <string>
#include <cctype>
bool isNumber(const std::string& s)
{
// this also validates the following access to s[0]
if(s.empty()){
return false;
}
const std::size_t offset = (s[0] == '+' || s[0] == '-') ? 1 : 0;
std::string::const_iterator begin = s.begin() + offset;
// this also validates the following dereferencing begin
if(begin == s.end()){
return false; // false if just a sign "+" or "-"
}
if(!std::isdigit(static_cast<unsigned char>(*begin))){
return false; // e.g. "+.123"
}
bool isdecimal = false;
for(std::string::const_iterator it = ++begin; it != s.end(); ++it)
{
if (!std::isdigit(static_cast<unsigned char>(*it)))
{
if(!isdecimal && (*it == '.'))
{
isdecimal = true;
if((it+1) == s.end()){
return false; // e.g. "+1."
}
}
else{
return false;
}
}
}
return true;
}
现在实现 main 函数变得简单明了:
演示
#include <iostream>
int main()
{
std::string s;
std::cout << "Enter something: ";
std::getline(std::cin, s);
std::cout << std::endl;
std::cout
<< s << " is a "
<< (isNumber(s) ? "number." : "character string.");
return 0;
}
你去吧,我已经评论了我改变的东西
#include <iostream>
bool isNum(char * p) {
if (NULL == p || *p == ' ') {
return false;
}
int dot = 0;
char a = *p;
if (a<'0' || a>'9') {
if (a != '-' && a != '+') { return false; }
else p++;
}
if (*p<'0' || *p>'9') return false;
p++;
while (*p != ' ') {
a = *p;
switch (a)
{
//Only allows 1 dot
case '.':
if (++dot > 1) {
return false;
}
p++;
if (*p == ' ') return false;
break;
default:
if (a < '0' || a > '9') {
return false;
}
p++;
break;
}
}
return true;
}
int main() {
//char array of size 1024
char c[1024];
std::cout << "Enter something: ";
std::cin >> c;
// you don't need to loop through every character just pass your array of characters & your function is looping through it
if (!isNum(c)) {
std::cout << c << " is a character string";
}
else {
std::cout << c << " is a number";
}
}
相关文章:
- 将无符号char*转换为std::istream*C++
- 为什么这个函数将"const char*"转换为"void* const"而不是"const void*"
- 将函数参数"const char*"转换为"std::string_view"是
- C ++如何在使用"tolower"时将char转换为int
- 有没有办法在C++的赋值中将"char**"转换为"double"?
- JavaCPP 错误:无法将参数"1"的"char*"转换为"Abc*
- 为什么从 char 转换为 std::byte 可能是未定义的行为?
- 如何在虚幻引擎4中将char*转换为TCHAR?
- C++:将模板类型 char 转换为 std::string
- strcmp/char* 转换无法按预期工作
- C++中的SQLite char*转换
- 无法从 const char* 转换为 const char *&
- 在 C++ 中将 char 转换为 int,而无需符号位传播
- 如果必须包含西里尔字符,如何将 char* 转换为 std::string?
- 试图使用strCMP时的char转换错误
- 无法从'const char *'转换为'LPCTSTR'
- 将 std::vector<char> 转换为字符* 会导致字符缺陷
- 使用 static_cast 将 char 转换为 int c++
- 如何在 C++(MFC) 中将 char* 转换为 LPCTSTR
- 无法将const char转换为字符串构造字符串