undeclared identifier help.

Cannot figure out undeclared identifier

Page 1 of 1

5 Replies - 1512 Views - Last Post: 19 January 2010 - 01:59 PM Rate Topic: -----

#1 ruevaughn  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 08-September 08

undeclared identifier help.

Posted 10 September 2008 - 06:20 PM

Hello, My name is Chase. This is my first post, and I am having some problems figuring this out. I am pretty new to c++, and am trying to program a game to play the game of sticks, where you alternate players taking sticks from a row, and the last player to take a stick wins. If I set all my 'strings' and 'ints' to global variables, my code works, but when i put them under int main it gives these errors. Heres my code and the error messages.

#include <iostream>
#include <string> 

using namespace std;





void drawboard(int r[]){

	for(int j = 0; j < 3; j++){

cout << "r" << j+1;

for(int i = 0; i < r[j]; i++){

cout << 'x';

}

cout << endl;

}

cout << endl;

}


//start game and set board


int play_game(int r[]){

drawboard(r);

if (player_turn == 1){

player_name = player1;

} else {

player_name = player2;

}

// print out instructions

cout << player_name << " Which row would you like to take from?";

cin >> user_row;

cout << "How many sticks would you like to take?" << endl;

cin >> user_sticks;


for (int i = 0; i < 3; i++){

if (user_row == i + 1){

if (user_sticks <= r[i] && user_sticks != i){

r[i] = r[i] - user_sticks;

}

}

}

if (player_turn == 1){

player_turn = 2;

} else {

player_turn = 1;

}

return 0; 

}

void start_game(){

cout << "Welcome to the game of Sticks. Player 1 please enter your name: " << endl;

cin >> player1;

cout << "Pleease enter Player 2's name: " << endl;

cin >> player2;

}


int main(){
// Setting Variables

int r[] = {3,4,5};

int user_row = 0;

int user_sticks = 0;

int player_turn = 1;

string player1;

string player2;

string player_name;

// Start game loop

start_game();

// Play game loop

do {

play_game(r);

} while (true);



return 0;

}





Thanks in advance! :)

Attached image(s)

  • Attached Image


Is This A Good Question/Topic? 0
  • +

Replies To: undeclared identifier help.

#2 no2pencil  Icon User is online

  • Admiral Fancy Pants
  • member icon

Reputation: 5364
  • View blog
  • Posts: 27,325
  • Joined: 10-May 07

Re: undeclared identifier help.

Posted 10 September 2008 - 06:24 PM

You have not declared these variables anywhere...

if (player_turn == 1){

player_name = player1;

} else {

player_name = player2;

}



The compiler has no idea what variable type that these are supposed to be, or any values. You can't just grab a variable name out of thin air, & the compiler is going to know how you want to use it. At the beginning of main, you should declare the variables that you will use.


int main(void) {
  int player_turn=1;
  str player_name;

  if (player_turn == 1){

  player_name = "player1";

  } else {

  player_name = "player2";
  return 0;
}


Was This Post Helpful? 1
  • +
  • -

#3 ruevaughn  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 08-September 08

Re: undeclared identifier help.

Posted 11 September 2008 - 09:58 AM

Thanks so much for your fast reply, I thought I had declared these in main, just haven't correctly passed them by reference?
Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6079
  • View blog
  • Posts: 23,550
  • Joined: 23-August 08

Re: undeclared identifier help.

Posted 11 September 2008 - 10:10 AM

Anything declared inside main -- or inside any function -- is only visible within that function.

You have to pass the names into the function in one case:
void start_game(string &player1, string &player2)
{
   ...
}


In the other case, I would put the variabled unneeded in main in the functions themselves.

void play_game(int r[], const string &player1, const string &player2)
{
    int user_row = 0;
    int user_sticks = 0;
    int player_turn = 1;
    string player_name;
...
}

int main()
{
    // Setting Variables
    int r[] = {3,4,5};

    string player1;
    string player2;

    // Start game loop
    // Player names come back in player1 and player2
    start_game(player1, player2);

    // Play game loop
    do {
        play_game(r, player1, player2);
    } while (true);

    return 0;
}

Was This Post Helpful? 1
  • +
  • -

#5 ruevaughn  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 08-September 08

Re: undeclared identifier help.

Posted 12 September 2008 - 08:13 AM

Thanks again, that solved my problem. I now cannot figure out why my player_name will not change. I know that I should have just used a bool for this changeplayer function, but I did not know how at the time. When I play this game, it does not change player 1's name to player 2 every round. I cannot figure it out. It was working, than when I started passing &player_name by reference it stopped working. It is under the play_game function i'm talking about. Sorry if this is confusing.

 // Chase Jensen
// CS 1410

#include <iostream>
#include <string>

using namespace std;



void drawboard(int r[]){

	for(int j = 0; j < 3; j++){

		cout << "Row " << j+1 << ": ";

		for(int i = 0; i < r[j]; i++){

			cout << 'x';
		}

		cout << endl;

	}

	cout << endl;

}



void play_game(int r[], const string &player1, const string &player2, string &player_name){
	int user_row = 0; 
	int user_sticks = 0; 
	int player_turn = 1; 
	drawboard(r);
   

	if (player_turn == 1){

		player_name = player1;

	} else {

		player_name = player2;

	}
	// print out instructions

	cout << player_name << " Which row would you like to take from?";
	cin >> user_row;

	cout << "How many sticks would you like to take?" << endl;
	cin >> user_sticks;


	for (int i = 0; i < 3; i++){
		if (user_row == i + 1){

			if (user_sticks <= r[i] && user_sticks != i){

				r[i] = r[i] - user_sticks;

			}

		}

   

	if (player_turn == 1){

		player_turn = 2;

	} else {

		player_turn = 1;

	}

	}
}

void start_game(string &player1, string &player2){

	cout << "Welcome to the game of Sticks. Player 1 please enter your name: " << endl;
	cin >> player1;
	cout << "Please enter Player 2's name: " << endl;
	cin >> player2;

}


void end_game(const string &player_name) {
	cout << player_name << "wins!!!";
}
int main(){
	// Setting Variables
	int r[3] = {3,4,5};
	int player_turn = 1;
	string player1;
	string player2;
	string player_name;
   

	// Start game loop

	start_game(player1, player2);

	// Play game loop

	do {

		play_game(r, player1, player2, player_name);
	} while (r[0]+r[1]+r[2] != 0);
   
	end_game(player_name);




	return 0;

}


Was This Post Helpful? 0
  • +
  • -

#6 ruevaughn  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 08-September 08

Re: undeclared identifier help.

Posted 19 January 2010 - 01:59 PM

Looking through my old posts, I decided to check this one out. This line of code kept setting my player to 1 every time.


void play_game(int r[], const string &player1, const string &player2, string &player_name){
	int user_row = 0;
	int user_sticks = 0;
	int player_turn = 1;
	drawboard(r);


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1