hello all,
i am new to c++ and just started learning about 1 week ago.

i wrote a (console) program that does simple math, it asks you to enter couple numbers and will output an average.
i want to know how to write and if statement where it will say "input should be a number" if the input is anythign else: letter, $sign or combination of letter and number.

i have been able to do it like this

cin<<value;
if ( value < 0 ) {
       cout<<"value should be more than 0", cin>>value;
}

but i want to know how to do if if value is a letter or anything other than a number

Dani AI

Generated

Short, practical pattern that builds on what , and suggested: read the whole line, try to parse it, and reject it if anything non-numeric remains. That avoids leaving cin in a bad state and handles spaces, signs and decimal/exponent forms in one place.

#include <iostream>
#include <string>
#include <sstream>

double readNumber() {
    std::string line;
    while (std::getline(std::cin, line)) {
        std::istringstream iss(line);
        double value;
        char extra;
        if (iss >> value && !(iss >> extra)) {
            return value; // parsed and nothing extra
        }
        std::cout << "Input should be a number. Try again: ";
    }
    throw std::runtime_error("No input");
}

How this helps: getline gets the whole user entry (so trailing junk does not hang the next read). istringstream attempts numeric extraction; if extraction fails, or if a subsequent >> char succeeds, the line contained invalid characters and should be rejected. This accepts negatives, decimals and scientific notation. For integers, parse into a long/long long and reject if a fractional part remains.

If you prefer C APIs or want positional checks, strtod with endptr or the std::stod family are alternatives—just remember std::stod throws on bad input, and strtod requires checking errno/endptr. See the standard references for std::istringstream and strtod for details: std::istringstream and .

Recommended Answers

All 7 Replies

Member Avatar for Member #46692

Declare it as a char[] or a std::string

Declare it as a char or a std::string

can u give me an example of how to go about doing this

That's is not as easy as it sounds...

You need to
- input the number as a string
- test the string making sure only digits were entered
- if not, output error
- if so, convert the string into your number.

I'd wait on this until you learn just a little more, specifically strings and characters. Give it a week or two and come back to this. It's a good task to understand.

That's is not as easy as it sounds...

You need to
- input the number as a string
- test the string making sure only digits were entered
- if not, output error
- if so, convert the string into your number.

I'd wait on this until you learn just a little more, specifically strings and characters. Give it a week or two and come back to this. It's a good task to understand.

sounds very confusing
i guess ill try to wait till i learn that part

Good choice. But as I said, come back to it. It's an important part of programming.

Member Avatar for Member #46692

Just to add to that, a number would be:

-Anything containing [0123456789]
-And if it has a decimal point it must contain ONLY one [.]

It would be simple enough to write the logic for that.


...And if you are considering the user entering spaces as well, you have to look at using getline() and then either flushing the input buffer or using getline for everything thereafter.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.