# Thread: annoying bugs on working code

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) {
cin >> n1;

cin >> n2;

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) {
cin >> n1;

cin >> n2;

cin >> n3;

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)  {
cin >> n1;

cin >> n2;

cin >> n3;

cin >> n4;

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)  {
cin >> n1;

cin >> n2;

cin >> n3;

cin >> n4;

cin >> n5;

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)  {
cin >> n1;

cin >> n2;

cin >> n3;

cin >> n4;

cin >> n5;

cin >> n6;

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)  {
cin >> n1;

cin >> n2;

cin >> n3;

cin >> n4;

cin >> n5;

cin >> n6;

cin >> n7;

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)  {
cin >> n1;

cin >> n2;

cin >> n3;

cin >> n4;

cin >> n5;

cin >> n6;

cin >> n7;

cin >> n8;

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)  {
cin >> n1;

cin >> n2;

cin >> n3;

cin >> n4;

cin >> n5;

cin >> n6;

cin >> n7;

cin >> n8;

cin >> n9;

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. 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.

3. 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.

4. Originally Posted by nick753
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.

5. Originally Posted by MWAAAHAAA
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. 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.