Reputation: 27
Write a program to help me compute the sum. Your program should prompt me to enter numbers until I’m done (when I’m done I’ll enter -1). Then your program should print out the sum.
#include<iostream>
using namespace std;
int main()
{
double sum;
double number;
double total;
while (number !=-1)
{
cout<<"Input numbers: "<<endl;
cin>>number;
total+=number;
}
sum = total + number;
cout<<"The sum is "<<sum<<endl;
return 0;
}
For example I enter 9 and 9 then -1 I get 25. Would I just make double total =2; it works but don't really understand why it outputs 25. What is wrong in my code? I just started learning appreciate the help.
Upvotes: 0
Views: 127
Reputation: 5704
A few more remarks aside that you should initialise your variables.
I think you don't like to subtract 1 form your result. If you enter -1
it will be added to total
and than the while condition will be false
and the loop ends. Thus, your result is wrong.
You don't need total
and sum
.
Make the scope of your variables as small as possible. number
only needs the loop body as scope.
Don't use using namespace std
.
Formatting of your code matters. I always use a tool such as clang-format.
Here my refactoring of your code:
#include <iostream>
int main() {
double sum = 0;
while (true) {
std::cout << "Input numbers: " << std::endl;
double number = 0;
std::cin >> number;
if (number == -1) {
break;
}
sum += number;
}
std::cout << "The sum is " << sum << std::endl;
return 0;
}
Upvotes: 0
Reputation: 49
If you enter -1, then two things happens: It usees the value to calculate the new sum (minus one) and after the while - } the loop will quit and continue... This and the other comments will make your programm work.
Upvotes: -1
Reputation: 1299
C++ doesn't automatically initialize declared variables to zero, and since variables are just labels on memory locations, you get the numerical conversion of whatever bits happen to be stored in that location as the de facto initialization. So make a habit of always initializing variables:
double sum = 0;
double total = 0;
double number = 0;
Notice this is really only a problem with your total variable, as the others are all assigned values before being used in an operation (by cin and by =, the assignment operator). (Edit: missed the while loop using number! See comment) It's still (always) a good idea to initialize at declaration.
Upvotes: 4
Reputation:
sum
, number
, and total
are all uninitialized. That means they don't necessarily have 0
in them when they start.
To fix this, just set them to 0
:
double sum = 0;
double number = 0;
double total = 0;
Note: I should warn you that I don't recommend comparing a float/double to an integer like -1
as a general rule because it can behave unexpectedly. However, I think you should probably be fine in this case. But that isn't a guarantee.
Upvotes: 2
Reputation: 7374
What you are missing from C++, is the initialization.
If you use a variable before initializing it, your program will have undefined behavior. From there on anything can happen.
You can solve it simply by e.g.
double sum = 0;
Remember to do it for all of your variables.
You don't need this line too:
sum = total + number;
total
is already the sum
.
Upvotes: 2