Help with "error: expected `;' before '{' token"

Sep 12, 2013 at 5:37am
Despite trying to follow the conventions for a void function, I still have an issue with the bug at line 17 that is going to keep me from finishing this assignment, the objective of which is to make a program to print out high scores and associated names from two different arrays. My problem is simply with the void function needed to input scores and names. Thanks! Also, the compiler in Xcode wants another '}' at end of input (at return 0;). Another } for what, I do know.

line 17 > initializeArrays(Names, Scores, size)
{

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

int main () {
using namespace std; 

	const int size = 5;
    string Names[size];
	int Scores[size];
	void initializeArrays(string Names[], int Scores[], const int size);
	void DisplayTops(string Names[], int Scores[]);	
		
	initializeArrays(Names, Scores, size) 
	{
		using namespace std; 
		for (int i = 0; i < size; i++)
		{
			cout << "Enter the name for score #" << i << ";" << endl;
			cin >> Names[i];
			cout << "Enter the score for Name #" << i << ";" << endl;
			cin >> Scores[i];
		}
	}


    return 0;
}
Last edited on Sep 12, 2013 at 5:41am
Sep 12, 2013 at 5:55am
closed account (Dy7SLyTq)
a) why do you have two using namespace stds?
b) why do you have them in main?
c) you cant have functions within functions in c++. you would have to do 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
#include <iostream>
#include <string>

using std::cout;
using std::endl;
using std::cin;
using std::string;

void initializeArrays(string Names[], int Scores[], const int size);
void DisplayTops(string Names[], int Scores[]);	

int main()
{
     const int size = 5;
     string Names[size];
     int Scores[size];

     initializeArrays(Names, Scores, Size);

    return 0;
}

void	initializeArrays(string Names[], int Scores[], const int size) 
{
		for (int i = 0; i < size; i++)
		{
			cout << "Enter the name for score #" << i << ";" << endl;
			cin >> Names[i];
			cout << "Enter the score for Name #" << i << ";" << endl;
			cin >> Scores[i];
		}
}


d) as for the error you were getting, because you were trying to declare a function within a function, your compiler saw it as a normal function call that was missing a semicolon (well it couldnt actually tell it was that but thats not important)
Sep 12, 2013 at 7:54am
closed account (z05DSL3A)
DTSCode wrote:
b) why do you have them in main?
because limiting the scope is a good thing?
Sep 12, 2013 at 10:23am
that was _______
Sep 12, 2013 at 2:31pm
closed account (Dy7SLyTq)
because limiting the scope is a good thing?
not here. hes wasting time writing it twice at the top of each function
Oct 8, 2013 at 3:57am
thanks all.
Topic archived. No new replies allowed.