int num = 1; int number; int total = 0; while (num <= 5) {   cin >> number; total += number;   num++; } cout << total << endl; | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

int num = 1; int number; int total = 0; while (num <= 5) {   cin >> number; total += number;   num++; } cout << total << endl;

why is total=0😐?

3rd Dec 2017, 3:31 PM
Mitra
Mitra - avatar
5 Answers
+ 3
First, there is a variable named number which is undefined, in line 2 You have to give number a value in order to add it in total Otherwise, everytime total will result in 0
3rd Dec 2017, 4:36 PM
#RahulVerma
#RahulVerma - avatar
+ 1
ok
3rd Dec 2017, 3:57 PM
Mitra
Mitra - avatar
+ 1
Your code is true. In this app, you must type all of inputs(here ints) that the code wants by pressing enter key between.
7th Dec 2017, 8:23 PM
Mahdi Ghahramani
Mahdi Ghahramani - avatar
0
why is it nessesery?
3rd Dec 2017, 3:46 PM
Mitra
Mitra - avatar
0
it's not?
3rd Dec 2017, 3:54 PM
Mitra
Mitra - avatar