Hey, i saw this thread about some one needing homework help but it was solved so i made one and it's not real homework but i was wondering how i could do something if the user entered a letter eg "e".

#include <iostream>
using namespace std;

int main()
{
    int dollarAmmount;
    
    cout << "Enter A Dollar Ammount Between $0 & $100" << endl;
    
    cout << "$";
    cin >> dollarAmmount;
    
    if (dollarAmmount < 0)
    cout << "ERROR : Dollar Ammount Is To Low.";
    
    if (dollarAmmount > 100)
    cout << "ERROR : Dollar Ammount Is To High.";
    
    else
    cout << "Thanks.";
    
    system("PAUSE>nul");
}

Like.. to make it have a error like "ERROR : Input Is Not A Digit."

Recommended Answers

All 3 Replies

when you enter a non-digit cin will set the error flag so all you have to do is test for it

if( cin.error() )
{
   cout << "ERROR ...";
   cin.clear(); // clear the error flag
}

error: 'struct std::istream' has no member named 'error'

Yes I see that doesn't work. Try this

if( cin.peek() != '\n')
    {
        cout << "Error\n";
    }
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.