Using void functions to display arrays

I want to write a code that gets three values from the user and puts them into three arrays. When the user enters -999, I want to print out a chart showing all the values they put in. This is what I have so far but it wont build. It tells me std::string is requested, but I'm not sure where to put it, and printArrays is declared void. How can I fix this?

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
#include <iostream>
#include <string>

using namespace std;

const int ARSIZE = 400;

void printArrays (string reportTitle, int levelsArray[], int scoresArray[], int starsArray[], int i);

int main()
{


	int levelsArray[ARSIZE];
	int scoresArray[ARSIZE];
	int starsArray[ARSIZE];
	int i;

	cout << "Please enter the level number (-999 to quit): ";
		cin >> i;


	while(i != -999)
	{
		i=ARSIZE;
	

		cout << "Please enter the score: ";
		cin >> scoresArray[i];

		cout << "Please enter the number of stars: ";
		cin >> starsArray[i];
		
		cout << "Please enter the level number (-999 to quit): ";
		cin >> i;
		
		if (i == -999)
		break;
		}
return ARSIZE; 

printArrays (levelsArray[ARSIZE], scoresArray[ARSIZE], starsArray[ARSIZE]);


void printArrays (levelsArray[ARSIZE],scoresArray[ARSIZE],starsArray[ARSIZE]) 
{
cout << "Level" << "	" << "Score" << "	" << "Stars" << endl;
cout << levelsArray[ARSIZE] << "	" << scoresArray[ARSIZE] << "	" << starsArray[ARSIZE];
}

		

}
Last edited on
Line 25: Because of this line, you're going to have out of bounds issues when your program actually does run, because of lines 29 and 32 where you're going to try and access element #400, when element numbers run from 0 to 399, inclusive.

Lines 40 and 42: Er. Did you mean to have these switched? Also, why return ARSIZE and not zero, out of curiosity?

Line 42: You should not have the [ARSIZE]s there because they're essentially getting the nonexistent 400th element and trying to pass it into a function parameter that expects an array. Also, your function expects a string as the first parameter and an integer as the last.

Line 43: You're missing a bracket here.

Line 45: The syntax of your function definition isn't quite right. That line needs to mirror the declaration you made on line 8.

Line 53: You have an extra bracket here. :P

-Albatross
Line 25: I think I may have confused myself here. The goal is to get the level number into the levelsArray but when I just cin<<levelsArray on lines 29 and 32, a whole bunch of errors come up.

Lines 40 and 42: Can they be switched and still work? The hint on the assignment is "Return the number of levels that were placed in the arrays (think about the subscript)" so I just assumed I should return ARSIZE.

Line 43: The brackets on void functions confuse me. Should it look like this?
1
2
3
4
5
6
{
void printArrays (levelsArray[ARSIZE],scoresArray[ARSIZE],starsArray[ARSIZE]) 

cout << "Level" << "	" << "Score" << "	" << "Stars" << endl;
cout << levelsArray[ARSIZE] << "	" << scoresArray[ARSIZE] << "	" << starsArray[ARSIZE];
}


I've fixed line 45 and 53 and deleted the ARSIZE's from line 42. How do I give the function a string as the first parameter and an integer as the last? I'm not sure what you mean by that.
Last edited on
Topic archived. No new replies allowed.