Question about Input ! Need your help!

Hello Everyone. I am new to C++ and have a quick question. I am working on a part that involves me writing an "error message" when entering a wrong input. How do i make it so if the user inputs nothing (blank) or a letter or or any other character - the error message will appear? Should I use else in this situation?


1
2
3
4
5
6
7
8
9
10
11
 
cout << "Number of lemons: ";
cin >> numberofLemons;
    
if (numberofLemons < 0)
    {
        cout << "---" << '\n' << "The number of lemons must be positive" << endl;
        return 1;
    }





Thank you.
You could use a switch and include every option you do not want the user to input. the else statement would be quite useless imo. think about this.
if someone inputs abc that would be some weird number like 12301230123123 so bascially you want to check to see if it is a number.

what I like to do for my inputs is something like this:
1
2
3
4
5
6
7
string input, error;

cout << "Input a number: " << flush;
getline(cin, input); // gets everything that the user inputs not just the first character like cin.
for(unsigned int i = 0; i<input.size(); i++) if(isdigit(input[i])) error = "false"; else error = "true";

if(error == "true") cout << "I am an error message." << endl; else cout << "My number is: " << input << endl;
Last edited on
If haven't been at it for very long either, but I think I can help in this case. Use a do ...... while loop.
1
2
3
4
5
6
do 
{
    cout << "Number of lemons: ";
    cin >> numberOfLemons;
}
while (numberOfLemons > 0)


you may want to use a cin.get () function in there as well. If you don't know what cin.get is look it up, it will come in handy down the road.
Yom your method would be useless also. Input 'a' and tell me what happens =p. Thats the one reason I hate using cin with an int.
Last edited on
Well unfortunately in my class i am taking. (I just started) we only learned up to loops so far. some of the terms you used are unfamiliar to me..... is there a simpler way?
which part do you not know? that's pretty simple I'll try to explain the code.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
string input, error;

cout << "Input a number: " << flush; //output(cout) initializer
// << is the "extractor" eg it extracts the string and puts it in the output
// flush just flushes the stream and ends it instead of flushing and starting the next line
getline(cin, input); // gets everything that the user inputs not just the first character like cin.
//getline gets the line
// cin is the input
//input is the string you are inputing it to.
for(unsigned int i = 0; i<input.size(); i++) if(isdigit(input[i])) error = "false"; else error = "true";
//unsigned int is saying it is any positive number
//input.size() is the size of the string eg the amount of characters
//example: Apple is 5 characters long.
//isdigit(... checks if the character is a digit(Number)
//if it is a digit it does something
//input[i] means the character in the string at that position.

if(error == "true") cout << "I am an error message." << endl; else cout << "My number is: " << input << endl;
He is just taking a basic class. Yes there are better ways, but a person needs to build up to it. As I said, I am just starting out as well, so I know what looking at and trying to understand your code just did to Fenrir's brain (boom). He doesn't have the basis to understand what you are doing. I doubt he has learned for loops yet.

That's why suggested he look up the cin.get function, but your right getline () is better. Gotta learn to crawl before you can walk.

Speaking of help. I have a post in the General programing form I could really use a hand with. It's called BlackJack game, if your interested.
Last edited on
I'm in a basic c++ class as well and to me that stuff is a necessity. How can you make any program with out knowing the basic input output, if you can't figure it out from the words getline then you could always just google it and you'd get this http://www.cplusplus.com/reference/string/string/getline/
that is how I teach myself I look up references to code. and he said he learned up to loops so I figured that means he knows what loops are if not
http://www.cplusplus.com/doc/tutorial/control/
the do - while loops will be your friends and for simpiler things the for loop is also nice.
and the rest of my code is just basic strings(const char) and char variables
http://www.cplusplus.com/doc/tutorial/variables/

edit and yeah your code would probably work for the asignment but if you type in a character that isnt a number you have a major problem.
it thinks that 'a' or any other character is a number because you are inputing an int instead of a string it's better to input strings and then read that and see if it is indeed a number or letters.
if you need that number for some sort of function or math then you can convert it back to an int pretty easy by this
#include <stdlib.h> int number = atoi(string.c_str());
http://www.cplusplus.com/reference/cstdlib/atoi/

http://www.cplusplus.com/forum/beginner/99748/

oh and the reference link on the left side at the top of this page is amazing thing to learn c++.
Last edited on
Topic archived. No new replies allowed.