Thread: annoying bugs on working code

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    1

    annoying bugs on working code

    hey, im new to the whole programming scene and i made this code to find the mean avrage of things. problem is i cannot get it to do more than three numbers. i will post the code and if you can make any changes to hellp and just leave a post id be more than grateful. thanks in advance.
    sam

    code;
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
            int amount_of_numbers, average, n1, n2, n3, n4, n5, n6, n7, n8, n9, n10;
    
            cout<<"hello, this program will identify the mean average of a group of numbers that you will input"<<endl;
            cin.get();
    
            cout<<"please input amount of numbers in group (min - 3, max - 10).\n";
            cin >> amount_of_numbers;
    
            if (amount_of_numbers >= 3) {
                cout<<"please input first number.\n";
                cin >> n1;
    
                cout<<"please input second number.\n";
                cin >> n2;
    
                cout<<"please input last number.\n";
                cin >> n3;
    
                average = n1+n2+n3/amount_of_numbers;
    
                cout<<"the average mean of this group of numbers is:";
                cout<< average;
            }
    
    
            else if (amount_of_numbers >= 4) {
                cout<<"please input first number.\n";
                cin >> n1;
    
                cout<<"please input second number.\n";
                cin >> n2;
    
                cout<<"please input third number.\n";
                cin >> n3;
    
                cout<<"please input last number.\n";
                cin >> n4;
    
                average = n1+n2+n3+n4/amount_of_numbers;
    
                cout<<"the average mean of this group of numbers is:";
                cout<< average;
            }
    
            else if (amount_of_numbers >= 5)  {
                cout<<"please input first number.\n";
                cin >> n1;
    
                cout<<"please input second number.\n";
                cin >> n2;
    
                cout<<"please input third number.\n";
                cin >> n3;
    
                cout<<"please input fourth number.\n";
                cin >> n4;
    
                cout<<"please input last number.\n";
                cin >> n5;
    
                average = n1+n2+n3+n4+n5/amount_of_numbers;
    
                cout<<"the average mean of this group of numbers is:";
                cout<< average;
            }
    
            else if (amount_of_numbers = 6)  {
                cout<<"please input first number.\n";
                cin >> n1;
    
                cout<<"please input second number.\n";
                cin >> n2;
    
                cout<<"please input third number.\n";
                cin >> n3;
    
                cout<<"please input fourth number.\n";
                cin >> n4;
    
                cout<<"please input fifth number.\n";
                cin >> n5;
    
                cout<<"please input last number.\n";
                cin >> n6;
    
                average = n1+n2+n3+n4+n5+n6/amount_of_numbers;
    
                cout<<"the average mean of this group of numbers is:";
                cout<< average;
            }
    
                else if (amount_of_numbers >= 7)  {
                    cout<<"please input first number.\n";
                cin >> n1;
    
                cout<<"please input second number.\n";
                cin >> n2;
    
                cout<<"please input third number.\n";
                cin >> n3;
    
                cout<<"please input fourth number.\n";
                cin >> n4;
    
                cout<<"please input fifth number.\n";
                cin >> n5;
    
                cout<<"please input sixth number.\n";
                cin >> n6;
    
                cout<<"please input last number.\n";
                cin >> n7;
    
                average = n1+n2+n3+n4+n5+n6+n7/amount_of_numbers;
    
                cout<<"the average mean of this group of numbers is:";
                cout<< average;
                }
    
                else if (amount_of_numbers >= 8)  {
                    cout<<"please input first number.\n";
                cin >> n1;
    
                cout<<"please input second number.\n";
                cin >> n2;
    
                cout<<"please input third number.\n";
                cin >> n3;
    
                cout<<"please input fourth number.\n";
                cin >> n4;
    
                cout<<"please input fifth number.\n";
                cin >> n5;
    
                cout<<"please input sixth number.\n";
                cin >> n6;
    
                cout<<"please input seventh number.\n";
                cin >> n7;
    
                cout<<" please input last number.\n";
                cin >> n8;
    
                average = n1+n2+n3+n4+n5+n6+n7+n8/amount_of_numbers;
    
                cout<<"the average mean of this group of numbers is:";
                cout<< average;
                }
    
                else if (amount_of_numbers >= 8)  {
                    cout<<"please input first number.\n";
                cin >> n1;
    
                cout<<"please input second number.\n";
                cin >> n2;
    
                cout<<"please input third number.\n";
                cin >> n3;
    
                cout<<"please input fourth number.\n";
                cin >> n4;
    
                cout<<"please input fifth number.\n";
                cin >> n5;
    
                cout<<"please input sixth number.\n";
                cin >> n6;
    
                cout<<"please input seventh number.\n";
                cin >> n7;
    
                cout<<"please input eighth number.\n";
                cin >> n8;
    
                cout<<"please input ninth number.\n";
                cin >> n9;
    
                average = n1+n2+n3+n4+n5+n6+n7+n8+n9/amount_of_numbers;
    
                cout<<"the average mean of this group of numbers is:";
                cout<< average;
                  }
    
                else if (amount_of_numbers >= 8)  {
                    cout<<"please input first number.\n";
                cin >> n1;
    
                cout<<"please input second number.\n";
                cin >> n2;
    
                cout<<"please input third number.\n";
                cin >> n3;
    
                cout<<"please input fourth number.\n";
                cin >> n4;
    
                cout<<"please input fifth number.\n";
                cin >> n5;
    
                cout<<"please input sixth number.\n";
                cin >> n6;
    
                cout<<"please input seventh number.\n";
                cin >> n7;
    
                cout<<"please input eighth number.\n";
                cin >> n8;
    
                cout<<"please input ninth number.\n";
                cin >> n9;
    
                cout<<"please input thenth number.\n";
                cin >> n10;
    
                average = n1+n2+n3+n4+n5+n6+n7+n8+n9+n10/amount_of_numbers;
    
                cout<<"the average mean of this group of numbers is:";
                cout<< average;
                     }
    }

  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    250
    1. Your first if statement includes the necessary conditions for all subsequent elseif statements, so these will never get executed.

    2. This is definitely not the way you want to implement this kind of logic. Get the number of numbers, and *loop* through all of them. No need for if statements. You need just a single loop, which can handle any number of numbers.
    iMalc: Your compiler doesn't accept misspellings and bad syntax, so why should we?
    justin777: I have no idea what you are talking about sorry, I use a laptop and there is no ascii eject or something

  3. #3
    Registered User
    Join Date
    Dec 2009
    Posts
    120
    Wow, that sure is a lot of code to do a simple task! Have you learned about loops yet? You can loop the output and input so you won't need the if statements and other code.

    Check this out

    Code:
     int nums, i, number, total = 0;
     float average;
        
               cout << "How many numbers would you like to compute the average of?\n";
               cin >> nums;
        
            for(i = 1; i <= nums; i++)
             {
              cout << "Enter number\n";
              cin >> number;
              total = total + number;
              }
              
              average = total / nums;
              
              cout << average << endl;
    As for your code, if the user selects 3 for the amount of numbers the it shouldn't be >=3, it should be ==3, or "equal to" 3.
    Last edited by nick753; 11-20-2010 at 06:25 PM. Reason: Had to indent properly so no one would throw a fit.

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    250
    Quote Originally Posted by nick753 View Post
    Wow, that sure is a lot of code to do a simple task! Have you learned about loops yet? You can loop the output and input so you won't need the if statements and other code.

    Check this out

    Code:
     int nums, i, number, total = 0;
        float average;
        
        cout << "How many numbers would you like to compute the average of?\n";
        cin >> nums;
        
        for(i = 1; i <= nums; i++)
        {
              cout << "Enter number\n";
              cin >> number;
              total = total + number;
              }
              
              average = total / nums;
              
              cout << average << endl;
    If you are going to dump solutions, at least bother to do your indentation properly.
    iMalc: Your compiler doesn't accept misspellings and bad syntax, so why should we?
    justin777: I have no idea what you are talking about sorry, I use a laptop and there is no ascii eject or something

  5. #5
    Registered User
    Join Date
    Dec 2009
    Posts
    120
    Quote Originally Posted by MWAAAHAAA View Post
    If you are going to dump solutions, at least bother to do your indentation properly.
    Jeez, you guys always find something to pick at. Who gives a flying .......... if I have a brace not indented while trying to help out a new programmer!!? The point was to show him how a loop could make his program much smaller. Sorry I am not as high and mighty as you all knowing programmers. God knows that you've never forgot to indent a brace on a sample program.

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Depending on the person you're posting the code for you have to consider that you may either be robbing the person of the satisfaction of working it out themselves, given just a little hint, or alternatively providing a solution that they will just use verbatim and wont bother to learn from. Well at least that's the two extremes...

    It simply needs to get through to everyone learning to program that indentation is very important. Lead by example.
    To those of us that understand the importance of indentation, when it's done badly it's like hunting through an alphabetised CD collection where most CDs are actually in a random wrong case. You don't have to be even moderately OCD for it to drive you nuts.

    If his solution actually requires asking "please input sixth number." rather than the "Enter number 6"? This can also be cleverly done without lots of duplicated code.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code for deleting a tree node not working
    By Mini in forum C Programming
    Replies: 2
    Last Post: 10-09-2010, 05:16 AM
  2. Replies: 3
    Last Post: 02-24-2009, 08:49 PM
  3. Help Code Is Not Working
    By srivatsan in forum C++ Programming
    Replies: 23
    Last Post: 11-12-2008, 12:30 PM
  4. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 04:17 PM
  5. cygwin -> unix , my code not working properly ;(
    By CyC|OpS in forum C Programming
    Replies: 4
    Last Post: 05-18-2002, 04:08 AM