Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 135,939 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,709 people online right now. Registration is fast and FREE... Join Now!




Simple loop to take values and calculate averages

 
Reply to this topicStart new topic

Simple loop to take values and calculate averages

theholygod
3 Oct, 2007 - 01:29 PM
Post #1

D.I.C Head
**

Joined: 5 Feb, 2006
Posts: 215


My Contributions
The other day i saw someone was attempting this task, and i thought it would be within my newbie abilities to give it a try.

The idea was to instruct the user to enter values and then -1 when finished, this would break the loop and calculate an average of the given values:

CODE

#include <iostream>
using namespace std;

int main()
{
    float count = 0;
    float temp = 0;
    float total = 0;
    float average = 0;
    
    cout << "Please enter a series of positive numbers \n";
    cout << "Enter -1 when finished \n";
    cout << "Press enter to continue \n";
    cin.get();
    
while (temp != -1)
{
    cout << "Number " << count + 1 << " : ";
    cin >> temp;
    cin.ignore();
    
    if (temp != -1);
    {
       total = total + temp;
       count = count + 1;    
    }
}
    cout << "You entered " << count + 1 << " values\n";
    cout << "The total was " << total << "\n";
    average = total / count;
    cout << "The average is " << average << "\n";
    
cin.get();
    
}


So, im sure to be poked by dreamincode's advanced c++ programmers as to technique and where i may be doing something a little incorrectly. This is very welcome!

However, the problem im having is that Count at the end is 2 higher than it should be and Total is adding the negative 1.

Im sure im making a really stupid mistake somewhere, but running that code through my head it seems to work fine. Any ideas?

Thanks
User is offlineProfile CardPM
+Quote Post

jjhaag
RE: Simple Loop To Take Values And Calculate Averages
3 Oct, 2007 - 01:49 PM
Post #2

me editor am smartastic
Group Icon

Joined: 18 Sep, 2007
Posts: 1,789



Thanked: 1 times
Dream Kudos: 775
Expert In: C,C++

My Contributions
it's close, but there is 1 typo that affects the actual result, and 1 that affects the output.

you have a stray semicolon after your if statement, so it essentially does nothing. the code in braces following the if statement executes regardless of your condition. so the number gets added to the total and the count is incremeneted; then, when the while loop starts over, the test condition is false, and the loop exits. so this gives you one extra number.

the other extra number comes from the fact that you output count + 1 rather than count. the total and average are correct, but you're a number one higher than the actual count. so that gives you one extra number again, which in total is two more than you were expecting.

hope that helps.

oh, and also - you probably want to initialize the floating point variables to 0.0 rather than 0. it won't affect things here, but it's a good practice to get into when dealing with literals in your code.

-jjh

This post has been edited by jjhaag: 3 Oct, 2007 - 01:50 PM
User is offlineProfile CardPM
+Quote Post

theholygod
RE: Simple Loop To Take Values And Calculate Averages
3 Oct, 2007 - 02:27 PM
Post #3

D.I.C Head
**

Joined: 5 Feb, 2006
Posts: 215


My Contributions
woo! it works, thanks!
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 08:39AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month