0

I entered only 1 number instead of 5 and got output as 5*number.. instead I should have got only the number . please clarify

using while loop topic.. entering 5 integers and summing the inputs.

1st Aug 2016, 2:14 PM
RB Vijey Shankar
RB Vijey Shankar - avatar
5 Réponses
+ 1
Please post your code. We cannot help without seeing where your problem is.
1st Aug 2016, 2:21 PM
Cohen Creber
Cohen Creber - avatar
+ 1
it is the tutorial itself... posting it for reference. #include <iostream> using namespace std; int main() { int num = 1; int number; int total = 0; while (num <= 5) { cin >> number; total += number; num++; } cout << total << endl; return 0; }
1st Aug 2016, 2:24 PM
RB Vijey Shankar
RB Vijey Shankar - avatar
+ 1
Where is your problem? You say you only entered one number, and got an output, however that while loop will not end until num > 5.
1st Aug 2016, 2:29 PM
Cohen Creber
Cohen Creber - avatar
+ 1
kindly execute it ... I entered 2 as my number and got output as 10. logically since I have entered only 1 number my answer should be 2 taking other inputs as 0 by default but it is taking the 1st number 5 times
1st Aug 2016, 2:32 PM
RB Vijey Shankar
RB Vijey Shankar - avatar
+ 1
If you are executing it from the Code Playground, I assume it is because you didn't enter enough inputs (it requires 5), so it will take the last input you entered and fill it in for the rest of the inputs. For example, if we entered: 1 2 We would get a result of 9, because 1+(4x2). This wouldn't happen on a proper IDE of course, nor if you run an actual executable. It's just how this app works I guess. I really do despise the input system on this app.
1st Aug 2016, 2:40 PM
Cohen Creber
Cohen Creber - avatar