Click here to Skip to main content
15,901,666 members
Please Sign up or sign in to vote.
1.25/5 (4 votes)
See more:
Hi there
I am developing a c# application which involves adding up numbers eg

1+2+3+4=Answer
When one of the numbers is missing, application fails to sum up but instead it throws an exception.

How can i continue adding in case some numbers are missing, and i just add the available numbers

In case if all the numbers are missing, i want the answer to evaluate to a string char like X.
Thanks in advance
Posted
Comments
Sergey Alexandrovich Kryukov 14-Jul-11 14:41pm    
Not clear missing from what and why all that. Also, there is no question. What's the problem?
--SA
[no name] 14-Jul-11 14:43pm    
What do you mean by missing? What data type do you use to store the numbers?
RaviRanjanKr 14-Jul-11 15:01pm    
Please be more specific while asking question. we need more clear information regarding to your question. :)
wizardzz 14-Jul-11 16:54pm    
It does sound like the code would be simple, so why not post what you have so far?

You should probably load those values into an array or list, or any sort of collection and add by looping through them, that way the actual number of variables is irrelevant. To get the answer in a string format just use .ToString() on it.
 
Share this answer
 
Give this a shot...

MIDL
string tmp = "1+2+3+5";
int total = tmp.Split('+').ToList().Sum(item => Convert.ToInt32(item));
Console.WriteLine(tmp + " = " + total.ToString());
 
Share this answer
 
Very good question... Hahaha. Come on give us some code! Sounds like it must be some trivial bug, missunderstanding or desing mistake.
So let's start guessing:
I think you read the numbers from some controls (textbox?) where the user can enter numbers.
You try to convert those numbers from the textbox strings to prepare them for your "calculation".
If one of the textboxes was left empty you can not convert/parse it to a number, this is where the exception is thrown.

Good guesses?
Anyway, please improve your question (show code). If you talk about exceptions allways tell us which one!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900