Thread: Pfft... I feel like a noob... help?

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    14

    Pfft... I feel like a noob... help?

    You know how the little programs you do for fun sometimes just leave you tearing your hair out? Here's my broken program - I think it's something to do with undefined floats, but... well you'll have to look. It totals things as 5.2412 exponential to some number, or 2.4359 exponential again and so on... grrr

    Code:
    //program to compute the average of n numbers
    #include <iostream>
    
    using namespace std;
    
    int main(int argc, char *argv[])
    {
       //greeter
       cout << "*****************Compute Averages**********************\n"
            << "**********input a zero to total them all up**********\n";
    
       int num[100], count, total;
       int done = 1;
       int total_count = 0;  //num[100] is array of the numbers, count current array index
       float average;    //the average
    
       for(done = 1; done == 1; total_count++)
       {
          cout << "Number: ";
          cin >> num[total_count];
          //if the number does not eq zero, continue
          if(num[total_count] == 0)
          {
             total_count--;
             done = 0;
          }
             
       }
    
       for(count = 0; count <= total_count; count++)
          total += num[count];
    
       average = total/total_count;
    
       cout << "Average: " << average << "\n";
    
       return(0);
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Well total isn't initialised before you start adding to it.

    Maximise the warnings your compiler can generate.
    Code:
    $ g++ -W -Wall -ansi -pedantic -O2 foo.cpp
    foo.cpp:6: warning: unused parameter ‘argc’
    foo.cpp:6: warning: unused parameter ‘argv’
    foo.cpp: In function ‘int main(int, char**)’:
    foo.cpp:12: warning: ‘total’ may be used uninitialized in this function

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    14
    Cheers, I'll remember to make g++ more verbose from now on!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Noob to programming need help with a project
    By Wheelsonbus in forum C Programming
    Replies: 6
    Last Post: 02-25-2009, 03:46 AM
  2. how do you feel...
    By ILoveVectors in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 08-16-2005, 05:47 AM
  3. noob needs help!!!!:(
    By tykenfitz in forum C Programming
    Replies: 1
    Last Post: 07-10-2005, 08:49 AM
  4. simple noob question: HWND, HDC etc.
    By freedik in forum Windows Programming
    Replies: 12
    Last Post: 08-19-2003, 03:59 AM
  5. Feel Guilty..
    By vasanth in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 01-22-2003, 03:00 PM