what am i doing wrong?

This is a discussion on what am i doing wrong? within the C++ Programming forums, part of the General Programming Boards category; This program is suppose to : Prompt the user to enter any number of integers. Keep prompting the user to ...

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    41

    what am i doing wrong?

    This program is suppose to :

    Prompt the user to enter any number of integers. Keep prompting the user to input integers until the input is 0, which is the signal to stop entering numbers. After the inputs are finished, print out the following information: how many positive numbers were entered, how many negative numbers were entered, the sum of the numbers entered, and the decimal average of the numbers entered.

    Sample run: (user input underlined)

    Input integer (0 to stop) 12
    Input integer (0 to stop) 4
    Input integer (0 to stop) -1
    Input integer (0 to stop) -5
    Input integer (0 to stop) 18
    Input integer (0 to stop) 0
    # of positives = 3
    # of negatives = 2
    Total = 28
    Average = 5.6


    [code]


    #include <iostream>

    using std::cout;
    using std::cin;
    using std::endl;

    int main ()

    {

    int integer;
    int positives = 1;
    int negatives = 1;
    int total = 0;
    double average = 0;

    cout << "Input integer (0 to stop): " ;
    cin >> integer;

    while ( integer != 0)
    {
    total = total + integer;
    average = ;// how would i compute the average?
    cout << "Input integer (0 to stop): " ;
    cin >> integer;

    }

    if ( integer >= 1)
    positives += 1;
    cout << " # of positives = " << positives << endl;

    if (integer < 1)
    negatives += 1
    cout << " # of negatives = "<< negatives << endl;


    cout << "total = " << total << endl;
    cout << "average = " << average << endl;


    return 0;

    }

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,655
    Well since the comparison of positive/negative occurs OUTSIDE of the loop, how do you expect to get an accurate count?

    An average is the total divided by the amount of numbers gathered. So count how many times you've looped.

  3. #3
    Registered User
    Join Date
    Oct 2002
    Posts
    41

    one more question

    If I don't know how many numbers that I will use, how will I know what number to divide the average by? The loop is infinite!

  4. #4
    Semi n00b Mithoric's Avatar
    Join Date
    Jun 2003
    Posts
    293
    // how would i compute the average?
    Average is just the total sum divided by number of entries.

    Example series:
    1,2,3,4,5

    total: 15
    average: total/5 = 3

  5. #5
    Semi n00b Mithoric's Avatar
    Join Date
    Jun 2003
    Posts
    293
    Code:
    int count;
    while ( integer != 0)
    {
    total = total + integer;
    average = ;// how would i compute the average?
    cout << "Input integer (0 to stop): " ;
    cin >> integer;
    count++;
    }

  6. #6
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,655
    Originally posted by Mithoric
    Code:
    int count;
    while ( integer != 0)
    {
    total = total + integer;
    average = ;// how would i compute the average?
    cout << "Input integer (0 to stop): " ;
    cin >> integer;
    count++;
    }


    Don't forget to initialize count to zero.

    Finally, while counting activities should occur INSIDE the loop, averaging should obviously be done OUTSIDE of it.

  7. #7
    Semi n00b Mithoric's Avatar
    Join Date
    Jun 2003
    Posts
    293
    :| didn't even see that the average (or where it would be calculated) was in the loop.

  8. #8
    Registered User
    Join Date
    Oct 2002
    Posts
    41

    i did it myself

    I figured out how to get the average and my program is working great. Thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 07-15-2004, 03:30 PM
  2. Debugging-Looking in the wrong places
    By JaWiB in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 11-03-2003, 09:50 PM
  3. Confused: What is wrong with void??
    By Machewy in forum C++ Programming
    Replies: 19
    Last Post: 04-15-2003, 12:40 PM
  4. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 11:01 AM
  5. Whats wrong?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 07-14-2002, 01:04 PM

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