Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jan 11th, 2006, 1:14 PM   #1
darkone916
Hobbyist Programmer
 
darkone916's Avatar
 
Join Date: Jul 2005
Location: Oman
Posts: 125
Rep Power: 4 darkone916 is on a distinguished road
Send a message via MSN to darkone916
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.
darkone916 is offline   Reply With Quote
Old Jan 11th, 2006, 1:26 PM   #2
Polyphemus_
Expert Programmer
 
Polyphemus_'s Avatar
 
Join Date: Aug 2005
Location: Rotterdam, the Netherlands
Posts: 942
Rep Power: 4 Polyphemus_ is on a distinguished road
    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.
Polyphemus_ is offline   Reply With Quote
Old Jan 11th, 2006, 1:28 PM   #3
Kaja Fumei
Hobbyist Programmer
 
Join Date: Oct 2005
Posts: 134
Rep Power: 4 Kaja Fumei is on a distinguished road
EDIT: I made the same three fixes as Polyphemus_, but he beat me to it.
Kaja Fumei is offline   Reply With Quote
Old Jan 11th, 2006, 1:33 PM   #4
darkone916
Hobbyist Programmer
 
darkone916's Avatar
 
Join Date: Jul 2005
Location: Oman
Posts: 125
Rep Power: 4 darkone916 is on a distinguished road
Send a message via MSN to darkone916
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.
darkone916 is offline   Reply With Quote
Old Jan 11th, 2006, 2:05 PM   #5
Polyphemus_
Expert Programmer
 
Polyphemus_'s Avatar
 
Join Date: Aug 2005
Location: Rotterdam, the Netherlands
Posts: 942
Rep Power: 4 Polyphemus_ is on a distinguished road
Quote:
Originally Posted by Kaja Fumei
EDIT: I made the same three fixes as Polyphemus_, but he beat me to it.
Lol, only two minutes diffence...

Anytime, darkone.. and yeah, I believe that's the same as static_cast. Using just (double) will safe typing, though .
Polyphemus_ is offline   Reply With Quote
Old Jan 11th, 2006, 2:08 PM   #6
Klarre
Game engine designer
 
Klarre's Avatar
 
Join Date: May 2005
Location: Sweden
Posts: 314
Rep Power: 4 Klarre is on a distinguished road
Quote:
Originally Posted by darkone916
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!
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
Klarre is offline   Reply With Quote
Old Jan 11th, 2006, 2:09 PM   #7
Cache
Hobbyist
 
Join Date: Sep 2005
Posts: 266
Rep Power: 4 Cache is on a distinguished road
Perhaps it would also be a good idea to initialize 'num' with a value other than -1 before the while loop?
Cache is offline   Reply With Quote
Old Jan 11th, 2006, 2:11 PM   #8
darkone916
Hobbyist Programmer
 
darkone916's Avatar
 
Join Date: Jul 2005
Location: Oman
Posts: 125
Rep Power: 4 darkone916 is on a distinguished road
Send a message via MSN to darkone916
Thanks Poly, Klarre and Kaja, thanks, i really need to improve! :o
__________________
From the bottom of the stone steps...
...i'm calling still.
darkone916 is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 12:47 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC