CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Threaded View

  1. #1
    Join Date
    Feb 2011
    Posts
    2

    Try Catch Question

    I need to use try...catch so if the user enters more then 10 digits

    Code:
    #include <iostream>
    
    int main( int argc, const char*argv )
    {
    	int numInput = 0, avgCount = 0, totalCount = 0;  
    	float average = 0;
    
    	while( numInput != 99 )
    	{
    		
    		std::cout << "Enter a Number (Max 10 digits)(99 to Exit): ";
    		std::cin >> numInput;
    
    		if ( numInput != 99 )
    		{
    			totalCount += numInput;
    			avgCount += 1;
    		}
    	}
    	
    	try
    	{
    		if( avgCount == 0 )
    		{
    			throw 0;
    		}
    		average = totalCount / avgCount;
    	}
    	catch(...)
    	{
    		std::cout << "Divide by Zero error" << std::endl;
    		totalCount = 0;
    	}
    	
    	std::cout << "Average of all Inputs: " << average << std::endl;
    
    	system("pause");
    	return 0;
    }
    Last edited by Marc G; February 17th, 2011 at 09:53 AM. Reason: added code tags

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured