Removing letter from number problem

This is a discussion on Removing letter from number problem within the C++ Programming forums, part of the General Programming Boards category; Hello. I'm learning C++ for personal use, and I'm having some trouble. Code: //Here are includes, the prototype, and using ...

  1. #1
    Registered User
    Join Date
    Aug 2010
    Posts
    3

    Removing letter from number problem

    Hello. I'm learning C++ for personal use, and I'm having some trouble.

    Code:
    //Here are includes, the prototype, and using namespace std;
    
    int main()
    {
        int testnumber = 0;
        cout<<"Hi! This is the Even or Odd program! Please type a number to determine if it's odd or even!"
        <<"\nIf you want to exit, please hit 0!\n";
        while(!(cin >> testnumber))
        {
            cin.clear();
            cin.ignore(numeric_limits<streamsize>::max(), '\n');
            cout<<"That is not a number! Please get rid of any decimals, and write a number!\n"
                <<"If you want to exit the program, please hit 0!\n";
        }
        EvenOdd(testnumber);
        cout<<"Thank you for using this program! Hit enter to exit.";
        cin.get();
        return 0;
    }
    
    int EvenOdd(int numero)
    {
        if(numero == 0)
        {
            return 0;
        }
        if(numero % 2 == 0)
        {
    
            cout<<numero <<" is an even number!\nPlease enter another number! "
            <<"If you want to exit, please hit zero!\n";
        }
        else
        {
            cout<<numero <<" is an odd number!\nPlease enter another number!\n"
            <<"If you want to exit, please hit zero!\n";
        }
        while(!(cin >> numero))
        {
            cin.clear();
            cin.ignore(numeric_limits<streamsize>::max(), '\n');
            cout<<"That is not a number! Please get rid of any decimals, write a new number, and hit enter!\n"
                <<"If you want to exit the program, please hit 0!\n";
        }
        return EvenOdd(numero);
    }
    As you can see, the program rejects the input if it is a letter. However, if I were to type "9s", the check would pass the value of 9 to EvenOdd. I would rather let the user determine if they should change the number. Thank you!

    -ilikepie2221

  2. #2
    Registered User
    Join Date
    Mar 2008
    Location
    Coimbra, Portugal
    Posts
    85
    Perhaps you should study getline and use that. Then you could search the string that you get for letters, or check if it is only a number in some manner.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Nim Trainer
    By guesst in forum Game Programming
    Replies: 3
    Last Post: 05-04-2008, 04:11 PM
  2. Printing output according to letter chosen problem
    By Guti14 in forum C Programming
    Replies: 5
    Last Post: 08-12-2004, 03:13 AM
  3. Prime number program problem
    By Guti14 in forum C Programming
    Replies: 11
    Last Post: 08-06-2004, 04:25 AM
  4. Random Number problem in number guessing game...
    By -leech- in forum Windows Programming
    Replies: 8
    Last Post: 01-15-2002, 04:00 PM
  5. Assingnment problem
    By Jason in forum C Programming
    Replies: 1
    Last Post: 08-31-2001, 01:20 PM

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