what does exactly do

What is the output of the program below, if the input values are 4 11 6 17 ? would it be the sum of all those value? Which of the following values will be returned if main () is called?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
using namespace std;
int   main ()

{

int  t = 0, value = 0;

while (cin>>value)

t+= value;

cout << "the value of t is: " <<t<<endl;

return 0;

}
Last edited on
Do you want the user to enter a value, or have it execute with the value (11) you defined?

(When posting code you can [_code] insert code here [/_code] (without the _ ) so it gives lines numbers, and also makes it easier to read. It's also off to the right of the text box where it says "Format:", the symbol icon like "< >" is for displaying code.)
Last edited on
The console is waiting for you to give it input.

Once you enter something that causes cin to fail, it will break out of the while loop and print the value of t.

One way to make the while loop stop would be to enter a letter instead of a number.
Last edited on
so the value of any number will be zero right?
No, not quite.

You have two variables: t and value.
t is initially 0, and value is initially 11.
When you enter more numbers, t's value will increase by that number,
and value's value will be replaced by the entered number. Hope that makes sense.

For example:
If you enter 3, then 2, then 'x', it will print
the value of t is: 5
Last edited on
ok I see, so let say the value=0 What is the output of the program , if the input values are 4 11 6 17 ? would it be the sum of all those numbers?
Try it for yourself and see: http://cpp.sh/2qk2

The initial value of the "value" variable doesn't matter because it will be overwritten in the loop.
But yes it would be the sum.
Last edited on
Topic archived. No new replies allowed.