Click here to Skip to main content
15,891,529 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I'm having errors in my code below,
This is my code:
C++
#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();
}

two errors,
it says that identifier is unknown,
and
it says variable char is uninitialazed local variable,
Posted

The compiler is right, of course. C++ has NO IsNumber function, and ch is actually used uninitialised.
Why are you mixing C I/O calls with C++ streams?
Did you mean something like:
C++
#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);
  }
}
 
Share this answer
 
v2
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.
 
Share this answer
 
Comments
m.r.m.40 30-Jan-15 4:22am    
well I solved it without using IsNumber, in a very easier way, do you think I should post that solution here?
nv3 30-Jan-15 5:30am    
Good for you. Should you post it as a solution? I don't think so. Reading a couple of numbers and displaying them again is not such a tremendous accomplishment. Please consider that the normal software developer doesn't even think about such trivial tasks. Sorry, that should not discourage you to continue learning.
You are calling IsNumber(ch) with ch 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 let IsNumber return true
  • Rewrite your code be removing the outer loop and use an if condition in the inner loop to check for numbers

Overall there are much more problems. One is:
getchar return characters and not numbers. So you may compare digit chracters using '0' to '9' (with quotes).
 
Share this answer
 
Well, All of the solutions were helpful and the resault of all the 3 solution before my own solution is the code below,

C++
#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''\n";
	//char ch;
	int counter = 0;	
		do{			
			int newnumber = 0;
			cout << "element(" << counter << ") = ";
			counter++;
			cin >> newnumber;
			numbers.push_back(newnumber);
			if (cin.fail()){
				cout << "entered numbers are:\n";
				for (vector<int>::iterator i = numbers.begin(); i != numbers.end(); i++)
				{
					cout << *i;
					if (i != numbers.end()-1)cout << " - ";
				}				
			}
		} while (cin.good());
		getchar();
}

using cin function ( cin.good and cin.fail) instead of IsNumber.
and I also removed the outer while loop.</int></int></iostream></vector>
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900