Sign in to follow this  

A problem with classes Part 2

This topic is 4763 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi guys.I've continued my training in c++.And since I was having so much trouble with classes I decided to give it another run through.This time I have a new problem.I went ahead and entered this into my compiler conquest.h
#include <stdio.h>
#include<iostream>
#include<string>


using namespace std;

//handles each nations for each player
class Nation
{
//Member Declarations
public:
int land;
int troops;

private:
string name;

int food;
int gold;
int people;
int farmers;
int merchants;
int blacksmiths;

public:
Nation(string lName);
Nation();
bool takeTurn(void);

private:
void menu(void);
};
//Create two nation objects
Nation nation1;
Nation nation2;

//a default constructor
Nation::Nation()
{

}

//takes a turn for the player
bool Nation::takeTurn()
{
cout <<"It's now " <<name <<"'s turn. \n";
people += land * 0.2;
food  += farmers - people * 0.25;
gold  += merchants * 20;
troops += blacksmiths;

menu();

if (nation1.land <= 0 || nation2.land <= 0) return false;
return true;
}

//displays and handles the menu options

void Nation::menu()
{
while (true)
{
int input = 0;
cout <<"food " <<food <<endl;
cout <<"gold " <<gold <<endl;
cout <<"land " <<land <<endl;
cout <<"merchants " <<merchants <<endl
<<"troops " <<troops <<endl
<<"unemployed " <<people <<endl;

cout <<"1) Buy Land \n"
<<"2) Hire Farmers \n"
<<"3) Hire Merchants \n"
<<"4) Hire Weaponsmiths \n"
<<"5) Attack!! \n"
<<"6) Take Turn \n";

cin >> input;

switch(input)
{
case 1: //Buys Land
cout <<"You buy " <<gold/20 <<" sections of land.\n";
land += gold/20;
gold %= 20;

cout <<"You now have " <<gold <<" gold.\n";
break;

case 2: //Hires Farmers

farmers += people;
cout <<"You hired " <<people <<" farmers. \n";
break;

case 3: //Hires Merchants
merchants += people;
cout <<"You hired " <<people <<" merchants. \n";

people = 0;
break;

case 4: //Hires Blacksmiths
blacksmiths += people;
cout <<" You Hired " <<people <<" blacksmiths. \n";
people = 0;
break;

case 5: //Handles the Battle
cout <<"The war wages into the night and all die!\n";
if(nation1.troops <nation2.troops)
{
nation2.land += 10;
nation1.land += 10;
}
else if(nation1.troops > nation2.troops)
{
nation2.land -= 10;
nation2.land += 10;
}

nation1.troops = 0 ;//War is a bloody thing
nation2.troops = 0;  //Very Bloody indeed

break;

case 6: return; //ends the turn
}
}
}


conquest.cpp
cout <<"Welcome to the Conquest \n";
cout <<"What is your name player 1?\n";
cin >> tempString;
nation1 = Nation(tempString);

cout <<"What is your name Player 2?\n";
cin >> tempString;
nation2 = Nation(tempString);

while (nation1.takeTurn() && nation2.takeTurn())
{

}
system("pause");
return 0;
}

I can't figure out what's causing this error.Here's the error message 1 conquest.cpp c:\my documents\conquest\conquest.h: In method `bool Nation::takeTurn()': I also get a couple of warnings but I'll worry about them later.Can anyone help me out on this one?? [Edited by - Gamesmaster3 on November 29, 2004 4:04:52 PM]

Share this post


Link to post
Share on other sites
It looks like the actual text of the error is missing from your post.

c:\my documents\conquest\conquest.h: In method `bool Nation::takeTurn()': (Should be an error message here)

Share this post


Link to post
Share on other sites
I went ahead and edited it up above.The other two messages the compiler shows me are warnings.Here they are.

48 conquest.h
warning: assignment to `int' from `double'

49 conquest.h
warning: assignment to `int' from `double'

Which I assume have something to do with the variable int people and int farmer; and the fact that the author of this program used decimals in the program which makes people and farmers actually a double.I tried changing them to doubles but I got a warning the other way too so I decided to keep it the way the author intended for now.

So you're saying that first part dosen't tell you anything??Because that's what's it say in its entirety

Share this post


Link to post
Share on other sites
Quote:
Original post by Gamesmaster3
I went ahead and edited it up above.


The error message is still missing (the part after the colon) and you need to add a beginning source tag to your second chunk-o-code.

Share this post


Link to post
Share on other sites
I'm copying and pasting it exactly the way it is in the copiler.There isn't anything else because the copiler hasn't anything else on the screen.When I click on the message all it does is highlight the #include conquest.h line on the conquest.cpp page.

n file included from c:\my documents\conquest\conquest.cpp:1:
c:\my documents\conquest\conquest.h: In method `bool Nation::takeTurn()':
c:\my documents\conquest\conquest.h:48: warning: assignment to `int' from `double'
c:\my documents\conquest\conquest.h:49: warning: assignment to `int' from `double'


That is all it says.I figured that it was reporting a error and I just couldn't understand what the compiler was trying to tell me.

Share this post


Link to post
Share on other sites
looks like you're using ints when you want to be using doubles. the only reason your compiler is complaining, is because the member variables in your class are declared as ints, and in your member function takeTurn() you are using double literals with these variables. It should still compile ok, but if you run it, you might find that your numbers won't be accumulating as you expect them to.

Share this post


Link to post
Share on other sites
I was playing with the code for a little while.I figured out that I was missing a part of the code.
Nation::Nation(lName)

was not there yet.I also had to put a else in one of the statements.I guess I should just show you.
conquest.h




#include <stdio.h>
#include<iostream>
#include <string>


using namespace std;

//handles each nations for each player
class Nation
{
//Member Declarations
public:
int land;
int troops;

private:
string name;

int food;
int gold;
int people;
int farmers;
int merchants;
int blacksmiths;

public:
Nation(string lName);
Nation();
bool takeTurn(void);

private:
void menu(void);
};
//Create two nation objects
Nation nation1;
Nation nation2;

//sets the default nation values
Nation::Nation(string lName)
{

//this was a initialiser list but I had to change it to this
//because of all the errors I was getting
string name = lName;
int land=20 ;
int food=50 ;
int troops=15;
int gold=100;
int people=100;
int farmers = 0;
int merchants = 0;
int blacksmiths = 0;

}





//a default constructor
Nation::Nation()
{

}

//takes a turn for the player
bool Nation::takeTurn()
{
cout <<"It's now " <<name <<"'s turn. \n";
people += land * 0.2;
food += farmers - people * 0.25;
gold += merchants * 20;
troops += blacksmiths;

menu();

if (nation1.land <= 0 || nation2.land <= 0) return false;
else
return true;
}

//displays and handles the menu options

void Nation::menu()
{
while (true)
{
int input = 0;
cout <<"food " <<food <<endl;
cout <<"gold " <<gold <<endl;
cout <<"land " <<land <<endl;
cout <<"merchants " <<merchants <<endl
<<"troops " <<troops <<endl
<<"unemployed " <<people <<endl;

cout <<"1) Buy Land \n"
<<"2) Hire Farmers \n"
<<"3) Hire Merchants \n"
<<"4) Hire Weaponsmiths \n"
<<"5) Attack!! \n"
<<"6) Take Turn \n";

cin >> input;

switch(input)
{
case 1: //Buys Land
cout <<"You buy " <<gold/20 <<" sections of land.\n";
land += gold/20;
gold %= 20;

cout <<"You now have " <<gold <<" gold.\n";
break;

case 2: //Hires Farmers

farmers += people;
cout <<"You hired " <<people <<" farmers. \n";
break;

case 3: //Hires Merchants
merchants += people;
cout <<"You hired " <<people <<" merchants. \n";

people = 0;
break;

case 4: //Hires Blacksmiths
blacksmiths += people;
cout <<" You Hired " <<people <<" blacksmiths. \n";
people = 0;
break;

case 5: //Handles the Battle
cout <<"The war wages into the night and all die!\n";
if(nation1.troops <nation2.troops)
{
nation2.land += 10;
nation1.land += 10;
}
else if(nation1.troops > nation2.troops)
{
nation2.land -= 10;
nation2.land += 10;
}

nation1.troops = 0 ;//War is a bloody thing
nation2.troops = 0; //Very Bloody indeed

break;

case 6: return; //ends the turn
}
}
}







conquest.cpp



#include<string>
#include "conquest.h"

#include <stdio.h>


//The main Game function

int main(void)
{
string tempString;

cout <<"Welcome to the Conquest \n";
cout <<"What is your name player 1?\n";
cin >> tempString;
nation1 = Nation(tempString);

cout <<"What is your name Player 2?\n";
cin >> tempString;
nation2 = Nation(tempString);

while (nation1.takeTurn() && nation2.takeTurn())
{

}
system("pause");
return 0;
}





It does compile without error but the problem is this gets me some really funky looking numbers so I can't understand what's going on in the program.Is this because of the warning??

Share this post


Link to post
Share on other sites
try changing the types of the member variables you use in the takeTurn() member function from int to either double or single.

when you use double values with an int, it cuts off everything past the right decimal point and takes the whole number to the left of the decimal point. Basically, if it's a number like 0.25, it will take the zero and drop the .25.

Hope this helps.

*edit:
oh yeah. Kelly G has a point. Totally slipped my mind.

Share this post


Link to post
Share on other sites
Did you initialize your variables anywhere in your program? At the beginning of your program, you don't what is going to be in those variables, the are just memory locations. The memory could still have numbers from previous use, the effect being that your variables contain seemingly random numbers.

Share this post


Link to post
Share on other sites
Yeah I noticed that I was just getting the memory.But I can't figure out what's wrong.In the Nation::Nation(lName) part I initialised all the variables.I had to change it from an initialiser List like the author had it but I assume it accomplishes the same goal if you break it down.

This secton right here is suppose to initialised all the variables

Nation::Nation(string lName)
{

//this was a initialiser list but I had to change it to this
//because of all the errors I was getting
string name = lName;
int land=20 ;
int food=50 ;
int troops=15;
int gold=100;
int people=100;
int farmers = 0;
int merchants = 0;
int blacksmiths = 0;

}



Am I wrong??

Share this post


Link to post
Share on other sites
Hothead was just telling me the samething.I went ahead and removed that but I still have the same problem with the memory address.I'm going to scrap this and call it a night.I'll try and redo the project tommorow.Maybe I just typed something wrong or something.I appreciate everybody's help.I wish I could have found a answer but the more I dig it seems the more complicated it becomes.

Goodnight everybody

Share this post


Link to post
Share on other sites
I'ts not just that you don't have to specify the type again, you shouldn't specify the type again because when you do it means you are declaring a variable. When you say "int land=20;" inside your constructor, you are creating a local variable called "land" that only exists inside the constructor. It has the same name as the variable "land" in your class definition but it's actually a new variable that is local to the function and consequently is deallocated at the end of the function. The "land" in your class definition, meanwhile, remains unchanged.

Share this post


Link to post
Share on other sites

This topic is 4763 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this