Question:
C++ program problem, "error: expected initializer before 'int'"?
SunshineBabies
2009-10-06 17:20:59 UTC
I'm trying to get my project to compile and I'm getting an error message...I think there's probably a simple solution, maybe something I need to declare or missing punctuation, but I'm really stuck. Any help would be great. i'll give a short description of my project and include the necessary code.

1. Change your project 1 code. Write a function for the main menu of the game. This
function should display the main game choices (1) Start New Game, 2) Load
Game, and 3) Quit.), read the answer into a variable of string int data type, and
return that answer to the caller. This function has no parameters, and a return value of
data type string int. In the main program, call this function to display the main menu
and read the user input from the keyboard. Test your program before you continue to
requirement 2.
2. Declare two new variables in your main program, at the beginning:
int x = 10, y = 10;
These variables will later be used to keep track of the position of the character in the
game world. These variable represent (x,y) coordinates of the character.
3. Change your project 1 code again. In the main menu loop, write functions for the C++
code that is executed for menu choices 1 and 2, respectively. These two functions should
be void-functions (which do not have a return value, see chapter 5). Menu choice 3 does
not require a function. The main menu loop should only contain function calls inside of
the switch (or if-else if) statement. The C++ code that is run for each menu choice should
be in its own function. You are free to choose your own names for these functions.
a. The function to run the code for ‘Start New Game’ should have two call-byreference
parameters: two integers named x and y. When this function is called,
pass the variables x and y declared in step 2 as arguments to this function.
b. Do the same for the function for menu choice 2, ‘Load Game’.
Test your program before you continue to requirement 4.
4. Change your project 1 code again. In project 1 you wrote code to display a small amount
of text on the screen that describes the main character and the current surroundings. The
user is then presented with a prompt asking them for a command (e.g. “?> ”). In this
project you are writing two functions to handle these commands, and move your existing
code from the main program into these functions. One function should handle movement
(north, n, south, s, east, e, west, and w), and one function should handle display
(look, l). The command quit, q does not require a function.
a. The function for movement should look like this:
int move(int &x, int &y, char direction);
This function moves the character in the specified direction (either ‘n’, ‘s’, ‘w’, or
‘e’). Your main program will read the user input and then call this function using
a single character for each command. If the user input was “north” then the
function should be called with the character ‘n’ as direction. Use the variables
x and y declared in step 2 as arguments for the function parameters x and y.
Also, change the coordinates as the character moves. Moving north should add 1
to the y-coordinate; south subtract 1 from the y-coordinate. East should add 1 to
the x-coordinate, and west subtract 1 from the x-coordinate.
b. The function for looking should look like this:
void look(int x, int y);
This function will display what the character sees in the game world, plus the
current coordinates of the caracter. Use the variables x and y declared in step 2 as
arguments for the function parameters x and y.
Test your code again at this point.
5. Format your code correctly (correct indentations) and don’t forget to put comments in
your code! They are part of the grade now. At the minimum you should have:
a. Header comments
b. Comments for each function prototype, to explain any function parameters (input
to the function) and the return values (if any)
c. Comments for each loop, explaining briefly the purpose of that loop
In general, you can’t have too many comments in your program code. The basic goal is to
make your code easy to read and easy to understand.

here's the code...
#include

using namespace std;

// Moves the character around the enviroment.
void Start_New_Game (int &x, int &y);

// Attempts to load previous game.
void Load_Game ();

//This keeps track of the coordinates of your character.
void Move (int &x, int&y, string direction)

int main ()
{

{
//variables
int choice=0;
int x=10, y=10;
string input="";

//Title
cout << "Nature Hike" << endl << endl;

do{
//Menu
cout << "1) Start New Game" << endl;
cout << "2) Load Game" << endl;
cout << "3) Quit" << endl;
cout << endl;

cout << "Make a menu selection.";
cin >> choice;

Start_New_Game (x,y);
Load_Game ();

}

}
void Start_New_Game (i
Three answers:
Ratchetr
2009-10-06 17:33:05 UTC
Start by adding a semicolon at the end of this line:

void Move (int &x, int&y, string direction) ;



One missing semi can generate a bunch of errors.



You'll get more errors...You have a do { with no matching }while();

Rest of your code got cut off.



When posting a compiler error, it helps to flag the line of code that the error is on. And we don't really need the entire homework assignment, just the code.
Erika
2016-11-06 09:55:06 UTC
Expected Initializer Before
2016-05-21 04:03:21 UTC
Just out of curiosity, what makes you say "Everything is alright,..". If your code doesn't compile, it is broken. Things are most definitely not alright ;-) Anyway...you really mucked up the definition of the grade variable. In the first if clause, you call it grade[81] and try to assign a char to it. In all the rest, you call it a char but try to assign a string to it. But...you should only define grade once, before you get into the if statements: char grade; Then, in the if statements, don't redeclare the variable, just assign a value to it: grade = 'A';


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...