Thread: Please help... Avoiding run-time errors.

  1. #1
    marCpluSpluS
    Guest

    Please help... Avoiding run-time errors.

    Heya,

    I created this code ->

    int max_point;

    do
    {
    //code...
    cin>>max_point;
    } while(max_point < 1 || max_point > 100)

    As long as the user inputs a number, this works well.
    However, when a user enters a character, the appl. crashes.

    What may I do to fix this?

    Rgrds,
    Marc

  2. #2
    Seeking motivation... endo's Avatar
    Join Date
    May 2002
    Posts
    537
    Just check the input stream, if its bad then exit the program, break the loop or whatever. I think this should work

    Code:
    int max_point;
    
    do
    {
    //code...
    cin>>max_point;
    
       if( !cin.good( ) )
       {
          cout << "Incorrect input\nExiting program...." << endl;
          exit( 1 );
       }
    } while(max_point < 1 || max_point > 100)
    Couldn't think of anything interesting, cool or funny - sorry.

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Try this:
    Code:
    int max_point;
    
    do
    {
    //code...
    cin>>max_point;
    if (cin.fail())
    {
       cin.clear();
       cin.ignore();
    }
    } while(max_point < 1 || max_point > 100);
    Or this:
    Code:
    int max_point;
    
    do
    {
    //code...
    cin>>max_point;
    if (cin.fail())
    {
       cin.clear();
       cin.seekg(0);
    }
    } while(max_point < 1 || max_point > 100);

  4. #4
    Registered User marCplusplus's Avatar
    Join Date
    Nov 2001
    Posts
    68
    Thanks for your answers!
    Here is the code I tried:

    Code:
    	do
    	{
    		system("CLS");
    		cout<<"\n\n\t[[ Scissors, Paper, Stone 2002 ]] - [ Coded by mvb ]";
    		cout<<"\n\n\t[ N E W    G A M E ]";
    		cout<<"\n\n\tPOINTS DETAILS:";
    		cout<<"\n\n\tPoints for a win [ between 1 and 100 ]...";
    		cout<<"\n\n\t\t>> ";
    		cin>>pointwin;
    		if (cin.fail())
    		{
    		   cin.clear();
    		   cin.ignore();
    		}
    	} while (pointwin < 1 || pointwin > 100 );
    	do
    	{
    		system("CLS");
    		cout<<"\n\n\t[[ Scissors, Paper, Stone 2002 ]] - [ Coded by mvb ]";
    		cout<<"\n\n\t[ N E W    G A M E ]";
    		cout<<"\n\n\tPOINTS DETAILS:";
    		cout<<"\n\n\tPoints for a win [ between 1 and 100 ]...";
    		cout<<"\n\t\t>> "<<pointwin;
    		cout<<"\n\n\tPoints for a draw [ between 0 and 100 ]...";
    		cout<<"\n\n\t\t>> ";
    		cin>>pointdraw;
    		if (cin.fail())
    		{
    		   cin.clear();
    		   cin.ignore();
    		}
    	} while (pointdraw < 0 || pointdraw > 100 );
    	do
    	{
    		system("CLS");
    		cout<<"\n\n\t[[ Scissors, Paper, Stone 2002 ]] - [ Coded by mvb ]";
    		cout<<"\n\n\t[ N E W    G A M E ]";
    		cout<<"\n\n\tPOINTS DETAILS:";
    		cout<<"\n\n\tPoints for a win [ between 1 and 100 ]...";
    		cout<<"\n\t\t>> "<<pointwin;
    		cout<<"\n\n\tPoints for a draw [ between 0 and 100 ]...";
    		cout<<"\n\t\t>> "<<pointdraw;
    		cout<<"\n\n\tPoints for a loss [ between 0 and 100 ]...";
    		cout<<"\n\n\t\t>> ";
    		cin>>pointloss;
    		if (cin.fail())
    		{
    		   cin.clear();
    		   cin.ignore();
    		}
    	} while (pointloss < 0 || pointloss > 100 );
    If I enter a letter in the first prompt, it works fine (it asks me again).
    However, when I input a letter in the second, it simply fills it with a 0 and moves on to the third and final input.

    What might be wrong in this case?

    Thanks,
    Marc
    No matter how much you know, you NEVER know enough.

  5. #5
    . Driveway's Avatar
    Join Date
    May 2002
    Posts
    469
    Try this:
    Code:
    for(int i=3; i>0; i--;)
    {
    do
    	{
    		system("CLS");
    		cout<<"\n\n\t[[ Scissors, Paper, Stone 2002 ]] - [ Coded by mvb ]";
    		cout<<"\n\n\t[ N E W    G A M E ]";
    		cout<<"\n\n\tPOINTS DETAILS:";
    		cout<<"\n\n\tPoints for a win [ between 1 and 100 ]...";
    		cout<<"\n\n\t\t>> ";
    		cin>>pointwin;
    		if (cin.fail())
    		{
    		   cin.clear();
    		   cin.ignore();
                                       i++;
                                       //so it "reasks"
    		}
    	} while (pointwin < 1 || pointwin > 100 );
    }
    Last edited by Driveway; 08-31-2002 at 10:12 AM.

  6. #6
    Registered User marCplusplus's Avatar
    Join Date
    Nov 2001
    Posts
    68
    Thanks, it worked

    A question though, why is there need to do that? Shouldn't it work well without the for loop?

    Rgrds,
    Marc
    No matter how much you know, you NEVER know enough.

  7. #7
    . Driveway's Avatar
    Join Date
    May 2002
    Posts
    469
    Well you should do it anyways. Less code. Why not do it?

  8. #8
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Anytime the cin fails, nothing gets stored for the variable, so whatever value it held previously it still holds. You can fix this either with a fail flag, or initializing the variable prior to the loop. Either:
    Code:
    	pointdraw = -1;
    	do
    	{
    		system("CLS");
    		cout<<"\n\n\t[[ Scissors, Paper, Stone 2002 ]] - [ Coded by mvb ]";
    		cout<<"\n\n\t[ N E W    G A M E ]";
    		cout<<"\n\n\tPOINTS DETAILS:";
    		cout<<"\n\n\tPoints for a win [ between 1 and 100 ]...";
    		cout<<"\n\t\t>> "<<pointwin;
    		cout<<"\n\n\tPoints for a draw [ between 0 and 100 ]...";
    		cout<<"\n\n\t\t>> ";
    		cin>>pointdraw;
    		if (cin.fail())
    		{
    		   cin.clear();
    		   cin.ignore();
    		}
    	} while (pointdraw < 0 || pointdraw > 100 );
    Or something like:
    Code:
    	bool fail = false;
    	do
    	{
    		system("CLS");
    		cout<<"\n\n\t[[ Scissors, Paper, Stone 2002 ]] - [ Coded by mvb ]";
    		cout<<"\n\n\t[ N E W    G A M E ]";
    		cout<<"\n\n\tPOINTS DETAILS:";
    		cout<<"\n\n\tPoints for a win [ between 1 and 100 ]...";
    		cout<<"\n\t\t>> "<<pointwin;
    		cout<<"\n\n\tPoints for a draw [ between 0 and 100 ]...";
    		cout<<"\n\n\t\t>> ";
    		cin>>pointdraw;
    		if (cin.fail())
    		{
    		   cin.clear();
    		   cin.ignore();
    		   fail = true;
    		}
    	} while (fail == true || pointdraw < 0 || pointdraw > 100);
    Last edited by swoopy; 08-31-2002 at 05:02 PM.

  9. #9
    Registered User marCplusplus's Avatar
    Join Date
    Nov 2001
    Posts
    68
    Thanks
    No matter how much you know, you NEVER know enough.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Displaying Function Run Time
    By pf732k3 in forum C Programming
    Replies: 1
    Last Post: 03-21-2008, 12:36 PM
  2. need help in time zone
    By Gong in forum C++ Programming
    Replies: 2
    Last Post: 01-03-2007, 04:44 AM
  3. file & linked list run time error
    By Micko in forum C Programming
    Replies: 9
    Last Post: 03-06-2004, 02:58 AM
  4. inputting time in separate compilation
    By sameintheend01 in forum C++ Programming
    Replies: 6
    Last Post: 03-13-2003, 04:33 AM
  5. Create class at run time
    By muaed in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 04-11-2002, 08:13 AM