Primary-expression for array

This is my first entry here, so I apologize in advance if I screw something up. I am attempting to write a program that will aid in studying for an exam. After reading a textbook I have created two arrays--question and answer--to hold study questions about the text and the corresponding answers, respectively. Then I used what I hope to be an effective function to loop through the arrays, print to the screen the question, let the user give an answer, then show the user the correct answer and keep track of the user's right/wrong. I have reduced the array sizes here to save space. When I try to compile the code, I get an error message--among others--in line 30 'expected primary before 'question'

So first question is what is it referring to when it says "expected primary expression"?
Second question, how do I need to rewrite this code so it works?

Thank you in advance!
Brandon

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

using namespace std;

int main{

	string question[] = {
		
		//chapter 2 and 3
		"What is ESD?",
		"What is EMI?",
		"What is RFI?",
		"In alphabetical order, What are the six major types\n of PC connections?",
		"What term describes the end of a cable that goes into a port?",
		"What term describes a hole or slot that accepts a plug?",
		"What term is an alternative descriptor of a port?" };

	string answer[] = {
		
		//chapters 3 and 2
		"Electrostatic Discharge",
		"Electromagnetic Interference",
		"Radio Frequency Interference",
		"audio, DB, DIN, FireWire, RJ, USB",
		"plug",
		"port",
		"jack" };

	int numCorrect;
	int numWrong;
	int questionNum;
	string userAnswer;
	char askCorrect;
	int i = 0;
			


	while( i < question.size() )
	{
	cout<< "Answered correctly: " << numCorrect << " \t Answered wrong: " << numWrong;
	cout<< "\n\nQuestion Number " << questionNum;
	cout<< "\n\n\n" << question[i];
	cin>> userAnswer;
		
	cout<< "\n\n" << answer[i];
	
	cout<< "\n\nDid you get it right? (y/n)";
	cin>> askCorrect;
	
		if ( askCorrect == y )
		{
		numCorrect++;
		} else {
		numWrong++;
		}

	
	i++;
	
	};


return 0;

}
closed account (Dy7SLyTq)
the only thing i can see incorrect is line 52 where it should be == 'y'
question is defined as an array. Arrays have no member functions including size. So this statement is invalid.

while( i < question.size() )
Last edited on
First, with your code:

Line 7 is wrong.
Should be : int main(){

Line 52 is wrong.
Should be : if ( askCorrect == 'y')

I had to change you code a bit, but I tried to keep it the same mostly. It
should look cleaner, and I added comments so you can see what I did. ANy questions just aks me.


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

using namespace std;

int main(){

    int numCorrect = 0;
	int numWrong = 0;
	 const int ARRAY_LENGTH =  7; //This is the number of questions and answers you have.
	string userAnswer;

	string question[ARRAY_LENGTH] = {

		//chapter 2 and 3
		"What is ESD?",
		"What is EMI?",
		"What is RFI?",
		"In alphabetical order, What are the six major types\n of PC connections?",
		"What term describes the end of a cable that goes into a port?",
		"What term describes a hole or slot that accepts a plug?",
		"What term is an alternative descriptor of a port?" };

	string answer[ARRAY_LENGTH] = {

		//chapters 3 and 2
		"Electrostatic Discharge",
		"Electromagnetic Interference",
		"Radio Frequency Interference",
		"audio, DB, DIN, FireWire, RJ, USB",
		"plug",
		"port",
		"jack" };


for(int i = 0; i <= ARRAY_LENGTH; i++) //Same as the while loop but a little cleaner.
{


        cout<< "Answered correctly: " << numCorrect << " \t Answered wrong: " << numWrong << endl; //Display how many you got right and how many you got wrong.
         cout << "Question number:" << i + 1<< endl; //Display what question number.
         cout << question[i] << endl; //Display question.
         getline(cin, userAnswer);//Gets the input from the user, I usedgetline so the answer can have spaces in it.


       //check to see if you got the answer right or wrong.
         if(userAnswer == answer[i])
         {
             cout << "got it right \n";
             numCorrect += 1;
         }
         if(userAnswer != answer[i])
         {
             cout << "got it wrong \n";
             numWrong += 1;
         }


}

return 0;

}


}
@Hertz


Your code is also incorrect. For example this statement is invalid

for(int i = 0; i <= ARRAY_LENGTH; i++) //Same as the while loop but a little cleaner.
Actually @Hertz your solution worked beautifully! Thank you so much! I tweaked it a little bit because I didn't need the program to compare the answer to the user's answer--the user would have to enter in exactly what I had in the answer array for that to work and I didn't really see that happening for some of the questions.

If I may, I'd like to ask a follow up question. Right now, the program cycles through the two arrays I have in order. As I mentioned, I only put a small portion of the arrays in the post I made above. The actual program has 182 items in both the question array and the corresponding answer array. It would be nice if the program asked these questions in a different random order each time I fire up the program.

I know only the very basics about the srand() function and rand() function. From what I understand rand() gives a number between 0 and like 37,000 or something like that, correct? Does anyone have any ideas of how I can get the random number to be somewhere between like 0 and 200?
From what I understand rand() gives a number between 0 and like 37,000 or something like that, correct?

It gives a number between 0 and RAND_MAX, which is different on different systems

Does anyone have any ideas of how I can get the random number to be somewhere between like 0 and 200?

use std::uniform_int_distribution<>(0, 200) -- see
http://www.cplusplus.com/reference/random/uniform_int_distribution
http://en.cppreference.com/w/cpp/numeric/random/uniform_int_distribution

if you must use rand(), cast its result to double, divide by RAND_MAX, and then scale up to your limit (200)
@brandon14 and Hertz

The error that Vlad quite rightly mentioned, is because it will cause the loop to go past the end of the array, because of the less than equal operator. It should be :

for(int i = 0; i < ARRAY_LENGTH; i++)

Hope all is well.
OK, @TheIdeasMan thank you for being more clear and helpful :)

@Cubbi
i read the link you posted, and it looks really useful to my situation. However, could you or someone else maybe explain the implementation of this function a little? The documentation still has me a little confused, sorry
Topic archived. No new replies allowed.