Hi, I am a high school student taking C++.
I had to write a program which takes in a number and lists all of the primes less than or equal to that number.
That I did no problem.
Now the teacher would like us to check the input to be sure that it is allowable.
I have read the FAQ and still don't understand how to take in a string (C++ style) and convert it to an int. Could someone help.
Or is that the best way to do it??
cout << "Please enter a positive integer: ( 0 to quit )";
cin >> user_input;
So I need to be sure that the user has entered a positive integer or 0. If 0 then quit. If not a positive int then I need to ask again.
We have not covered pointers yet so I don't know how to use them. Any help or a link to a place that could help would be much appreciated.
here you go...
Use atoi(). I am not sure which .h file, though:
If(atoi(user_input) <= 0)
cout << "Not a positive number!\n\n";
// Do stuff because it is allowable
Hi I appreciate the response!
When I tried to compile, I got the following error:
cannot convert `user_input' from type `string' to type `const char *'
with the line number with the atoi function on it.
This is the same problems I was having when trying to implement it.
I thought that you can not use atoi on a C++ string, and that it only works on C style strings. But I am having trouble with that as well.
You have to...
You have to change:
or use atoi(user_input.c_str()) which will allow you to use the string class