A problem with classes Part 2

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:
};
//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;

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

//displays and handles the menu options

{
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;

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

cin >> input;

switch(input)
{
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 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 on other sites
1) to contain the actual error, and
2) to close the source tags (closing tags use forward slashes /, not backslashes \ [smile])

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 on other sites
Quote:
 Original post by Gamesmaster3I 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 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 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 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 playerclass Nation{//Member Declarationspublic: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 objectsNation nation1;Nation nation2;//sets the default nation valuesNation::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 constructorNation::Nation(){}//takes a turn for the playerbool 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;   elsereturn true;}//displays and handles the menu optionsvoid 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 Landcout <<"You buy " <<gold/20 <<" sections of land.\n";land += gold/20;gold %= 20;cout <<"You now have " <<gold <<" gold.\n";break;case 2: //Hires Farmersfarmers += people;cout <<"You hired " <<people <<" farmers. \n";break;case 3: //Hires Merchantsmerchants += people;cout <<"You hired " <<people <<" merchants. \n";people = 0;break;case 4: //Hires Blacksmithsblacksmiths += people;cout <<" You Hired " <<people <<" blacksmiths. \n";people = 0;break;case 5: //Handles the Battlecout <<"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 thingnation2.troops = 0;  //Very Bloody indeedbreak;case 6: return; //ends the turn}}}

conquest.cpp

#include<string>#include "conquest.h"#include <stdio.h>//The main Game functionint 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 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 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 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 on other sites
you don't need to specify the types a second time when you initialize them. Just the name, an equal sign, and the literal value will do.

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 on other sites
I know the feeling. Get some rest and let it settle in the back of your head. When you look at it tomorrow, you'll see it a little more clearly. Good luck.

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.

Create an account

Register a new account

• Forum Statistics

• Total Topics
627701
• Total Posts
2978709

• 21
• 14
• 12
• 10
• 12