![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Hobbyist Programmer
|
average = 0
I borrowed a C++ book and i went to the beginning to revise Control Structures...
This is a code using loops. #include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
int i=0;
int num;
int total=0;
double average;
while (num != -1)
{
cout << "Enter the number -1 to break" << endl;
cin >> num;
total = total+num;
i++;
}
if (total = 0)
exit(1);
else
{
average = total / i;
cout << average << endl;
}
cin.ignore();
cin.get();
return 0;
}This programme is supposed to take numbers until (num==-1), after that, it will add up all the numbers and find the average. But the problem is that the average is always nought... .Anyway, i spent a long time looking for the problem, yet i'm sure its just a silly thing i missed. Thank you.
__________________
From the bottom of the stone steps... ...i'm calling still. |
|
|
|
|
|
#2 |
|
Expert Programmer
Join Date: Aug 2005
Location: Rotterdam, the Netherlands
Posts: 942
Rep Power: 4
![]() |
if (total = 0)
exit(1);You set total to 0 there, so your answer will always be 0. Use this code instead: if (total == 0) return 1; Secondly, you should cast i and total to doubles before dividing: average = (double) total / (double) i; And thirdly, your code adds -1, your loop should terminate before adding it: while (1)
{
cout << "Enter the number -1 to break" << endl;
cin >> num;
if(num == -1)
break;
total = total + num;
i++;
}I think that will do it. |
|
|
|
|
|
#3 |
|
Hobbyist Programmer
Join Date: Oct 2005
Posts: 134
Rep Power: 4
![]() |
EDIT: I made the same three fixes as Polyphemus_, but he beat me to it.
|
|
|
|
|
|
#4 |
|
Hobbyist Programmer
|
Thanks!
That worked indeed!Anyway, in the "Secondly" part, that is called typecasting right? Is it the same as : static_cast <double>(total) Thanks again!
__________________
From the bottom of the stone steps... ...i'm calling still. |
|
|
|
|
|
#5 | |
|
Expert Programmer
Join Date: Aug 2005
Location: Rotterdam, the Netherlands
Posts: 942
Rep Power: 4
![]() |
Quote:
![]() Anytime, darkone.. and yeah, I believe that's the same as static_cast. Using just (double) will safe typing, though . |
|
|
|
|
|
|
#6 | |
|
Game engine designer
Join Date: May 2005
Location: Sweden
Posts: 314
Rep Power: 4
![]() |
Quote:
static_cast<double>(x); // The C++ way (double)x; // C cast C++ also have more casts than C, that only got one. These are const_cast reinterpret_cast dynamic_cast |
|
|
|
|
|
|
#7 |
|
Hobbyist
Join Date: Sep 2005
Posts: 266
Rep Power: 4
![]() |
Perhaps it would also be a good idea to initialize 'num' with a value other than -1 before the while loop?
|
|
|
|
|
|
#8 |
|
Hobbyist Programmer
|
Thanks Poly, Klarre and Kaja, thanks, i really need to improve! :o
__________________
From the bottom of the stone steps... ...i'm calling still. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|