Error Handling

Discussion in 'Computer Science & Culture' started by Gunjan, Sep 11, 2003.

Thread Status:
Not open for further replies.
  1. Gunjan Registered Member

    Messages:
    12
    here is some codes:
    Code:
    #include&ltiostream&gt
    using namespace std;
    
    int main(int argc,char *argv[]){
    	int ReturnCode=0;
    
    	float Dividend=0;
    	cout&lt&lt"Dividend:";
    	cin&gt&gtDividend;
    	if(!cin.fail()){
    		float Divisor=1;
    		cout&lt&lt"Divisor:";
    		cin&gt&gtDivisor;
    
    		float Result=(Dividend/Divisor);
    		cout&lt&ltResult&lt&ltendl;
    	}
    	else{
    		cerr&lt&lt"Input Error,not a number"&lt&ltendl;
    		cin.clear();
    		char BadInput[80];
    		cin&gt&gtBadInput;
    		ReturnCode=1;
    	}
    		char StopCharacter;
    		cout&lt&ltendl&lt&lt"Press a key and \"Enter\":";
    		cin&gt&gtStopCharacter;
    		
    		return ReturnCode;
    
    }
    
    
    Why do I need these variables:ReturnCode,BadInput[80],StopCharacter?
    pls suggest.

    thanks,
    Gunjan
     
  2. Google AdSense Guest Advertisement



    to hide all adverts.
  3. daktaklakpak God is irrelevant! Registered Senior Member

    Messages:
    710
    ReturnCode is for telling the caller what error the program has encountered. The return code is usually used by a script or batch file.

    BadInput[80] is a dummy buffer that trys to eat all the bad characters in the input stream. It will fail if the last input was more than 80 characters in a line.

    StopCharacter is a dummy variable that trys to eat the "any key". It's a way to pause the output until user press a key then enter. Useful for displaying the result until user decides it's time to move on.
     
  4. Google AdSense Guest Advertisement



    to hide all adverts.
Thread Status:
Not open for further replies.

Share This Page