-
February 17th, 2011, 08:20 AM
#1
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
-
February 17th, 2011, 08:39 AM
#2
Re: Try Catch Question
Please use code tags for posting code.
I need to use try...catch so if the user enters more then 10 digits
If you have a question, it helps to actually ask the question.
-
February 17th, 2011, 09:56 AM
#3
Re: Try Catch Question
Your main signature is wrong. It should be:
Code:
int main(int argc, char** argv)
Don't use system("pause"), seePause Before Exiting a Console Application
Use code tags to post code.
So check the length of user input, read the user input as a string. Then you can count the number of characters and convert it to an int.
-
February 17th, 2011, 10:00 AM
#4
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|