"DO-OVER!" (5th grade kickball)

This is a discussion on "DO-OVER!" (5th grade kickball) within the C++ Programming forums, part of the General Programming Boards category; sorry about the stupid subject but I didnt know what else to use. In my program I would like the ...

  1. #1
    Unregistered
    Guest

    "DO-OVER!" (5th grade kickball)

    sorry about the stupid subject but I didnt know what else to use.

    In my program I would like the input to be validated as a legitatime number and if its not then I want to see an error and ask to reinput the number.

    Code:
    Example:
    Enter number 1: 
    4
    Integer Valid.
    
    Enter number 2:
    5f
    Invalid Integer.  Please try again.
    
    Enter nuber 2:
    -8.8
    Integer Valid.

    So I really have 2 questions:
    1) How can I get a loop to move back up to ask the question again (I hope that makes sense)
    and
    2) Can someone help me with the validation. It already accepts negative and decimals but I dont know how to get it to post an error if its invalid.

    I know its not much, but here is what I have so far.

    Code:
    double Array[25];
    for(int counter = 0; counter < maxnum ; counter++){
    cout << "\nEnter number " <<(counter+1) << "\n";
    cin >> Array[counter];  
    cout << "\n";

  2. #2
    Registered User subdene's Avatar
    Join Date
    Jan 2002
    Posts
    359
    Just use a while loop and a boolean variable. Set the variable to either true or false, depending on the conditions you want to test.

    e.g while(!ValidAns)
    Be a leader and not a follower.

  3. #3
    san
    san is offline
    Registered User
    Join Date
    Jul 2002
    Posts
    13
    i think you have to use a do while loop..

  4. #4
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Originally posted by san
    i think you have to use a do while loop..
    What are you serious? You never HAVE to use a do while loop. I used to use them all of the time but now I hate them. You can use a simple while loop just fine.

  5. #5
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    Use a while loop.

    syntax:
    Code:
        while(condition is true)
        {
               //do what's in here 
        }
    The condition can be any expression such as (i < 4) or ( i == 3 )

    To do loop again if the user did something wrong, then use the
    continue statement. This will jump back up to the beginning
    of the loop.

    You could also us a do...while() loop which will execute at least
    once.

    syntax:
    Code:
       do
       {
            //do what's in here
       }while(condition is true);        //note the semi-colon
    Here's an example:
    Code:
       int i = 0;
       int array[6] = {1,2,3,4,5,-1};
       
       while( array[i] != -1)
       {
             cout << array[i] << " ";
       }
    As for validation, you'd have to read the input into a string, then
    test the characters of the string to see that the input was valid.

    BUT
    cin will accept anything so if the user enter a number then some
    characters, then cin will ignore the charcters.

  6. #6
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    Sorry 'bout that code.
    'i' isn't being incremented as it should be.
    Code:
       int i = 0;
       int array[6] = {1,2,3,4,5,-1};
       
       while( array[i] != -1)
       {
             cout << array[i++] << " ";
       }

  7. #7
    san
    san is offline
    Registered User
    Join Date
    Jul 2002
    Posts
    13
    u could u do while if u want the loop to execute atleast once ..

    pardon my ignorance if this is not correct tell me so that i can learn .. because i very new to c++ and this is my first reply for this post ..

  8. #8
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Originally posted by san
    u could u do while if u want the loop to execute atleast once ..

    pardon my ignorance if this is not correct tell me so that i can learn .. because i very new to c++ and this is my first reply for this post ..
    Well yes you could do it in a do while loop. If you re-read your post it sounds like you are saying the only way to do it is through a do while loop. Sorry for the confusion I just wanted to point out multiple options.

  9. #9
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >It already accepts negative and decimals but I dont know how to get it to post an error if its invalid.
    Does it also accept octal and hexadecimal values? Or floating-point in scientific notation? Those are also concerns if you want the validation routine to be generic across what is considered a "number". But for the error reporting, the simplest way is to return a bool from the validation function and use that return value to print the message:
    Code:
    #include <iostream>
    #include <string>
    
    bool validate ( std::string val )
    {
      int i, j;
      const std::string valid = "0123456789-.";
      for ( i = 0; i < val.length(); i++ ) {
        for ( j = 0; j < valid.length(); j++ )
          if ( val[i] == valid[j] ) 
            break;
        if ( j == valid.length() )
          return false;
      }
      return true;
    }
    
    int main()
    {
      std::string c;
      std::getline ( std::cin, c );
      if ( !validate ( c ) )
        std::cerr<<"Error: Input is not a valid number.\n";
      return 0;
    }
    Past that there are a great number of error handling techniques ranging from simplistic to mindbogglingly complex. Which one you choose depends heavily on what your program does and how secure it needs to be against idiots and bogus values. Most of the time a simple return value which can be descriptive of the general error is more than enough.

    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can someone help me with these errors please code included
    By geekrockergal in forum C Programming
    Replies: 7
    Last Post: 02-10-2009, 01:20 PM
  2. Compiled without error but Result varies!!
    By RahulDhanpat in forum C Programming
    Replies: 10
    Last Post: 02-12-2008, 09:32 AM
  3. Help me pls...About computing a grade
    By Sunday in forum C Programming
    Replies: 2
    Last Post: 11-03-2007, 12:41 PM
  4. C Array
    By fab04 in forum C Programming
    Replies: 14
    Last Post: 04-13-2007, 01:28 AM
  5. Creating a student grade book-how?
    By Hopelessly confused in forum C Programming
    Replies: 5
    Last Post: 10-03-2002, 08:43 PM

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