Member Avatar for redback93

I am trying to make a code, where the points are not working. The points always give an odd number. Can you try it, if you can see a problem, I would love help.

#include <iostream>
#define cls system("cls")
#define pause system("pause")
#include <string>

using namespace std;

class games
{
  public:
   long double points;
   int tictactoe(void);    
};

int games::tictactoe(void)
{
 int computer, playerint;
 string player;
 computer = rand()%(2);
 cls;
 cout << "Heads (h) or Tails (t)?";
 cin >> player;  
 if (player == "t" || player == "T")
 {
  playerint = 0;           
 } else if(player == "h" || player == "H")
 {
  playerint = 1;       
 }
 if (playerint == computer)
 {
  cls;
  cout << "Well done! You Won!   +100 Points!" << endl;
  pause;
  points += 100;             
 } else 
 {
  cls;
  cout << "Sorry! You Lost!   -100 Points!" << endl;
  pause;
  points -= 100;       
 }
}


int chooser()
{
 int selection = 0;
 int temp;
 games player1;
 cls;
 cout << "Your Choices:" << endl << "1. Tic Tac Toe" << endl << "5. View Profile" << endl << "0. Exit" << endl << "Your Choice:";
 cin >> selection;
 if (selection == 1)
 {
  player1.tictactoe();  
 } else if (selection == 5){
  cls;
  cout << "Points: " << player1.points << endl;
  pause;
  cls;
  chooser();           
 } else if(selection == 0) 
 {
  return 0;      
 } else {
 cls;
 chooser();       
}
chooser();
}

int main()
{
 chooser(); 
}

People always ask, "Why do you always have to initialize your variables?"

This is why:

//Here you create something.  It is never initialized; therefore probably contains some sort of value..   what exactly it contains, we don't know.

long double points;

So when you start using this varible for accumulation, you are just adding to an unknown value:

points += 100;

Usually one would use a simple = assignment to initialize a variable, but since your variable is part of a class, it can only be initialize via use of a class constructor:

class games
{  
     public:

     games();
     long double points;   
     int tictactoe(void);    
};

games::games()
{
     points = 0.0;
}

Now, everytime a 'game' object is created, it's contents will automatically be initialized. You can then safely add your points to it and recieve expected results.

Lesson of the day: Take the time to initialize EVERYTHING.. ALL THE TIME. Then you will always get expected performance.

commented: Exactly, many problems comes from un-initialized variables! +2
Member Avatar for redback93

Thank you very much for the quick response!

I never kept thinking to myself, I have to put a default value to it, but this post reminded me how to do it :)

I tried adding this to my code, it debugs fine(as I suspected) but if you try it yourself, you would notice that the points weren't adding correctly, could you try helping me with this. Because once I figure this out, I will be fine.

I am trying something now, I will give you a reply if I figure it out.

Member Avatar for redback93

Ah! Got it to work!

I figured out what was happening, it wasn't focusing on the Player1.Points but just Points.

I made an integer that is added to Player1.Points everytime you go back to chooser()

Here is the code:

#include <iostream>
#define cls system("cls")
#define pause system("pause")
#include <string>

using namespace std;
int pointsReceive = 0;
class games
{
  public:
   games();
   long double points;
   int tictactoe(void);   
};

games::games()
{
 points = 0.0;           
}

int games::tictactoe(void)
{
 int computer, playerint;
 string player;
 computer = rand()%(2);
 cls;
 cout << "Heads (h) or Tails (t)?";
 cin >> player;  
 if (player == "t" || player == "T")
 {
  playerint = 0;           
 } else if(player == "h" || player == "H")
 {
  playerint = 1;       
 }
 if (playerint == computer)
 {
  cls;
  cout << "Well done! You Won!   +1 Points!" << endl;
  pause;
  pointsReceive = 1;             
 } else 
 {
  cls;
  cout << "Sorry! You Lost!   -1 Points!" << endl;
  pause;
  pointsReceive = -1;       
 }
}


int chooser()
{
 int selection = 0;
 int temp;
 games player1;
 cls;
 player1.points += pointsReceive;
 cout << "Your Choices:" << endl << "1. Tic Tac Toe" << endl << "5. View Profile" << endl << "0. Exit" << endl << "Your Choice:";
 cin >> selection;
 if (selection == 1)
 {
  player1.tictactoe();  
 } else if (selection == 5){
  cls;
  cout << "Points: " << player1.points << endl;
  pause;
  cls;
  chooser();           
 } else if(selection == 0) 
 {
  return 0;      
 } else {
 cls;
 chooser();       
}
chooser();
}

int main()
{
 chooser(); 
}

Sadly, it doesn't add the points, it just changes the points.

Member Avatar for redback93

Here is the new code:

#include <iostream>
#define cls system("cls")
#define pause system("pause")
#include <string>

using namespace std;
int pointsReceive = 0;

class games
{
  public:
   games();
   long double points;
   int tictactoe(void);   
};
games player1;
games::games()
{
 points = 0.0;           
}

int games::tictactoe(void)
{
 int computer, playerint;
 string player;
 computer = rand()%(2);
 cls;
 cout << "Heads (h) or Tails (t)?";
 cin >> player;  
 if (player == "t" || player == "T")
 {
  playerint = 0;           
 } else if(player == "h" || player == "H")
 {
  playerint = 1;       
 }
 if (playerint == computer)
 {
  cls;
  cout << "Well done! You Won!   +1 Points!" << endl;
  pause;
  pointsReceive = 1;             
 } else 
 {
  cls;
  cout << "Sorry! You Lost!   -1 Points!" << endl;
  pause;
  pointsReceive = -1;       
 }
}


int chooser()
{
 int selection = 0;
 int temp;
 cls;
 player1.points += pointsReceive;
 cout << "Your Choices:" << endl << "1. Coin Toss" << endl << "5. View Profile" << endl << "0. Exit" << endl << "Your Choice:";
 cin >> selection;
 if (selection == 1)
 {
  player1.tictactoe();  
 } else if (selection == 5){
  cls;
  cout << "Points: " << player1.points << endl;
  pause;
  cls;
  chooser();           
 } else if(selection == 0) 
 {
  return 0;      
 } else {
 cls;
 chooser();       
}
chooser();
}

int main()
{
 chooser(); 
}

I rewrote out some of the code to make it a bit cleaner and noticed that you had points being changed everytime you ran the choose() function (when you check the points or run the tictactoe() function or just enter a wrong input it would change the points) and I added a few comments to show you what I did take a look and see what I did if you want.

Also I see that you are using the cls command. People that use linux will cry when they see this just because its only useable on windows so it is not too good to get in the habbit of using "system" commands. I have windows and I don't care because I use "pause" all the time but its just for good practice I guess.

#include <iostream>
//#include <string> iostream includes string for you and you just need a char for the input you had before

using namespace std;

#define cls system("cls")
#define pause system("pause")

void chooser(); //declare prototype up here so you can get back to the chooser menu in the tictactoe function

//games class
class games
{
	long double points;
	
	public:
	
	games();
	
	long double getPoints();
	void tictactoe();
	
}player1; //can declare objects of this class right here if you want them public

games::games()
{
	points = 0.0;
}

long double games::getPoints()
{
	return points;
}

void games::tictactoe() //nothing is being returned? make a void function
{
	int computer, playerint;
	char player; //no need for a string use a char
	computer = rand()%2;
	cls;
	cout << "Heads (h) or Tails (t)?";
	cin >> player;  
	if (player == 't' || player == 'T')
		playerint = 0;           
	else if(player == 'h' || player == 'H')
		playerint = 1;
		
	if (playerint == computer)
	{
		cls;
		cout << "Well done! You Won!   +1 Points!" << endl;
		points++; //directly change the points instead of adding in chooser and having a global variable
		pause;
	}
	else 
	{
		cls;
		cout << "Sorry! You Lost!   -1 Points!" << endl;
		points--;
		pause;
	}
	chooser();
}

//end of games class

void chooser() //you dont need a return value for this function
{
	int selection = 0;
	cls;
	cout << "Your Choices:" << endl << "1. Coin Toss" << endl << "5. View Profile" << endl << "0. Exit" << endl << "Your Choice:";
	cin >> selection;
	if (selection == 1)
	{
		player1.tictactoe();  
	}
	else if (selection == 5)
	{
		cls;
		cout << "Points: " << player1.getPoints() << endl; //can use a get function to get the points since I made it private
		pause;
		cls;
		chooser();
	}
	else if(selection == 0)
		return; //just call return with nothing to end the function
	else
		chooser();
}
	
int main()
{
	chooser();
	return 0;
}
Member Avatar for redback93

Wow! Thank you very much for the code, last time I tried doing points++ it didn't add to player1 specifically, but just to the points, and that was the only problem I was running into.

Thank You Very Much!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.