Hey all, here is the part of my code I need help with:

#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
    int acctNo, dayMin, nightMin, tmin;
    double amtDue;
    char serviceCode;

    cout << "Account Number:  " << flush;
    cin >> acctNo;

What I'm wanting to do is, if someone inputs anything other than an int, output an Invalide Service Code error... here is the rest of my code.

#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
    int acctNo, dayMin, nightMin, tmin;
    double amtDue;
    char serviceCode;

    cout << "Account Number:  " << flush;
    cin >> acctNo;

    cout << "\nService Type:  " << flush;
    cin >> serviceCode;
    cout << endl;

    switch (serviceCode)
        case 'r': case 'R':
        {
        cout << "Please input total minutes used:  " << flush;
        cin >> tmin;

        amtDue = 10.00 + ( (tmin - 50) * 0.20 );

        cout << endl << fixed << showpoint << setprecision(2);
        cout << left << "Account:  " << acctNo << endl << endl;
        cout << setfill('.') << setw(25) << left << "Service Type: " << right << " Regular" << endl;
        cout << setfill('.') << setw(25) << left << "Total Minutes Used " << right << " " << tmin << endl;
        cout << setfill('.') << setw(25) << left << "Amount Due: " << right << " $" << amtDue << endl;
        break;

        case 'p': case 'P':

            cout << "Total Day Time Minutes (6:00am-6:00pm):  " << flush;
            cin >> dayMin;
            cout << "Total Night Time Minutes (6:00pm - 6:00am):  " << flush;
            cin >> nightMin;

            amtDue = 25.00 + ((dayMin - 75) * 0.10) + ((nightMin - 100) * 0.05);
            tmin = dayMin + nightMin;

            cout << endl << fixed << showpoint << setprecision(2);
            cout << left << "Account:  " << acctNo << endl << endl;
            cout << setfill('.') << setw(25) << left << "Service Type: " << right << " Premium" << endl;
            cout << setfill('.') << setw(25) << left << "Total Minutes Used " << right << " " << tmin << endl;
            cout << setfill('.') << setw(25) << left << "Amount Due: " << right << " $" << amtDue << endl;
            break;

        default: cout << "!! Invalid Service Code !!" << endl;
        }
}

If you look at line 51 you'll see something as to what I want to do, though I'm not sure how to do it.

I tried if (acctNo != int) .... but it gave an error of unexpected int.

Thanks for any help! :mrgreen:

Edited 3 Years Ago by Dani: Fixed formatting

oops, can someone delete the other thread? deffinately didn't mean to do that :(

A simplistic way with the minimalistic error checking would be:

#include <iostream>
#include <string>

int main ()
{
    using namespace std ;

    const char allowed_chars[] = "0123456789" ;
    string my_var ;
    int num  = 0 ;

    cout << "Enter the account number: " ;
    cin >> my_var ;
    getchar () ;

    if ( my_var.find_first_not_of (allowed_chars) != string::npos )
    {
        cout << "The thing you have entered is not a number" ;
    }
    else
    {
        num = atoi (my_var.c_str()) ;
        cout << "The number is " << num ;
    }

    getchar ( ) ;
    return 0 ;
}

The trick here is accepting the input as a string so that you would have more control over it and validating it according to your own business rules.

ah, ok... very clever.


only problem is, we haven't gone over any of this:
( my_var.find_first_not_of (allowed_chars) != string::npos )

Its just a simple function of the string class. The above statement means that if the variable my_var doesn't contains any one of the characters specified in the allowed_chars variable, it will return the index of that character.

More reference can be found here.

How about:
Read a string as ~sos~ suggests.
Convert the string to int
If it a valid value, use it, else output error

This article has been dead for over six months. Start a new discussion instead.