A problem with classes Part 2

Started by
13 comments, last by Kelly G 19 years, 4 months ago
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]
Advertisement
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)
Please edit your original post
1) to contain the actual error, and
2) to close the source tags (closing tags use forward slashes /, not backslashes \ [smile])
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
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.
- A momentary maniac with casual delusions.
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.
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.
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??
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.
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.

This topic is closed to new replies.

Advertisement