char,isnumber方法 [英] char, isnumber method
本文介绍了char,isnumber方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的代码中有错误,
这是我的代码:
Hi,
I'm having errors in my code below,
This is my code:
#include "stdafx.h"
#include <vector>
#include <iostream>
using namespace std;
int main(){
vector<int> numbers(0);
cout << "please enter you numbers :::\n''entering any characters but numbers is the end of entry''";
char ch;
int i = 0;
while (Isnumber(ch)){ //here is the error
do{
ch = getchar();
int newnumber = 0;
cout << "element(" << i << ") = ";
cin >> newnumber;
numbers.push_back(newnumber);
} while (ch>0 || ch < 9);
}
getchar();
}
两个错误,
它表示标识符未知,
和
它表示变量char是uninitialazed局部变量,
two errors,
it says that identifier is unknown,
and
it says variable char is uninitialazed local variable,
推荐答案
编译器当然是正确的。C ++
没有IsNumber
函数,实际使用ch
uninitialised。
为什么你用C ++
流混合CI / O
电话?
你的意思是:
The compiler is right, of course.C++
has NOIsNumber
function, andch
is actually used uninitialised.
Why are you mixingC I/O
calls withC++
streams?
Did you mean something like:
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector <int> v;
int i;
for (;;)
{
cout << "please enter a number (any other string to exit): ";
cin >> i;
if (!cin.good()) break;
v.push_back(i);
}
}
Isnumber确实是一个未知的标识符,拼写为大写I。
ch确实是一个未初始化的变量。您在为其分配值之前使用它。
Isnumber is indeed an unknown identifier, as spelled it with a capital "I".
And ch is indeed an uninitialized variable. You use it before a value is assigned to it.
您正在使用ch调用
首次执行代码时未定义。为避免这种情况,您有三种选择:IsNumber(ch)
- 使用
执行
...while
loop- 初始化
ch
,其值为IsNumber
return true- 重写你的代码是移除外部循环并在内部循环中使用
if
条件来检查数字
You are callingIsNumber(ch)
withch
undefined when the code is executed the first time. To avoid this you have three options:
- Use a
do
...while
loop- Initialize
ch
with a value that letIsNumber
return true- Rewrite your code be removing the outer loop and use an
if
condition in the inner loop to check for numbers
这篇关于char,isnumber方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文