Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 131,592 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,975 people online right now. Registration is fast and FREE... Join Now!




Error Checking - Recursive

 
Reply to this topicStart new topic

Error Checking - Recursive, Integer vs Character

grinsalot
post 9 Oct, 2006 - 10:19 AM
Post #1


New D.I.C Head

*
Joined: 8 Apr, 2006
Posts: 49


My Contributions


Hi,

I'm writing a program that needs to accept input of integers only. I am having problems with the recursive portion of my program. If the user enters any characters (A-Z) or otherwise, I need to output an error message and prompt again for input. My current code works; however, it's not recursive. I've only succeeded in making this an infinite loop by changing the "if" to a "while". Thank you for any help.

CODE
int main()
{
  int j = 0;
  cout << "Please enter a number to calculate: ";
  if(!(cin >> j))
    cout << "You must enter a number!" << endl << endl;

  else
    cout << "You entered " << j << endl << endl;
}
User is offlineProfile CardPM

Go to the top of the page


Vextor
post 9 Oct, 2006 - 11:13 AM
Post #2


D.I.C Regular

Group Icon
Joined: 22 May, 2002
Posts: 288



Thanked 1 times

Dream Kudos: 25
My Contributions


Under the 'if' you would put a call to main(). This way everytime the user inputs an invalid character it 'recalls' the function and starts over. If this is all the program does, this should be ok. If not, however, you may want to make a function specifically for this process to keep it organzied.
User is offlineProfile CardPM

Go to the top of the page

grinsalot
post 12 Oct, 2006 - 01:37 PM
Post #3


New D.I.C Head

*
Joined: 8 Apr, 2006
Posts: 49


My Contributions


QUOTE(Vextor @ 9 Oct, 2006 - 12:13 PM) *

Under the 'if' you would put a call to main(). This way everytime the user inputs an invalid character it 'recalls' the function and starts over. If this is all the program does, this should be ok. If not, however, you may want to make a function specifically for this process to keep it organzied.



Thank you Vextor!
User is offlineProfile CardPM

Go to the top of the page

Amadeus
post 12 Oct, 2006 - 03:21 PM
Post #4


g++ -o drink whiskey.cpp

Group Icon
Joined: 12 Jul, 2002
Posts: 12,158



Thanked 32 times

Dream Kudos: 25
My Contributions


I feel obligated to point out that a program should never be designed so that the main function recursively calls itself. This is extremely ill advised, and a very bad coding habit. Contrary to what some may think, calling the main function from within the main function does not simply begin the main function over...it launches another instance of the program. This can lead to undefined behaviour. It is not legal in standard C++.

The proper way to structure this program would be to prompt for input, check it, and prompt again if invalid. This can be accomplished with a while loop, but should not be done through a recursive call to the main function.
User is offlineProfile CardPM

Go to the top of the page

janotte
post 13 Oct, 2006 - 03:17 AM
Post #5


New D.I.C Head

*
Joined: 28 Sep, 2006
Posts: 26


My Contributions


So to be explicit you should do your recursion outside main(). Something like the following would be better practice.

CODE


// declare function prototype
void InputChecker();

int main()
{
  InputChecker();
} // end main


void InputChecker()
{
   // collect input
   if(!(cin >> j)) // test input
  {
    // do something
    InputChecker();        // recursive function call
  }
    else
    // do something else

} // end InputChecker

User is offlineProfile CardPM

Go to the top of the page

gregoryH
post 13 Oct, 2006 - 06:44 PM
Post #6


D.I.C Regular

Group Icon
Joined: 4 Oct, 2006
Posts: 417



Dream Kudos: 50
My Contributions


Hi All

see my explanation about recursion here

a better way to go would be:

CODE
/*
   Project Software Engineer
      Mr Gregory Hicks BIT
        Sydney Australia
           (C) 2006
        
   This code may be used provided
   credit is given for the source
*/

#include <cstdlib>
#include <iostream>

using namespace std;

int  getUserInput ( const char* msg , const int  min , const int  max , const int  quit );
char getUserInput ( const char* msg , const char min , const char max , const char quit );

int main(int argc, char *argv[])
{
    int t =  getUserInput ( "Enter an integer" , 0 , 10 , -1 );
    if ( t != -1 )
      cout << " Integer entered is: " << t << endl;
      
    char c = getUserInput ( "Enter a letter" , 'A' , 'z' , '0' );
    if ( c!= '0' )
      cout << "Letter entered is: " << c << endl;
      
    system("PAUSE");
    return EXIT_SUCCESS;
}

/*
   Purpose: To get an integer input from the user
   Accepts: msg - display message
        Min - smallest acceptable value
        Max - largest acceptable value
        quit - vaue to force quit on return
*/
int getUserInput (  const char* msg , const int  min , const int  max , const int quit  )
{
    int input;
    
    do {
        cout << msg << " (Range: " << min << " to " << max << " , quit= " << quit << "): ";
        cin >> input;
    } while ( input != quit && ( input < min || input > max ) );
    return input;
}

/*
   Purpose: To get a char input from the user
   Accepts: msg - display message
        Min - smallest acceptable value
        Max - largest acceptable value
        quit - vaue to force quit on return
*/
char getUserInput ( const char* msg , const char min , const char max , const char quit )
{
    char input;
    
    do {
        cout << msg << " (Range: " << min << " to " << max << " , quit= " << quit << "): ";
        cin >> input;
    } while ( input != quit && ( input < min || input > max ) );
    return input;
}


This post has been edited by gregoryH: 13 Oct, 2006 - 07:06 PM
User is offlineProfile CardPM

Go to the top of the page

Amadeus
post 13 Oct, 2006 - 07:11 PM
Post #7


g++ -o drink whiskey.cpp

Group Icon
Joined: 12 Jul, 2002
Posts: 12,158



Thanked 32 times

Dream Kudos: 25
My Contributions


Again, an excellent example, but I again fail to see where you have gathered the idea that recursion is not a concept that is understood. In my post above, please note that I have recommended using a loop and a suitable break condition - exactly as you have demonstrated in your code.
User is offlineProfile CardPM

Go to the top of the page

gregoryH
post 13 Oct, 2006 - 07:28 PM
Post #8


D.I.C Regular

Group Icon
Joined: 4 Oct, 2006
Posts: 417



Dream Kudos: 50
My Contributions


QUOTE(grinsalot @ 9 Oct, 2006 - 11:19 AM) *

<snip> having problems with the recursive portion <snip>.


QUOTE
but I again fail to see where you have gathered the idea that recursion is not a concept that is understood.


The idea began in this thread at the first quote. The attempts to recurse main() and then move the recursive code to call something other than main() I perceived as a lack of understanding how recursion works and its application.

Hope this clears up where I got the idea from.
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/20/08 03:49AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month