-NaN problem

I'm trying to use a for loop to get the users input of numbers and add them together to get a total, however I'm having some trouble. No matter what i've tried, the program always returns the result of "-Nan".

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

#include <iostream>
using namespace std;

int main()
{
int counter;
float price, sub;

cout<<"Enter the number of books in the sale: ";
cin>>counter;

for(int i=1; i <=counter; i++)
{
cout<<"Enter price: ";
cin>>price;

sub = sub + price;
}

cout<<"Subtotal: $"<<sub<<endl;
return 0;
}
 

Any help is greatly appreciated



Last edited on
You don't seem to give sub an initial value. So it probably contains garbage.

Try:
 
float sub = 0.0f;
Topic archived. No new replies allowed.