Thread: problems with error-handling

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    244

    problems with error-handling

    My assignment in my extremely basic C++ class is to create a program which tells the user if a is a factor of b, and vice versa.

    Well thats fine and dandy, and takes a minute to code, but as a challenge to myself I decided to make the program "smart" - not let the user crash it or enter non-numeric values. Anyways, this is what I have so far, but it just doesn't work:

    Code:
    // By Will Herrick
    // takes two numbers and sees if they are factors of each other.
    // also some practice on error-handling.
    // last updated 9-26-02 (doesn't work quite yet!)
    
    #include <iostream>
    #include <cstdlib>
    
    using namespace std;
    
    int checkSize(int);
    int checkNumeric(char[6]);
    void factor(int,int);
    int getNumber();
    
    int main()
    {
    	int ai;
    	int bi;
    	int exit = 0;
    	int ans;
    
    	do
    	{
    		cout << "Enter a:";
    		ai = getNumber();
    		cout << endl << "Enter b:";
    		bi = getNumber();
    		cout << endl;
    		factor(ai,bi);
    		cout << "Try Again(1) or Quit(2)? ";
    		cin >> ans;
    		if(ans == 1) exit = 0;
    		if(ans == 0) exit = 1;
    		else 
    		{
    			cout << endl << "Thats not a possible choice, try again: ";
    			cin >> ans;
    		}
    	} while(exit==0);
    	system("PAUSE");
    	return 0;
    }
    
    int checkSize(int a)
    {
    	if(a > 32766)
    	{
    		return 0;
    	}
    	if(a < 32766) return 1;
    }
    
    int checkNumeric(char a[6])
    {
    	int x;
    	int i;
     int marker;
    	
    	for(x = 0; x < 6; x++)
    	{
    		i = a[x];
    		if((i < 48 || i > 57) && i != '\0')
    		{
    			marker = 1;
    		}
    	}
    	if(marker == 1) return 0;
     if(marker != 1) return 1;
    }
    
    void factor(int a, int b)
    {
    		if(a % b == 0) cout << b << " is a factor of " << a << endl;
    		if(b % a == 0) cout << a << " is a factor of " << b << endl;
    		if(a % b != 0 && b % a != 0) cout << "Neither number is a factor of the other." << endl;
    }
    
    int getNumber()
    {
    	char a[6];
    	char *p;
    	int r;
    	int isNum;
    	int isSize;
    	
    	cout << endl << "Enter the NUMBER: ";
    	cin.get(a,6);
    	cin.ignore(80, '\n');
    	isNum = checkNumeric(a);
    	if(isNum == 1)
    	{
    		p = &a[0];
    		r = atoi(p);
    		isSize = checkSize(r);
    		if(isSize == 1) return r;
    		else 
    		{
    			r = getNumber();
    			return r;
    		}
    	}
    	if(isNum == 0) 
    	{
    		r = getNumber();
    		return r;
    	}
    }
    The problem is that no matter what the user enters, it thinks its not a number even if it is. [edit - just made one change, fixes one problem, but still doesn't work]

    Oh and if their a much easier way of doing this that'd be great too.

    Thanks.
    Last edited by Captain Penguin; 09-26-2002 at 09:57 AM.

  2. #2
    Seeking motivation... endo's Avatar
    Join Date
    May 2002
    Posts
    537
    Right check out this program I did for someone the other day. It takes a user number and then does some stuff to it and prints the answer. The only problem I have with it is that entering a letter makes it enter an infinite loop, prompting for input continuously. The reason I've posted it (most of my non-working programs are quickly deleted ) is that it tries to do the same error checking as yours but in a totally different way. not sure if its better or worse, but if anyone can tell me how to stop it entering the loop I'd be greatful as well

    Code:
    #include <iostream.h>
    
    void main( int argc, char* argv )
    {
    
    	int input;
    	int failed = 0;
    
    	do
    	{
    		if( failed )
    		{
    			failed = 0;
    			cin.clear( );
    		}
    
    		cout << "Enter an integer between 0 and 9999" << endl;
    		cin >> input;
    		failed = cin.fail( );
    	}while( input < 0 || input > 9999 || failed );
    
    	cout << "Your number is : " << input << endl << endl;
    
    	int firstDigit = input/1000;
    	int secondDigit = (input%1000)/100;
    	int thirdDigit = (input%100)/10;
    	int fourthDigit = (input%10);
    
    	cout << "The first digit is : " << firstDigit << endl;
    	cout << "The second digit is : " << secondDigit << endl;
    	cout << "The third digit is : " << thirdDigit << endl;
    	cout << "The fourth digit is : " << fourthDigit << endl << endl;
    	cout << "The sum of the digits is : " 
                        << firstDigit + secondDigit + thirdDigit + fourthDigit << endl << endl;
    
    	int i;
    	for( i = 2; i <= 9; i++ )
    	{
    		if( input%i == 0 )
    		{
    			cout << "Your number is divisible by " << i << endl;
    		}
    	}
    }
    Couldn't think of anything interesting, cool or funny - sorry.

  3. #3
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765
    > but if anyone can tell me how to stop it entering the loop I'd be greatful as well
    Code:
    #include <iostream.h>
    #include <stdio.h>
    
    void main( int argc, char* argv )
    {
    
    	int input;
    	int failed = 0;
    
    	do
    	{
    		if( failed )
    		{
    			failed = 0;
    			cin.clear( );
    		}
    
    		cout << "Enter an integer between 0 and 9999" << endl;
    		if ( scanf("%d", &input) == 0 )
    		{
    			cout << endl << "Invalid data" << endl;
    			return 1;
    		}
    		failed = cin.fail( );
    	}while( input < 0 || input > 9999 || failed );
    
    	cout << "Your number is : " << input << endl << endl;
    
    	int firstDigit = input/1000;
    	int secondDigit = (input%1000)/100;
    	int thirdDigit = (input%100)/10;
    	int fourthDigit = (input%10);
    
    	cout << "The first digit is : " << firstDigit << endl;
    	cout << "The second digit is : " << secondDigit << endl;
    	cout << "The third digit is : " << thirdDigit << endl;
    	cout << "The fourth digit is : " << fourthDigit << endl << endl;
    	cout << "The sum of the digits is : " 
                        << firstDigit + secondDigit + thirdDigit + fourthDigit << endl << endl;
    
    	int i;
    	for( i = 2; i <= 9; i++ )
    	{
    		if( input%i == 0 )
    		{
    			cout << "Your number is divisible by " << i << endl;
    		}
    	}
    }
    The world is waiting. I must leave you now.

  4. #4
    Registered User
    Join Date
    Aug 2002
    Location
    Hermosa Beach, CA
    Posts
    446
    why not something like:

    Code:
    #include <string>
    #include <iostream>
    #include <sstream>
    using namespace std;
    
    bool prompt(int& i)
    {
        string s;
        stringstream ss;
    
         // read line of input into string:
         getline(cin, s);
         
         // put the string into a stringstream
         ss<<s;
    
         // read an int out of the stringstream: 
         // should read thru the last numeric char in stream
         ss>>i;
         
         // unable to read an int 
         if ( ss.fail() )
         {
              ss>>s;
              cout<<"Error in input: "<<s<<endl;
              return false;
         }
         
           // check for additional chars
          ss>>s;
          if ( s.length() != 0 )
                 cout<<"Warning: Extra chars ignored";
    
           return true;
    }
    The crows maintain that a single crow could destroy the heavens. Doubtless this is so. But it proves nothing against the heavens, for the heavens signify simply: the impossibility of crows.

  5. #5
    Registered User
    Join Date
    Aug 2001
    Posts
    244
    Cool, but can seomeone determine why my code doesn't work? I spent a long time troubleshooting that and it would stink if it forever remained a mystery. What its SUPPOSED to do is take a char array, go through each value of the array and store it in an int value and compare the ascii codes to make sure it is a numeral. if it isn't, it calls the function to get the value again. if it is, it converts the string to an integer and checks to make sure it isn't larger than 36766 or something like that. If it is, it gets the input again.

    But it doesn't work!

  6. #6
    Registered User
    Join Date
    Aug 2001
    Posts
    244
    if anyone cares, I finally figured out the problem.. changed checkNumeric to as follows

    Code:
    int checkNumeric(char a[6])
    {
    	int x;
    	int i;
    	int marker;
    	int length;
    
    	length = strlen(a);
    	
    	for(x = 0; x < length; x++)
    	{
    		i = a[x];
    		if((i < 48 || i > 57) && i != '\0')
    		{
    			marker = 1;
    		}
    	}
    	if(marker == 1) return 0;
    	if(marker != 1) return 1;
    }
    the string length portion is the new part.

  7. #7
    Seeking motivation... endo's Avatar
    Join Date
    May 2002
    Posts
    537
    Originally posted by Shadow
    > but if anyone can tell me how to stop it entering the loop I'd be greatful as well
    Code:
    #include <iostream.h>
    #include <stdio.h>
    
    void main( int argc, char* argv )
    {
    
    	int input;
    	int failed = 0;
    
    	do
    	{
    		if( failed )
    		{
    			failed = 0;
    			cin.clear( );
    		}
    
    		cout << "Enter an integer between 0 and 9999" << endl;
    		if ( scanf("%d", &input) == 0 )
    		{
    			cout << endl << "Invalid data" << endl;
    			return 1;
    		}
    		failed = cin.fail( );
    	}while( input < 0 || input > 9999 || failed );
    
    	cout << "Your number is : " << input << endl << endl;
    
    	int firstDigit = input/1000;
    	int secondDigit = (input%1000)/100;
    	int thirdDigit = (input%100)/10;
    	int fourthDigit = (input%10);
    
    	cout << "The first digit is : " << firstDigit << endl;
    	cout << "The second digit is : " << secondDigit << endl;
    	cout << "The third digit is : " << thirdDigit << endl;
    	cout << "The fourth digit is : " << fourthDigit << endl << endl;
    	cout << "The sum of the digits is : " 
                        << firstDigit + secondDigit + thirdDigit + fourthDigit << endl << endl;
    
    	int i;
    	for( i = 2; i <= 9; i++ )
    	{
    		if( input%i == 0 )
    		{
    			cout << "Your number is divisible by " << i << endl;
    		}
    	}
    }

    I was actually hoping for something which would prompt the user again if they entered a character, I just want to stop it entering an infinite loop.
    Couldn't think of anything interesting, cool or funny - sorry.

  8. #8
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765
    Create a loop for your program based on invalid input then.
    The world is waiting. I must leave you now.

  9. #9
    Seeking motivation... endo's Avatar
    Join Date
    May 2002
    Posts
    537
    Thats what I was trying. I was checking the state of cin.fail() and if it becomes 1 then a char has been entered and the loop must repeat. I also have to clear cin so that it doesn't enter an infinite loop because the first entry is incorrect.

    ps sorry to captain penguin, I seem to have hijacked your thread
    Couldn't think of anything interesting, cool or funny - sorry.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. No clue how to make a code to solve problems!
    By ctnzn in forum C Programming
    Replies: 8
    Last Post: 10-16-2008, 02:59 AM
  2. C Pointers Problems
    By mhelal in forum C Programming
    Replies: 8
    Last Post: 01-10-2007, 06:35 AM
  3. String Manipulation problems -_-
    By Astra in forum C Programming
    Replies: 5
    Last Post: 12-13-2006, 05:48 PM
  4. Rendering problems (DirectX?)
    By OnionKnight in forum Tech Board
    Replies: 0
    Last Post: 08-17-2006, 12:17 PM
  5. contest problems on my site
    By DavidP in forum Contests Board
    Replies: 4
    Last Post: 01-10-2004, 09:19 PM