problem C++

Started by
1 comment, last by memento_mori 16 years, 8 months ago
Hi. I'm made some modifications to my game, and now I get a warning: It's: "l>.\Mind Game.cpp(51) : warning C4129: 'P' : unrecognized character escape sequence" What does this mean? Here's the code:

/* Mind Game.cpp : Defines the entry point for the console application.
 For info see void info() or info.txt*/

#include<iostream>
#include<fstream>
#include<ctime>
using namespace std;

ofstream ofile;

void write_points (int& points)
{	
	//writes the points to "Points.txt"

	ofstream write_points ("Points.txt");
	//writes codes, to avoid cheating by editing the file ;-)
	write_points << points * 1024 * (-1) << " " << points * points - points << " " << 100 << " " << 1 
				 << "\n\nThese are codes to avoid cheating. To see your actual points open Mind Game.exe"
				 ;

	write_points.close();
}

void read_points (int& points)
{
	//reads the points from "Points.txt"

	ifstream read_points ("Points.txt");

	read_points >> points;

	points = points / 1024 / -1;

	read_points.close();
}

void info()
{
	ofstream info("Info.txt");
	info << "Progammed by Minas Mina, \n\n"
		 << "// 22/07/07 : Main game finished\n"
		 << "// 23/07/07 : More readable code & text ofile with the last game history\n"
		 << "// 24/07/07 : More readable code & no global variables, all in Main() & program information text ofile (Info.txt)\n"
		 << "// 26/07/07 : New variable (n_max)-starts from 6 and increases as  the player wins (6, 8, 10)\n" 
		 << "// 27/07/07 : New variables: chance_addition & chance_subtraction, difficulty variable (1:easy, 2:normal, 3:hard, 4:very hard)\n"
		 << "// 14/08/07 : Points! They increase when you win and decrease when you lose\n\n"
		 << "Easy: 50% addition, 50% subtraction, 0% multiplication\n"
		 << "Normal: 45% addition, 45% subtraction, 10% multiplication\n"
		 << "Hard: 40% addition, 40% subtraction, 20% multiplication\n"
		 << "Very Hard: 35% addition, 35% subtraction, 30% multiplication\n\n"
		 << "\POINTS-> Easy: 10 points, Normal: 20 points, Hard: 60 points, Very Hard: 80 points\n"
		 ;

	info << flush;
	info.close();
}

void set_difficulty(int difficulty, int& chance_addition, int& chance_subtraction)//prints ... in the Mind Game.txt and sets the difficulty
{
	if (difficulty==1)
	{
		chance_addition=50;
		chance_subtraction=100;

		ofile<<" (Easy)";
	}
	else if (difficulty==2)
	{
		chance_addition=45;
		chance_subtraction=90;

		ofile<<" (Normal)";		
	}	
	else if (difficulty==3)
	{
		chance_addition=40;
		chance_subtraction=80;

		ofile<<" (Hard)";		
	}
	else if (difficulty==4)
	{
		chance_addition=35;
		chance_subtraction=70;

		ofile<<" (Very Hard)";
	}

}

void addition(int& final_num, int random_num)
{
	final_num = final_num + random_num;

	cout << " + " << random_num << " ";
	ofile << " + " << random_num << " ";
}

void subtraction (int& final_num, int random_num)
{	
	final_num = final_num - random_num;

	cout << " - " << random_num << " ";
	ofile << " - " << random_num << " ";
}

void multiplication (int& final_num, int random_num)
{
	final_num = final_num * random_num;

	cout << " x " << random_num << " ";
	ofile << " x " << random_num << " ";
}

void win(int& score, int number_of_games, int& n_max, int difficulty, int& points)
{
	score++;
	
	n_max+=2;

	//the points increase
	if (difficulty==1)
	{
		points += 10;
	}
	else if (difficulty==2)
	{
		points += 20;
	}
	else if (difficulty==3)
	{
		points += 40;
	}
	else //if difficulty==4
	{
		points += 80;
	}

	cout << " Congratulations, you win!!"
		<< " [You: " << score << " PC: " << number_of_games - score << "]" << " [Points: " << points << "]\n";

	ofile << " Congratulations, you win!!"
		 << " [You: " << score << " PC: " << number_of_games - score <<"]" << " [Points: " << points << "]\n";
}

void lose(int final_num, int score, int number_of_games, int difficulty, int& points)
{
	//the points increase
	if (difficulty==1)
	{
		points -= 10;
	}
	else if (difficulty==2)
	{
		points -= 20;
	}
	else if (difficulty==3)
	{
		points -= 40;
	}
	else //if difficulty==4
	{
		points -= 80;
	}

	cout << " The answer was " << final_num << ". Sorry, you lose..."
		<< " [You: " << score << " PC: " << number_of_games - score << "]" << " [Points: " << points << "]\n";

	ofile << " The answer was " << final_num << ". Sorry, you lose..."
		 << " [You: " << score << " PC: " << number_of_games - score << "]" << " [Points: " << points << "]\n";
}

void process(int& final_num, int num, int& chance, int& random_num, int& n, int n_max, int chance_addition, int chance_subtraction)
{
	srand(static_cast<unsigned int>( time(NULL) ) );  //Initialize random number generator

	final_num = num;
	
	while (n <= n_max)
	{
		chance = 1 + rand() % 100; //chance is a random number from 1 to 100
		
		random_num = 1 + rand() % 10;
		
		if (chance <= chance_addition)
		{
			addition(final_num, random_num);//goes to addition()
		}
		else if ( (chance > chance_addition) && (chance <= chance_subtraction) )
		{
			subtraction(final_num, random_num);//goes to subtraction()
		}
		else if ( (chance > chance_subtraction) && (chance <= 100) )
		{
			multiplication(final_num, random_num);//goes to multiplication()
		}

		n++;

		cout << "\n";
		ofile << "\n";

	}
}

int main()
{ 
	info();//goes to info() which creates a text file with the program update information 

	int num; //num is the number that is given to the player
	int final_num; //this is the final number that the player must enter to win
	
	int random_num; //this is a random number from 1 to 10 that is being added/subtracted/multiplicated
	
	int n=1; //n is the number of...numbers that are being added/subtracted/multiplicated
	int n_max=6;//max numbers that are added, subtracted etc -> increases by 2 when player wins 6, 8, 10

	int user_num; //the number that the player enters
	int number_of_games_max=5;//when x games have been played (or when someone reaches 3 wins), the game ends
	int number_of_games=1; //shows how many games have been played
	int score=0; //the score

	int points=0;

	int difficulty;//the difficulty (1,2,3,4)

	int chance;//a random number from 1 to 100
	int chance_addition;//see void set_difficulty
	int chance_subtraction;//see void set_difficulty

	char letter = 'Y';

	read_points (points); //reads the points from Mind Game.***

	ofile.open ("Mind Game.txt");

//////////////////////////////
	cout<< "//Mind Game [Programmed by Minas Mina]//\n\n"
		<< "//[Points: " << points << "]" << "\n\n"
		<< "//You will play max "
		<< number_of_games_max
		<< " games. If you win 3 games you are champion!!\n"
		<< "//Select difficulty [1:Easy, 2:Normal, 3:Hard, 4:Very Hard]: ";
		
	cin >> difficulty;

	cout<< "////////////////////////////////////////////////////////////////////////////////\n";
	
	ofile << "//Mind Game [Programmed by Minas Mina]//\n\n"
		  << "//[Points: " << points << "]" << "\n\n"
		  << "//Difficulty: " << difficulty;
	
	//checks if difficulty is a number from 1 to 4. If not, the game ends
	if ( (difficulty >= 1) && (difficulty <= 4) )
	{
		set_difficulty (difficulty, chance_addition, chance_subtraction);//goes to set_difficulty
	}
	else
	{
		return 0;//if difficulty is not a number from 1 to 4 then the game ends!
	}

	ofile << "\n\n//You will play max "
		 << number_of_games_max
		 << " games. If you win 3 games you are champion!!\n\n";
	
	//the game loop
	while( (letter=='y') || (letter=='Y') )
	{	
		num = 1 + rand() % 10; // you are given a random number
		
		cout << "//Game " << number_of_games << ":// Your number is " << num << ". ";
		ofile << "//Game " << number_of_games << ":// Your number is " << num << ". \n"; //ofile needs \n because cin, which is used in cout, goes to the next line

		system("PAUSE");

		cout << "\n";
		ofile << "\n";
		
		process(final_num, num, chance, random_num, n, n_max, chance_addition, chance_subtraction); //goes to the void process
	
		cout << "\n What's the number now? ";
		ofile << "\n What's the number now? ";

		cin>>user_num; //the player enters the final answer

		cout << "\n";
		ofile << user_num
			 << "\n";

		if (user_num == final_num) //if the player's answer was correct
		{
			win(score, number_of_games, n_max, difficulty, points);//the player wins
		}
		else
		{
			lose(final_num, score, number_of_games, difficulty, points);//the player loses
		}

	
		num=0; final_num=0; user_num=0; n=1;

		//if the game is still running (if no-one has won until now)
		if ( (number_of_games < number_of_games_max) && (number_of_games - score < 3) && (score < 3) )
		{
			cout<<"\n Press y + enter to continue, anything else + enter to exit: ";
			cin>>letter;
			
			if ( (letter == 'Y') || (letter == 'y') )
			{
				number_of_games++;//number_of_games increases by one (1)
				cout << "\n"
					<< "////////////////////////////////////////////////////////////////////////////////\n";
				
				ofile << "\n";
			}
			else
			{
				break;
			}
		}
		else if (score == 3) // if the player wins
		{
			cout<<"\n//You did it!! You are the champion!!//"
				<<"\n\n ";

			ofile<<"\n//You did it!! You are the champion!!//"
				<<"\n\n ";

			system("PAUSE");
			break;
		}
		else //if the player loses
		{
			cout << "\n //Final Result: You lose.. Try harder next time..//"
				 << "\n\n ";

			ofile << "\n //Final Result: You lose.. Try harder next time..//"
				 << "\n\n ";

			system("PAUSE");
			break;
		}
	}

	write_points (points); //writes the points to "Points.txt" and to the screen

	ofile << flush;
	ofile.close();

	return 0;
}

[\source]

[Edited by - sheep19 on May 15, 2009 1:47:24 AM]
Advertisement
Quote:Original post by sheep19
"\POINTS


A '\' begins an escape sequence, such as the newline '\n'. This warning tells you that '\P' is not an escape sequence. If you want to print a '\' to the screen, do this:
"\\POINTS ..."
The mistake is in this line:

<< "\POINTS-> Easy: 10 points, Normal: 20 points, Hard: 60 points, Very Hard: 80 points\n"




it should be:


<< "\\POINTS-> Easy: 10 points, Normal: 20 points, Hard: 60 points, Very Hard: 80 points\n"




Reason: the compiler is trying to translate \P into something else. Just like \n breaks the line... \P doesn't really do anything, hence the warning. :)

This topic is closed to new replies.

Advertisement