how to exclude character input

This is a discussion on how to exclude character input within the C++ Programming forums, part of the General Programming Boards category; the program i wrote: it works alright if i input a number, but any mist ake to input a letter, ...

  1. #1
    Unregistered
    Guest

    how to exclude character input

    the program i wrote: it works alright if i input a number, but any mist ake to input a letter, it will cause the program run infinitely. Any comment or help to fix the problem will be very appreciated.


    #include <iostream.h>
    int main ()
    {
    double premium, discounted;
    int age, accident, units;
    do
    {
    cout << "Enter your age:" ;
    cin >> age;
    if ( age <= o)
    cout << " error input, try again : " ;
    } while ( age <= 0 ) ;


    cout << "Enter accident if you had in the past, (yes = 1 and no = 0): " ;
    cin >> accident ;
    cout << "Enter number of units if you are a student ; if not, enter 0: " ;
    cin >> units;

    if ( age >= 26 && accident > 0 && units >= 12 )
    premium = 80 - ( 80*0.05 ), discounted = ( 80*0.05 ) ;

    else if ( age >=26 && accident <= 0 && units >= 12 )
    premium = 80 - ( 80*0.05 + 80*0.10 ), discounted = ( 80*0.05 + 80*0.10 ) ;

    else if ( age >=26 && accident <= 0 && units < 12 )

    premium = 80 - ( 80*0.10 ), discounted = ( 80*0.10 ) ;

    else if ( age >=26 && accident > 0 && units < 12 )

    premium = 80, discounted = 0 ;
    else if ( (age < 26 && age >= 18 ) && accident > 0 && units >= 12 )

    premium = 100 - ( 100*0.05 ), discounted = ( 100*0.05 ) ;

    else if ( ( age < 26 && age >= 18 ) && accident <= 0 && units >= 12 )

    premium = 100 - ( 100*0.15 ), discounted = ( 100*0.15 ) ;

    else if ( ( age < 26 && age >= 18 ) && accident <= 0 && units < 12 )

    premium = 100 - ( 100*0.10 ), discounted = ( 100*0.10 ) ;

    else if ( ( age < 26 && age >= 18 ) && accident > 0 && units < 12 )

    premium = 100, discounted = 0 ;

    else if ( age < 18 && accident > 0 && units >= 12 )

    premium = 150 - ( 150*0.05 ), discounted = ( 150*0.05 ) ;

    else if ( age < 18 && accident <= 0 && units >= 12 )

    premium = 150 - ( 150*0.15 ), discounted = ( 150*0.15 ) ;

    else if ( age < 18 && accident <= 0 && units < 12 )

    premium = 150 - ( 150*0.10 ), discounted = ( 150*0.10 ) ;

    else if ( age < 18 && accident > 0 && units < 12 )

    premium = 150, discounted = 0 ;

    cout << "discounted :" << discounted << endl;

    cout << "premium : " << premium << endl;
    return 0;
    }

  2. #2
    Just one more wrong move. -KEN-'s Avatar
    Join Date
    Aug 2001
    Posts
    3,230
    try looking up the isdigit() function.

  3. #3
    Registered User Dual-Catfish's Avatar
    Join Date
    Sep 2001
    Posts
    802
    if ( age <= o)
    cout << " error input, try again : " ;
    } while ( age <= 0 ) ;
    You made a typo there.
    Also
    You can do:

    while (cin.fail())
    {
    cout << "Input Error!\n";
    cin >> age;
    }

    -or-

    while (!(cin >> age))
    {
    cout << "Input Error!\n";
    cin >> age;
    }

    To catch input errors..
    I'm not sure about the second, it might just be while (cin >> age)

  4. #4
    Unregistered
    Guest
    hello

    I try to fix but have no success because I am a beginer. Any more help? this is a lab excercise, I already get grade for it, instructor said it's ok for now, but I want to find out the "bug"/

    thank

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    If your variable isn't a char type then you can use a conditional to check the success of cin.
    Code:
    int var;
    if ( cin>>var )
      // It worked, var is an int
    else
      // Error, user entered a char
    
    or
    
    while ( cin>>var ) {
      // Do your processing
    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. About aes
    By gumit in forum C Programming
    Replies: 13
    Last Post: 10-24-2006, 04:42 PM
  2. Trouble with a lab
    By michael- in forum C Programming
    Replies: 18
    Last Post: 12-06-2005, 11:28 PM
  3. Ending user input with # character
    By jowatkins in forum C++ Programming
    Replies: 2
    Last Post: 04-27-2004, 11:41 AM
  4. Character handling help
    By vandalay in forum C Programming
    Replies: 18
    Last Post: 03-29-2004, 05:32 PM
  5. Character input and conditional processing
    By Yawgmoth in forum C++ Programming
    Replies: 3
    Last Post: 12-23-2002, 06:25 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21