error: expected unqualified-id before ‘return’

I am trying to create a piece of code for an assignment that is due in 17 hours. it needs to be in the style of a gameshow in which a question is asked in code, for example "what animal is this" and the clue would be given as 3,1,20. the user would have to input the answer of "cat" to move onto the next question. i need to have five questions that are supposed to be randomly selected from 1 of 3 arrays per question (as seen in the code), however I believe that may be beyond my ability (however if someone could help with that it would be marvelous), I also need to have a scoring system that I believe I will be able to code myself, however I cannot get past this point in the code where I get the error of "error: expected unqualified-id before ‘return’". any help would be massively appreciated as this has been a hard year for learning and unfortunately this side of my coursework has not received the attention it should.

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
#include <iostream>
#include <string>
#include <fstream>
#include <list>
using namespace std;

int main()
{
	const string Teams[20] = { "Leicester City", "Tottenham Hotspur", "Liverpool", "Southampton", "Chelsea", "Aston Villa", "Everton", "Crystal Palace", "Wolverhampton Wanderers", "Manchester City", "Arsenal", "West Ham United", "Newcastle United", "Manchester United", "Leeds United", "Brighton and Hove Albion", "Fulham", "West Bromwich Albion", "Burnley", "Sheffield United" };
	const string Characters[11] = { "Bilbo Baggins", "Smaug", "Elrond", "Gollum", "Gandalf", "Thorin Oakenshield", "Oin","Gloin", "Dori", "Nori", "Ori" };
	const string Countries[12] = { "Argentina", "Bolivia", "Brazil", "Chile", "Colombia", "Ecuador", "Guyana", "Paraguay", "Peru", "Suriname", "Uruguay", "Venezuela" };
//this is where the potential answers are stored on the code, to be called upon by the questions


    
    cout << "Richard Osman's house of games,\nThis round is in code.\n";
    cout << endl;
    cout << "Press enter to begin questioning." <<endl;
    cin.get();
// this is added to include a level of immersion to the game
        
    cout << ("What premier league football club is this?\n");
    cout << "";

    int guess;
    // this is what the player actually inputs   
    char answer1;
    // this is the correct answer that the player needs to input
    int guesscount = 0;
    // this is in place so that the users guesses starts at 0
    int guesslimit = 3;
    // this is in place so that the user cannot guess infinetly or get stuck
    bool outofguesses = false;
    // when the user runs out of guesses they will have to retry
    
    while (answer1 != guess && !outofguesses) {
        if (guesscount < guesslimit) {
            cout << "Enter Guess: ";
            cin >> guess;
            guesscount++;
        }
        else {
            outofguesses = true;
        }
    }
            
    // this while loop will allow the user to input an answer, and they will be kept in the while loop until they get the question correct or run out of question attempts (3 attempts)

    // this cout tells the user they got the answer correct and allows them to progress to the next question or that they failed and will have to restart


//	
//	rand()
//	array.at
    }
    return 0;

Last edited on
i understand WHY it outputs error expected unqualified-id before 'return' but I'm unsure how to fix that
Swap lines 55 & 56. Your return 0; is outside your main function.

Or you could just delete the return and leave the closing bracket. C++ will implicitly add one if one isn't actually coded in main.
Thank you, because of your rpely i was able to work out I needed to add another } as well which let me keep in the return.


any idea on how I should start on the other parts of my task?
A start would be to initialize your variables guess (line 25) and answer1 (line 27) before you compare them in line 36.
guess is a variable based off of user input, would that affect how I initialize it?
and when i try to give answer 1 a value I get the error of "invalid conversion from "const char*" to "char"
Did you try to initialize answer1 with "", double quotes? They define a constant C string (char array or char*). To initialize a char variable use single quotes ''.

25
26
27
   int guess = 0;
   //
   char answer1 = '0';// or any single character not expected as input 

Modern C++ using C++11 uniform initialization with default values:
25
26
27
   int guess { };
   //
   char answer1 { };

https://mbevin.wordpress.com/2012/11/16/uniform-initialization/
in my code the answer can change dependent on what the question is, as they are randomly selected from a bank of 33 potential questions in arrays. would that work with it?
A char variable can hold a single character. That doesn't fit the requirements as I understand them. A std::string for holding the answer might be a better fit.

There are problems with comparing strings, mismatched cases for example. "Cat" and "cat" are not the same.

Maybe you should post the entire assignment requirements, though with a 17 hour (or less) time limit will make it hard for anyone to assist you in a timely fashion.
as the assignment is due in less than 17 hours I'm not bothered about having it pull the string from a separate file (unless that would be easier). I'm also not massively concerned if all of the requirements aren't met because this assignment is only 10% of the grade for this module.

I probably should have said this, to begin with, but I had started this assignment weeks ago(not just today) however my laptop broke (which I had the data on) and I've had to start again on my old PC. I promise I'm not just disorganised haha


Assignment Brief:
Make a prototype version of the game “This Round is in Code” from the TV series “Richard Osman’s House of Games”. It should take a list of answers from different categories (use moodle files) and show them to the user using a simple substitution cipher (a -> 1, b -> 2, c -> 3 etc). To give an example, if the answer is “Arsenal” the question should be “1 18 19 5 14 1 12”.
To improve code, present the questions as “the winner of the premier league was: 1 18 19 5 14 1 12” and the player would have to answer “arsenal”
Your program should read the Moodle files on start-up and store the category data using a suitable data structure. When the user starts the game, a category will be randomly selected, and 5 questions randomly asked from that category. You should implement the substitution using a separate function or method, passing the answer string and returning the question. The function should follow the format below:
Sorry it's too late now for my help on this, but for future you might want to consider buying a usb flash drive and after making source changes copy your code file(s) to the stick.
thank you, i will note that down for my next assignment
Thank you to everyone who helped. i managed to submit my work to a standard i was happy with and on time :D
here is the code if you are interested

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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#include <iostream>
#include <string>
#include <fstream>
#include <list>
using namespace std;
int score;

int main()
{
	const string Teams[20] = { "Leicester City", "Tottenham Hotspur", "Liverpool", "Southampton", "Chelsea", "Aston Villa", "Everton", "Crystal Palace", "Wolverhampton Wanderers", "Manchester City", "Arsenal", "West Ham United", "Newcastle United", "Manchester United", "Leeds United", "Brighton and Hove Albion", "Fulham", "West Bromwich Albion", "Burnley", "Sheffield United" };
	const string Characters[11] = { "Bilbo Baggins", "Smaug", "Elrond", "Gollum", "Gandalf", "Thorin Oakenshield", "Oin","Gloin", "Dori", "Nori", "Ori" };
	const string Countries[12] = { "Argentina", "Bolivia", "Brazil", "Chile", "Colombia", "Ecuador", "Guyana", "Paraguay", "Peru", "Suriname", "Uruguay", "Venezuela" };
	//this is where the potential answers are stored on the code, to be called upon by the questions, i would have used this array instead of calling upon files, however i could not get the code to function correctly when i attempted it this way, so i have simplified it for functionality.
  
	string answer1 = "Arsenal";
	// this is the correct answer that the player needs to input for question 1
	string answer2 = "Liverpool";
	// this is the correct answer that the player needs to input for question 2
	string answer3 = "Argentina";
	// this is the correct answer that the player needs to input for question 3
	string answer4 = "Guyana";
	// this is the correct answer that the player needs to input for question 4
	string answer5 = "Gandalf";
	// this is the correct answer that the player needs to input for question 5
	string guess;
	// this is how we read the players guess
	
    cout << "Richard Osman's house of games,\nThis round is in code.\n";
    cout << "The objective of this round is to unscramble the coded word, you will be given the category as the clue and you have to type out what you belive the answer to be, with a capital letter";
    cout << endl;
    string Name;
    cout << "Enter your first name.\n";
    cin >> Name;
    cout << "Welcome contestant " << Name << " Are you ready to begin the quiz?\n";
// this is to add a level of immersion to the game, by allowing the code to recall the players name so that they have a personalised experience

    string respond;
    cout << "please type Yes or No (case sensitive).\n";
    cin >> respond;
    if (respond == "Yes")
    {
        cout << "\nGood luck!";
    }
        else
        {
        cout << "Maybe next time!";
        return 0;
        }

	cout << "The category is..." << endl;
	
	cin.get();

	cout << "Premier League Football Teams (press enter)" << endl;
	
	cin.get();
	
	cout << "What Team is this?" << endl << endl;
	
	cout << "[1] {18] [19] [5] [14] [1] [12]" << endl << endl;
	cin >> guess;

	if (guess == answer1) {
		cout << "Nice job " << Name << ", press enter to continue to the next question" << endl;
		score = score + 1;
	} else  {
		cout << "Sorry " << Name << ", that was incorrect, press enter to continue to the next question" << endl;
		
		}
	cin.get();

	cout << "next category is... Premier leaugue football teams. (press enter to continue)" << endl << endl;
	cin.get();
	cin.get();
	cout << "What Team is this?" << endl;

	cout << "[12] [9] [22] [5] [18] [16] [15] [15] [12]" << endl;
	cin >> guess;

	if (guess == answer2) {
		cout << "Nice job " << Name << ", press enter to continue to the next question" << endl;
		score = score + 1;
		// this is the score system that will only increase your score when you get a question correct
	} else  {
		cout << "Sorry " << Name << ", that was incorrect, press enter to continue to the next question" << endl;
		
		}
		
    cout << "next category is... South American countries. (press enter to continue)" << endl << endl;  	
	cin.get();
	cin.get();
	cout << "What country is this?" << endl;

	cout << "[1] [18] [7] [5] [14] [20] [9] [14] [1]" << endl;
	cin >> guess;

	if (guess == answer3) {
		cout << "Nice job " << Name << ", press enter to continue to the next question" << endl;
		score = score + 1;
	} else  {
		cout << "Sorry " << Name << ", that was incorrect, press enter to continue to the next question" << endl;
		
		}
	
	cout << "next category is... South American countries. (Press enter to continue)" << endl << endl;
	cin.get();
	cin.get();
	cout << "What country is this?" << endl;

	cout << "[7] [21] [25] [1] [14] [1]" << endl;
	cin >> guess;

	if (guess == answer4) {
		cout << "Nice job " << Name << ", press enter to continue to the next question" << endl;
		score = score + 1;
	} else  {
		cout << "Sorry " << Name << ", that was incorrect, press enter to continue to the next question" << endl;
		
		}
		
	cout << "next category is... Hobbit Characters.(Press enter to continue)" << endl << endl;
	cin.get();
	cin.get();
	cout << "What Character is this?" << endl;

	cout << "[7] [1] [14] [4] [1] [12] [6]" << endl;
	cin >> guess;

	if (guess == answer5) {
		cout << "Nice job " << Name << ", press enter to continue to the next question" << endl;
		score = score + 1;
	} else  {
		cout << "Sorry " << Name << ", that was incorrect, press enter to continue to the next question" << endl;
		
		}
	
	cout << "Congratulations " << Name << "! you have completed the game" << endl;
	cout << "Your final score was " << score << " Points";
	
	int letter = 26;
	switch (letter) {
	case 'a':
		cout << "1";
		break;
	case 'b':
		cout << "2";
		break;
	case 'c':
		cout << "3";
		break;
	case 'd':
		cout << "4";
		break;
	case 'e':
		cout << "5";
		break;
	case 'f':
		cout << "6";
		break;
	case 'g':
		cout << "7";
		break;
	case 'h':
		cout << "8";
		break;
	case 'i':
		cout << "9";
		break;
	case 'j':
		cout << "10";
		break;
	case 'k':
		cout << "11";
		break;
	case 'l':
		cout << "12";
		break;
	case 'm':
		cout << "13";
		break;
	case 'n':
		cout << "14";
		break;
	case 'o':
		cout << "15";
		break;
	case 'p':
		cout << "16";
		break;
	case 'q':
		cout << "17";
		break;
	case 'r':
		cout << "18";
		break;
	case 's':
		cout << "19";
		break;
	case 't':
		cout << "20";
		break;
	case 'u':
		cout << "21";
		break;
	case 'v':
		cout << "22";
		break;
	case 'w':
		cout << "23";
		break;
	case 'x':
		cout << "24";
		break;
	case 'y':
		cout << "25";
		break;
	case 'z':
		cout << "26";
		break;
		// This is where the question would converts the answer into a number instead of the letters of the word, the answer will still be accpeted as the word.
		
	return 0;

}}
Last edited on
i didnt use all of the feautures i intended on, such as the case section and the string at the top being essentially useless, but having them in there at least shows what I was aiming to do
Topic archived. No new replies allowed.