+ 2

What's wrong with my code?

Help me please. #include <iostream> using namespace std; int main() { int sum = 0; // take input from the user cout << "Enter a number: "; cin >> number; while (number >= 0){ // add all positive numbers sum += number; // take input again if the number is positive cout << "Enter a number: "; cin >> number; } // display the sum cout<< "\nThe sum is " << sum << endl; return 0 }

29th Jan 2021, 12:31 PM
Hyacenth Luyas
Hyacenth Luyas - avatar
12 Antworten
+ 5
There's no syntactical errors other than the return 0 must end with semicolon ...But seeing your commenting on the code , ig your program lacks logic .It would be helpful if you have shared the question ...
29th Jan 2021, 12:35 PM
Alphin K Sajan
Alphin K Sajan - avatar
+ 5
Hyacenth Luyas Read this lesson.This is a self learning platform , So ig you can make it yourself : https://www.sololearn.com/learn/CPlusPlus/1617/?ref=app
29th Jan 2021, 12:56 PM
Alphin K Sajan
Alphin K Sajan - avatar
+ 3
I don't think you have declared the number variable.
29th Jan 2021, 12:45 PM
Sonic
Sonic - avatar
+ 2
Mygod I didn't see the last part... Anyway thank you for your help.
29th Jan 2021, 12:38 PM
Hyacenth Luyas
Hyacenth Luyas - avatar
+ 2
I think it would have been better to use a do-while loop.
29th Jan 2021, 12:46 PM
Sonic
Sonic - avatar
+ 2
Thanks for the info
29th Jan 2021, 1:09 PM
Hyacenth Luyas
Hyacenth Luyas - avatar
+ 1
Sonic teach me how please.
29th Jan 2021, 12:49 PM
Hyacenth Luyas
Hyacenth Luyas - avatar
+ 1
You don't declared any number variable.
30th Jan 2021, 11:24 PM
❤️😍Prerana😍❤️
❤️😍Prerana😍❤️ - avatar
0
Ohh from where I can see the two problems are firstly you should declare the variable ....like 0 is an integer....also return 0 must end with a semicolon...
30th Jan 2021, 12:15 PM
Arnav..
Arnav.. - avatar
0
Declaration of number variable is absent. Declare the "number" variable with valid data type first and then use in the code. 👍👍
30th Jan 2021, 4:08 PM
Shivesh Ratan
0
It will works.
30th Jan 2021, 11:24 PM
❤️😍Prerana😍❤️
❤️😍Prerana😍❤️ - avatar