CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    May 2008
    Posts
    16

    Question Beginner C++ question

    My program prompts the user for a number between 1 & 5.

    How can I check if the user entered a number between the given range, and not a random character?

    I am using fgets(...) to read the user's answer which returns a string if I am correct.

    Thanks in advance!

  2. #2
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: Beginner C++ question

    Translate the string to the equivalent number. Using atoi is probably the easiest way.

  3. #3
    Join Date
    May 2008
    Location
    *****, Nigeria
    Posts
    40

    Re: Beginner C++ question

    use something like
    Code:
    (0 < 1) && (1 < 5);
    good with if statement.

  4. #4
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Beginner C++ question

    Since it's a single digit, you could just read in a char and compare it to '1' and '5' (the ASCII characters, not the numbers). However, to protect against multiple characters being entered, you might want to read an entire string and then just compare the first character.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured