When I enter 4 it gives me Error.

When I enter 4 after I add a number to the list it gives me an error and then for some reason I have to do a "break"??

#include<iostream>
#include<conio.h>
#include<stdio.h>
#include<stdlib.h> //header file not included
using namespace std;
void addToList(int *); // adds integer to integer array
void displayList(int *);//displays all items in array
void dispSum(int *); //gives sum of all elements in array
void dispAvg(int *); //gives average of elements in array.
unsigned short SIZE = 0;
void update(int); //update the value of SIZE ever time it is called
int main()
{
unsigned int input;
int arrayList[10];
do
{
cout << "***Main Menu***" << endl
<< "1. add number to list" << endl
<< "2. display list." << endl
<< "3. display sum." << endl
<< "4. display average." << endl
<< "0. Quit" << endl;

//clears iostream buffer
cin.clear();
cin.ignore(10, '\n');
cout << "enter a menu choice: ";
cin >> input;
switch (input)
{
case 1: addToList(arrayList);
break;
case 2: displayList(arrayList);
break;
case 3: dispSum(arrayList);
break;
case 4: dispAvg(arrayList);
break;
case 0: break;
default:cout << "enter a valid choice from menu:";
}
} while (input != 0);
return 0;
}
void update(int s)
{
s = s + 1;
}
void addToList(int *arrayList)
{
int input;
cout << "enter a number int the array: ";
cin >> input;
arrayList[SIZE++] = input;
update(SIZE);
}
void displayList(int *arrayList)
{
for (int i = 0; i < SIZE; ++i)
cout << arrayList[i];
cout << endl;
}
void dispSum(int *arrayList)
{
int sum = 0;
for (int i = 0; i < SIZE; ++i)
sum += arrayList[i];
cout << "The Sum is " << sum << "!" << endl;
}
void dispAvg(int *arrayList)
{
int avg, sum = 0;
for (int i = 1; i <= SIZE; ++i)
{
sum += arrayList[i - 1];

}
avg = sum / SIZE;

cout << "the avg is: " << avg;

}
Hello KenTheNoobProgrammer,

PLEASE ALWAYS USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/
Hint: You can edit your post, highlight your code and press the <> formatting button.
You can use the preview button at the bottom to see how it looks.

Starting at the top the header files "conio.h>", "stdio.h" and "stdlib.h" are all C header files and not standard C++ header files. I do not see anything in you code that would require these files,

The line "unsigned short SIZE = 0;" is better not hidden in your proto types. The "unsigned short" is a small problem later in the program in the for loops when you compare an "int" to an "unsigned short". This will not stop the program from running, but it will cause a warning on compile. The bigger problem is "SIZE". When I set up your program I needed to include the "Windows.h" header file for a function I use. One of the header files that "Windows.h" includes has a typedef for "SIZE" thus causing your use of "SIZE" to be a redefinition of "SIZE" which causes an error when compiled.

I had renamed the variable to get the program to work. For a variable that changes I would suggest using lower case letters for the variable name and choose a more descriptive name, something like, "maxSize" or "maxArraySize" so that by the time you get to your functions you will know what it means especially six month from now. Upper case letters are better used for a variable that is defined as "const" or in a "#define".

These lines:

1
2
3
//clears iostream buffer
cin.clear();
cin.ignore(10, '\n');


are not needed where you have put them. There is no reason to reset the "cin" flags or clear the buffer at this time. It also causes you to enter the menu choice two times. FYI the more accepted way to use "cin.ignore()" is: std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); which requires the "limits" header file. "std::" optional if using "using namespace std;" in your program.

The function "void update(int s)" has no real use. The "SIZE" that you pass to the function is passed by value. So, when "s" is changed there is no change to "SIZE" and when the function ends and returns to where it was call from the variable and value of "s" is lost. Also I have not found where "s" is used anywhere in the program.

In the function "void dispAvg(int *arrayList)" the for loop could be written better. First starting "i" at 0 instead of 1. Then arrayList[i - 1] would be arrayList[i]. Then i <= SIZE could be changed to i < SIZE Thus decreasing the chance of trying to access the array outside of its boundary.

After I made the changes the program ran fine. I could not find any problem unless I chose 4 before I added something to the array. I suggest adding some code to "dispAvg()" function to check that the array has something to work with before you run the for loop.

Hope that helps,

Andy
Topic archived. No new replies allowed.