jeudi 12 avril 2018

C++ : error with total+= in a while loop

I am new to programming and currently reading the book "C++ Primer Plus". This is a part of one of the exercises of a chapter. The outcome of this code was not what I expected,so I found alternatives and completed the exercise. Still curious though, why does the total+= doesn't just add the values from donations [1] to donations [9], which is still wrong, but it is the mistake i expected when i reviewed the code?

     int main()
   {    double total;
        int bigger=0;
        int values=0;
        double donation[10];

        cout<<"Enter 10 donation values(numbers only). ";
        cout<<"Enter first value: ";

        while (values<10 && cin>>donation[values] )
     {  ++values;
        if (values<10)
        {
        cout<<"Enter value no " <<(values+1)<<" :";
        total+=donation[values];
        }
     }
       double average=total/10;

Aucun commentaire:

Enregistrer un commentaire