Click here to Skip to main content
15,906,341 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a problem a bit. I don't know how should I put answer?

Here sample output:

a + b = 3
The value of c is: 5

please help me thanks

What I have tried:

int a = 1;
double b = 2;
int c;

Console.WriteLine("a + b = " + (a + b));
Console.WriteLine("The value of c is: " + c);
Posted
Updated 4-Sep-16 21:46pm

In the posted code, you never assign c, hence such code shouldn't even compile. If you are trying to store in c the result of the computation then do it:
c = (int) (a+b);
By the way: why are you mixing integer values with floating point ones?
 
Share this answer
 
The trouble is that you aren't assigning anything.
Think of a variable as a pocket in your shirt, which can hold an item : a pen, a pencil, a hankie.
Unless you explicitly put something in the pocket, you can't take it out!
So try doing the operation, but putting the result into the "pocket":
C#
int a = 1;
int b = 2;
int c = a + b;
Console.WriteLine("The value of {0} + {1} is {2}", a, b, c);
You shouldn't "mix types" unless you need to - it can cause some odd problems - so I changed the declaration of b from double to int to match a and c.
The final line is just a "tidy up" form or what you had: the curly brackets in the string will be replaced with the value from the list after it, using the number as a count: {0} will be replaced with the first value (in this case from the variable a), {1} will be replaced with the second value (in this case from the variable b), and so on.
Try it and see what happens!
 
Share this answer
 
Comments
bearforever99 6-Sep-16 22:29pm    
I got it so thanks. But i have another more questions for even now?
OriginalGriff 7-Sep-16 2:35am    
So ask another question - just post it as a new question so everybody gets to see it.
But...please, for your own sake - try thinking about your problem for a bit before you post a question. You will learn a lot more that way than being told "do this", and that will save you a lot of time in the future.

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