Arrays

Hello,
Like many others I have recently started learning C++. I have written the following program and have a problem with displaying my areasArray. No matter what values I enter, the results are always zero. I would appreciate any help with this. Thanks :)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#include<iostream>
#include <iomanip>
#include<string>
#include<cmath>
#include<stdlib.h>
using namespace std;

void displayMenu(void);
void menuChoice(int &choice);
void storeAreas(int areasArray[], int &arraySize);
void readAreas(int &areas);
void readInteger(int &areas);
void clear();
void generateAreas (int areas[], int arraySize);
int displayAreas(int areasArray[], int arraySize);
void getAverage(int arraySize, int areasArray[]);
const int maxSize=7;
int main()
{
	//initialise variables
	int arraySize=7, mode=0, choice=0;
	int areasArray[maxSize]={0};
	double area=0, average=0, total=0, min=199.99, max=1999.99;
	//Display menu
	displayMenu();
	//Get users' choice
	menuChoice(choice);
	while(true)
	{
		switch (choice)
		{
		case 1:storeAreas(areasArray, arraySize);break;
		case 2:cout<<"3";break;
		case 3:displayAreas(areasArray, arraySize);break;
		case 4:cout<<"largestAreas(areasArray, arraySize)";break;
		case 5:cout<<"5";break;
		case 6:cout<<"6";break;
		case 7:cout<<"7";break;
		case 8: cout<<"You are about to leave this program." <<endl;return 0;
		default: cout<<"That is an invalid choice please re-enter menu selection"<<endl;
		}
		displayMenu();
		menuChoice(choice);
	}

}//end main
//***************************************************
void displayMenu(void)
{
	cout<<"Menu"<<endl;
		cout<<"1. Read Areas of 7 countries and store them in an array."<<endl;
		cout<<"2. Randomly generate Areas of M countries and store them in an array."<<endl;
		cout<<"3. Display areas stored in an array and the average area."<<endl;
		cout<<"4. Find and display the array index for largest country based on Area."<<endl;
		cout<<"5. Find and display the array index for smallest country based on Area."<<endl;
		cout<<"6. Sort and display in ascending order Areas stored in the array."<<endl;
		cout<<"7. Search and display Area which is equal to the given area."<<endl;
		cout<<"8. Exit from program."<<endl; 
		cout<<"Please enter a menu choice."<<endl;
}
//*********************************************************
void menuChoice(int &choice)
{
	cin>>choice;
		while(choice<1 || choice>8)
		{
			cout<<"Please enter a valid choice (0-8).";
			cin>>choice;
		}
}
//****************************************************
void storeAreas(int areasArray[], int &arraySize)
{
	int area=0;
	for(arraySize=0; arraySize<7; arraySize++)
	{
	cout<<"Please enter area in sqkm (199.99 - 1999.99), 0 to end)=>."<<endl;
		readAreas(area);
	}	
}
//****************************************************************
void readAreas(int &area)
{
	double min=199.99, max=1999.99;
	readInteger(area);
	while (area<min || area>max)
	{
		cout << "ERROR: The area must be between 199.99 and 1999.99 sqkm." << endl;
		readInteger(area);
	}
}
//*************************************************************
void readInteger(int &area)
{
	cin>>area;
	while(cin.fail())
	{
		cin.clear();
		clear();
		cout<<"You must enter a number, characters are not allowed."<<endl;
		cin>>area;
	}
}
//***********************************************************
void clear()
{
	char Char=' ';
	while (Char !='\n')
	{
		cin.get(Char);
	}
}
//**********************************************************
//This is the function I am having problems with. 
//I would like it to display the contents of my array however it returns zero values.
int displayAreas(int areasArray[], int arraySize)
{
	cout<<setw(7)<<"Country"<<setw(13)<<"Areas"<<endl;
	for (int i= 0; i<7; i++)
		cout<<setw(7)<<i+1<<setw(13)<<areasArray[i]<<endl;
	getAverage(arraySize, areasArray);
	return (0);
}
//**************************************************************
void getAverage(int arraySize, int areasArray[])
{
	double average=0, total=0;
	if(arraySize==0)
		cout<<"No areas to work with";
	else
	{
		for (int i=0; i<arraySize; i++)
		{
			total=total + areasArray[i];
		}
		average=total/arraySize;
		cout<<"The average of all areas is:"<<average<<endl;
	}
}
Last edited on
I'd recommend using the "code" formatting (click the icon with the <>), because it's much harder to read code if you don't use them, and people are less likely to answer your question.
1. Please format your code. (See the "<>" symbol to the right of the edit-box under "Format:".)
2. Only post the parts of the code you think are relevant to the problem.
3. Post some comments in the areas where you think the problem might be, and to explain what you want the line do do. Sometimes by just going through and comment your code you can find errors.

I'll be happy to look at it once I can read it easier.
Ah sorry. Here it is again :)
Hey, just sorted it after a good sleep. Silly mistake. thanks anyway :)
Topic archived. No new replies allowed.