Simple program, stopping invalid character infinite loop

This is a discussion on Simple program, stopping invalid character infinite loop within the C++ Programming forums, part of the General Programming Boards category; Hi everyone, I've been super busy over the last couple of months and not had a chance to carry on ...

  1. #1
    Registered User
    Join Date
    Jun 2012
    Location
    Morden, UK
    Posts
    127

    Simple program, stopping invalid character infinite loop

    Hi everyone,

    I've been super busy over the last couple of months and not had a chance to carry on with the jumping into c++ pdf. However, now I'm back and ready, and stuck, already!

    Here is my program, I thought I'd refresh my memory on how loops work:

    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main ()
    
    {
        int userSelection;
        string file = "File";
        string edit = "Edit";
        string view = "View";
        string navigate = "Navigate";
        
        cout << "1 - " << file << "\n";
        cout << "2 - " << edit << "\n";
        cout << "3 - " << view << "\n";
        cout << "4 - " << navigate <<  "\n\n";
        
        cout << "Please select a number from the list: ";
        cin >> userSelection;
        
        while ( userSelection < 1 || userSelection > 4 )
        {
            cout << "Please try again: ";
            cin >> userSelection;
        }
        
        if ( userSelection == 1 )
        {
            cout << "You chose " << file;
        }
        else if ( userSelection == 2 )
        {
            cout << "You chose " << edit;
        }
        else if ( userSelection == 3 )
        {
            cout << "You chose " << view;
        }
        else
        {
            cout << "You chose " << navigate;
        }
        
        
        return 0;
        
    }
    So it all works ok, entering any number other than 1,2,3 or 4 results in a prompt to "Please try again:".

    However, if entering anything other than a number, i.e. a letter, t, r, a, whatever, the cin fails and goes into an infinite loop.

    I've been reading about this happening in other people's programs but haven't been able to apply any solution to my own. Looking for some kind of 'not a number' function or something. If the program only allows 1,2,3 or 4 in order to continue, I would have logically thought any other input would be bad, hence show the prompt. However, I don't understand how c++ works so my own logic probably goes out the window!

    Any help is much appreciated, thanks.

    Sam.
    Last edited by samwillc; 01-02-2013 at 02:09 PM.

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Glyfada,Athens
    Posts
    2,650
    You could something like this .

    Or just check the ascii code. But this can be a little tricky, if you suppose an input that contains numbers and letters (e.g. 3rr4)
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  3. #3
    Registered User
    Join Date
    Jun 2012
    Location
    Morden, UK
    Posts
    127
    Thanks for the help but still not following.

    I tried this in the code:

    Code:
        while ( userSelection < 1 || userSelection > 4 )
        {
            if(cin.fail())
            {
                cin.ignore();
                cout << "Please enter a number: ";
                cin >> userSelection;
            }
                cout << "Please try again: ";
                cin >> userSelection;
        }
    No good either. Still goes into the loop when entering a letter. Just runs a loop, Please enter a number. Please try again. Please enter a number. Please try again. Please enter a number. Please try again. Please enter a number. Please try again...

    I could do with knowing WHY it fails. What's the problem with anything other than a number?

    **update**

    After doing a bit more digging, I found a snippet from another stackoverflow post and modified thus:

    Code:
        while ( userSelection < 1 || userSelection > 4 )
        {
            if (!(cin))
            {
                cout << "Please enter numbers only.\n";
                cin.clear();
                cin.ignore(); 
            }
                cout << "Please try again: ";
    This works

    However, should this line:

    Code:
    if (!(cin))
    be

    Code:
    if(!(cin >> userSelection))
    or does it not matter?

    Sam.

  4. #4
    Registered User
    Join Date
    Dec 2012
    Posts
    289
    try it and see. thats how you learn

  5. #5
    Registered User
    Join Date
    Jun 2012
    Location
    Morden, UK
    Posts
    127
    I did and it didn't make any difference, to the naked eye i.e to result was the same if I tried a letter.

    However, what I really wanted to know was whether it mattered or not in a technical sense/better programming practice.

    Sam.

  6. #6
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Glyfada,Athens
    Posts
    2,650
    If you want to realise it, then it matters. If it is for a homework, I think it is ok.

    If I were you I would have a look in cctype . They are really easy to use and helpful.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  7. #7
    Registered User
    Join Date
    Jun 2012
    Location
    Morden, UK
    Posts
    127
    Quote Originally Posted by std10093 View Post
    If you want to realise it, then it matters. If it is for a homework, I think it is ok.

    If I were you I would have a look in cctype . They are really easy to use and helpful.
    Hi, thanks, it's just a practice program so I guess it's not of major importance right now. I will be writing many small programs to practice until I fully understand the basics before I move on. Tedious I know, but so was learning HTML/CSS and now I'm very proficient at this! Maybe not the best example but just to point out I'm not put off with boredom if the greater goal is a good one!

    cctype looks interesting, this may be a silly question, but how can I use this library. Do I need only to include ctype.h at the top of my program or do I need to 'install' the library. Not sure how this works.

    Thanks.

    Sam.

  8. #8
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Glyfada,Athens
    Posts
    2,650
    Exactly! You just include the header like this
    Code:
    #include <cctype>
    ...
    If you want to be updated in HTML and CSS start learning HTML5 and CSS3. They are not standard yet, but for sure there are going to be. I did a post in my blog, if you want to see.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  9. #9
    Registered User
    Join Date
    Jun 2012
    Location
    Morden, UK
    Posts
    127
    Great, thanks.

    I use a bunch of stuff from CSS3 already and a few tags from HTML5, no animation though. With limited time, I am always pulled in two directions, the web design vs the programming.

    I feel learning programming for me takes priority as if I get back into web design, it (hopefully) will pay off having a much better grasp of logic. I use Drupal and have no knowledge of php so I need to learn to code! I will be doing Java for 1 year at the end of this year in my degree so I am hoping to carry on with c++ throughout.

    My plan is to develop a good skill set by the time I'm 40! 33 now and feeling very old compared to all these 20y/o programmers that are already miles ahead of me. It's actually quite daunting but not going to put me off!

    Sam.

  10. #10
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Glyfada,Athens
    Posts
    2,650
    I am exactly 20 years! And every night before going to bed, I say to myself I am a programmer! But the 'miles ahead' you said seems too much

    I like your determination. Feel free to post back if needed
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  11. #11
    Registered User
    Join Date
    Jun 2012
    Location
    Morden, UK
    Posts
    127
    Quote Originally Posted by std10093 View Post
    I am exactly 20 years! And every night before going to bed, I say to myself I am a programmer! But the 'miles ahead' you said seems too much

    I like your determination. Feel free to post back if needed
    Thanks. Written another 3 small programs today that do next to nothing! All good though

    Sam.

  12. #12
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Glyfada,Athens
    Posts
    2,650
    You have to begin from somewhere
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  13. #13
    Registered User
    Join Date
    Dec 2012
    Posts
    289
    i used to be the 13 year old programmer, i was great with dos batch, basic, appletalk.....and now i am a 33 year old programmer with c,c++,php.....

    and i still have a long way to go as i decided now to start win32 apps, instead of just console apps......

    no matter how much you learn, how much you study, how much you read, you will NEVER know it all!!!

    the 20 years olds just think they know it all!!!

    poor std10093!!! :P

    joking!!!

  14. #14
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Glyfada,Athens
    Posts
    2,650
    Good one

    I agree, that education and learning lasts for a lifetime
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  15. #15
    Registered User
    Join Date
    Jun 2012
    Location
    Morden, UK
    Posts
    127
    Quote Originally Posted by Crossfire View Post
    i used to be the 13 year old programmer, i was great with dos batch, basic, appletalk.....and now i am a 33 year old programmer with c,c++,php.....

    and i still have a long way to go as i decided now to start win32 apps, instead of just console apps......

    no matter how much you learn, how much you study, how much you read, you will NEVER know it all!!!

    the 20 years olds just think they know it all!!!

    poor std10093!!! :P

    joking!!!
    lol. I'm still doing little programs on the command line. But unlike yourself, I'm 33 and only just starting! However, I'm a quick learner and have a huge amount of determination and patience to boot, which I imagine I will need both of in abundance!!

    I'm doing java for a year in my degree too (module starts September 2013) so some formal learning will be very useful.

    Sam.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. My program's in an infinite loop!
    By Peach in forum C Programming
    Replies: 3
    Last Post: 02-24-2008, 12:58 PM
  2. Stopping an Infinite Loop
    By linuxpyro in forum C Programming
    Replies: 4
    Last Post: 11-30-2006, 11:21 AM
  3. Infinite Loop when entering invalid input
    By acwheat in forum C Programming
    Replies: 5
    Last Post: 04-18-2006, 04:17 PM
  4. Program gets stuck in infinite loop
    By Xanth in forum C++ Programming
    Replies: 10
    Last Post: 02-07-2005, 11:51 PM
  5. detect infinite loop in c program
    By abhivyakat in forum C Programming
    Replies: 19
    Last Post: 10-01-2003, 06:55 AM

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