Thread: is my loop badly set up?

  1. #1
    C++ beginner
    Join Date
    Jun 2004
    Posts
    66

    is my loop badly set up?

    With this simple program I am trying to make -- I need to use a while loop to take input from the user until they don't want to input anymore and then calculate the average of what they are entering(they are entering numbers).

    So far - I'm having trouble getting my loop to work properly... If I'm not mistaken I think my while loop is crappily set up, but I'm not sure how to improve it... So what I'm asking you all - is if there is a better way to set up this loop - or if my seemingly easy way out works well enough!

    The code works fine - I'm just thinking that my loop is badly set up, is it?

    Code:
    #include <cstdlib>
    #include <iostream>
    #include <iomanip>
    
    using namespace std;
    
    int main()
    {
        
       float number = 0;
       int numbers = 0;
       float sum = 0;
       float average = 0;
       char answer = 'y';
       
        while(answer == 'y'){
                    
                     cout <<"Enter a number: ";
                     cin >> number;
                     numbers++;
                     
                     sum += number;
                     
                     cout <<" Do you want to input another number? (y/n) ";
                     cin >> answer;
                     answer = tolower(answer);
                     
                     
                     }
                     
        
        average = sum / numbers;
        cout <<"\nThe average of all the numbers you entered is: ";
        cout << setprecision(3) << average;
        cout << endl;
                     
                     
        
        
        system("PAUSE");
        return EXIT_SUCCESS;
    }
    Oh my goodness.

  2. #2
    Registered User OnionKnight's Avatar
    Join Date
    Jan 2005
    Posts
    555
    No not really. The indentation might be a little exaggerated though and it doesn't care if a user enters something different than 'y' or 'n'.

  3. #3
    Slave MadCow257's Avatar
    Join Date
    Jan 2005
    Posts
    735
    This might apply
    http://faq.cprogramming.com/cgi-bin/...&id=1043284392

    And the system call is bad

  4. #4
    C++ beginner
    Join Date
    Jun 2004
    Posts
    66
    Quote Originally Posted by MadCow257
    This might apply
    http://faq.cprogramming.com/cgi-bin/...&id=1043284392

    And the system call is bad

    Nevermind the system call - as I won't be sharing this with anyone buy myself.
    Oh my goodness.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 12-01-2008, 10:09 AM
  2. Need some help...
    By darkconvoy in forum C Programming
    Replies: 32
    Last Post: 04-29-2008, 03:33 PM
  3. Personal Program that is making me go wtf?
    By Submeg in forum C Programming
    Replies: 20
    Last Post: 06-27-2006, 12:13 AM
  4. OpenGL Window
    By Morgul in forum Game Programming
    Replies: 1
    Last Post: 05-15-2005, 12:34 PM
  5. OpenGL and Windows
    By sean345 in forum Game Programming
    Replies: 5
    Last Post: 06-24-2002, 10:14 PM