Thread: Help with try catch block

  1. #1
    PC Fixer-Upper Waldo2k2's Avatar
    Join Date
    May 2002
    Posts
    2,001

    Help with try catch block

    I have a while loop, within it I get an integer variable...the problem is if the user enters a character the loop goes into a cycle infinitely, my work around was kind of humorous...i added one to a loop count and if it went over 20 in one shot i killed the loop and went on with an error, cleaned up and exited. But, i want to formulate a try catch block so that i can restart the loop safely to get a number instead of a letter....any ideas?

    a stripped down example would work great, i've seen tuturials but they're examples are too vague...i cna't get a grip on it
    PHP and XML
    Let's talk about SAX

  2. #2
    Registered User shuesty's Avatar
    Join Date
    Oct 2002
    Posts
    21
    There are couple ways of doing this. Unfortunately I don't really have the time to get them working but hopefully I'll get you mind going in the right direction.

    If you are looking for integers that are more than one digit long, I would suggest using a while loop to take in the input one character at a time which is inside your main while loop. If it checks out to be a number add it to a string. If you get to the end of the input and you have found only number ... change the string to an integer and exit the loops. If you find a character, jump out of the first loop and restart the program.

    here is some pseudo code to help
    Code:
    int finalNum
    int check
    char input
    string number
    
    while (true)
    
       while (more input)
          check input for integer
          if integer 
             concat to string
          else
             check = -1
             break loop
    
       if check != -1
          break out of loop
    Yes the code is kinda tedious however it's also crash proof. If you write it correctly you don't have to worry about what the user enters because you will have account for any stupidity by them.

  3. #3
    CS Author and Instructor
    Join Date
    Sep 2002
    Posts
    511
    Show us some code that you have attempted. I am unclear what you are trying to ask.
    Mr. C: Author and Instructor

  4. #4
    TransparentMember correlcj's Avatar
    Join Date
    Jun 2002
    Posts
    378
    the problem is if the user enters a character the loop goes into a cycle infinitely
    Greetings,
    Why not use the assert() so that when a character is inputted it will terminate the program. But I would try posting your code just so we can have a better idea of what you have.

    I hope this helps and post your code pls.

  5. #5
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Code:
    if (!cin)
    {
      //[print error msg]
      cin.clear();
      cin.ignore(numeric_limits<streamsize>::max(),'\n');
    }
    //go on...

    (Edit: don't forget to #include <limits>)
    Last edited by Sang-drax; 10-11-2002 at 06:30 PM.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  6. #6
    PC Fixer-Upper Waldo2k2's Avatar
    Join Date
    May 2002
    Posts
    2,001
    Code:
    int company=25;
    int infiniteLoopCout=0;
    
    ...
    
    	while (company>24&&infiniteLoopCount<10) 
    	{
    		std::cout<<"Which Company Is The Part For?"<<endl;
    		std::cout<<"		AMT		0"<<"	||	Mercury		13"<<endl;
    		std::cout<<"		Barber		1"<<"	||	Misc.		14"<<endl;
    		std::cout<<"		BeloitCP	2"<<"	||	Modern		15"<<endl;
    		std::cout<<"		Borg		3"<<"	||	Nissan		16"<<endl;
    		std::cout<<"		Bosch		4"<<"	||	Packard		17"<<endl;
    		std::cout<<"		Camcar		5"<<"	||	Phil		18"<<endl;
    		std::cout<<"		Colt		6"<<"	||	Simmons		19"<<endl;
    		std::cout<<"		Durst		7"<<"	||	SSI		20"<<endl;
    		std::cout<<"		Enkel		8"<<"	||	Tradewind	21"<<endl;
    		std::cout<<"		Goex		9"<<"	||	Warman		22"<<endl;
    		std::cout<<"		Isthmus		10"<<"	||	Warner		23"<<endl;
    		std::cout<<"		Kulasik		11"<<"	||	Witco		24"<<endl;
    		std::cout<<"		Lear		12"<<"	||"<<endl;
    		std::cout<<"\nEnter Choice Here:";
    		std::cin>>company,cin.get();
    		std::cin.clear();
    		infiniteLoopCount++;
    	}
    	if (infiniteLoopCount>=10)
    	{
    		/*normally you would use a try/catch block for this...but what the hey, lyall could use a little entertainment...
    		plus i'd like to see what his reaction would be, considering he doesnt use a computer all that much...*/
    		system("cls");
    		ErrorHandler("Oh No!!!\nYou've Been Caught In The Infinite Loop Of Death!!!\n\nNext Time Enter The Number Of The Company, Have A Nice Day\n");
    	}
    }
    that's the original code, i'm going to try that last suggestion though
    PHP and XML
    Let's talk about SAX

  7. #7
    PC Fixer-Upper Waldo2k2's Avatar
    Join Date
    May 2002
    Posts
    2,001
    well that last suggestion didn't work....here's the problems:
    MING\C++\PROGRAMS\Part Finder Fixed\Part Finder Fixed.cpp(162) : error C2275: 'streamsize' : illegal use of this type as an expression
    C:\Program Files\Microsoft Visual Studio\VC98\INCLUDE\iosfwd(21) : see declaration of 'streamsize'
    c:\DREW\PROGRAMMING\C++\PROGRAMS\Part Finder Fixed\Part Finder Fixed.cpp(162) : warning C4003: not enough actual parameters for macro 'max'
    c:\DREW\PROGRAMMING\C++\PROGRAMS\Part Finder Fixed\Part Finder Fixed.cpp(162) : error C2589: '(' : illegal token on right side of '::'
    c:\DREW\PROGRAMMING\C++\PROGRAMS\Part Finder Fixed\Part Finder Fixed.cpp(162) : error C2059: syntax error : '::'

    anyhow i'm still open for suggestions...thanks much to those who have tried.
    PHP and XML
    Let's talk about SAX

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >anyhow i'm still open for suggestions...
    Make sure you included the header <limits>.

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

  9. #9
    PC Fixer-Upper Waldo2k2's Avatar
    Join Date
    May 2002
    Posts
    2,001
    oops, forgot the header
    im at work right now, and the programs at home, i'll check it there. thanks for telling me about that, i must have missed his edit

  10. #10
    PC Fixer-Upper Waldo2k2's Avatar
    Join Date
    May 2002
    Posts
    2,001
    well i did include limits...here's my code as it stands now...with errors in compiling:
    Code:
    		if (!cin)
    		{
    			cin.clear();
    			cin.ignore(numeric_limits<streamsize>::max(),'\n');
    			company=25;
    		}
    errors:

    \DREW\PROGRAMMING\C++\PROGRAMS\Part Finder Fixed\Part Finder Fixed.cpp(157) : warning C4003: not enough actual parameters for macro 'max'
    c:\DREW\PROGRAMMING\C++\PROGRAMS\Part Finder Fixed\Part Finder Fixed.cpp(157) : error C2589: '(' : illegal token on right side of '::'
    c:\DREW\PROGRAMMING\C++\PROGRAMS\Part Finder Fixed\Part Finder Fixed.cpp(157) : error C2143: syntax error : missing ')' before '::'
    c:\DREW\PROGRAMMING\C++\PROGRAMS\Part Finder Fixed\Part Finder Fixed.cpp(157) : error C2059: syntax error : ')'

    thanks again for all your time
    PHP and XML
    Let's talk about SAX

  11. #11
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Solution:
    Code:
    #ifdef max
      #undef max
    #endif
    #include <limits>
    or just forget about the numeric_limits thing and replace it with 10000 or something.
    But numeric_limits<streamsize>::max() looks more professional.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  12. #12
    PC Fixer-Upper Waldo2k2's Avatar
    Join Date
    May 2002
    Posts
    2,001
    ok, got all that worked out, but i had to change some of my logic around...and it's giving me a quirk, when entering in the company variable i have to hit enter twice, this is because i removed cin.get() because it forced the if (!cin) part to not work correctly, does anyone have any suggestions on how to fix this? (note i can't use cin.get or getline because it's an integer variable)
    Code:
    while (company>24)
    {
    ...
    		std::cout<<"\nEnter Choice Here:";
    		std::cin>>company;
    		if (!cin)
    		{
    			cin.clear();
    			cin.ignore(numeric_limits<streamsize>::max(),'\n');
    			company=25;
    			cout<<"\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"; //cls messes up so just do blank lines
    		}
    		cin.get();
    		cin.clear();
    	}
    ??
    PHP and XML
    Let's talk about SAX

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. About aes
    By gumit in forum C Programming
    Replies: 13
    Last Post: 10-24-2006, 03:42 PM
  2. catch block giving parse error
    By Mr_roboto in forum C++ Programming
    Replies: 5
    Last Post: 03-01-2006, 04:14 PM
  3. HUGE fps jump
    By DavidP in forum Game Programming
    Replies: 23
    Last Post: 07-01-2004, 10:36 AM
  4. Manipulating the Windows Clipboard
    By Johno in forum Windows Programming
    Replies: 2
    Last Post: 10-01-2002, 09:37 AM
  5. pointers
    By fanaonc in forum C Programming
    Replies: 3
    Last Post: 11-17-2001, 02:18 AM