• Advertisement
Sign in to follow this  

My program. was(Wierd Errors in this program.)

This topic is 4791 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

Source
[source lang = "cpp"]
#include <iostream>
#include <string>
using namespace std;
int goblinhp = 10;
int  yourhp = 20;
void start() {
     cout<<"Hello, this is the monster program."<<endl;
     cout<<"You are going to fight other monsters."<<endl;
     cout<<"Say, what is your name?"<<endl;
     string name;
     cin>>name;
     cout<<"Okay, "<<name<<" lets get started."<<endl;
};
void attack() {
     while(yourhp > 0 || goblinhp > 0)
     {
     cout<<"Oh no, you are attacked by a goblin."<<endl;
     cout<<"What are you going to do? (Type in lower case, attack, or magic."<<endl;
     string aom;
     cin>>aom;
     if(aom == 'attack') {
            cout<<"You hit the goblin for 2."<<endl;
            goblinhp = goblinhp - 2;
            cout<<"You get hit by the goblin for 3"<<endl;
            yourhp = yourhp - 2;
            }
     if(aom == 'magic') {
            cout<<"You hit the goblin with a spell for 3"<<endl;
            goblinhp = goblinhp - 3;
            cout<<"You get hit by the goblin for 4"<<endl;
            yourhp = yourhp - 4;
            }
     }
}
     
int main() {    
    int goblinhp = 10;
    int  yourhp = 20;
    start();
    attack();
    return 0;
}




errors.
 
C:/Documents and Settings/HP_Owner/Desktop/inventory.cpp:21:16: warning: character constant too long for its type
C:/Documents and Settings/HP_Owner/Desktop/inventory.cpp: In function `void 
   attack()':

C:/Documents and Settings/HP_Owner/Desktop/inventory.cpp:21: error: no match 
   for 'operator==' in 'aom == 1952539499'

C:/Documents and Settings/HP_Owner/Desktop/inventory.cpp:27:16: warning: character constant too long for its type
C:/Documents and Settings/HP_Owner/Desktop/inventory.cpp:27: error: no match 
   for 'operator==' in 'aom == 1634167139'

C:/Documents and Settings/HP_Owner/Desktop/inventory.cpp:44:1: warning: no newline at end of file

Execution terminated




Help please? Adam [Edited by - Meta Adam on January 6, 2005 11:46:36 PM]

Share this post


Link to post
Share on other sites
Advertisement
your misusing single quotes (they are for single characters not for strings) you want to start by replacing

if(aom == 'attack') to "attack"
and
if(aom == 'magic') to "magic"

edit you are also declaring goblinhp & yourhp twice (wich is ok since it's not in the same scope but since you're only using 1 of them remove the one in main since the one used is the global one)

Share this post


Link to post
Share on other sites
Revised program:
[source lang = "cpp]
#include <iostream>
#include <string>
using namespace std;
void start() {
cout<<"Hello, this is the monster program."<<endl;
cout<<"You are going to fight other monsters."<<endl;
cout<<"Say, what is your name?"<<endl;
string name;
cin>>name;
cout<<"Okay, "<<name<<" lets get started."<<endl;
};
int attack() {
int yourhp = 20;
int goblinhp = 10;
cout<<"Oh no, you are attacked by a goblin."<<endl;
while(yourhp > 0 && goblinhp > 0)
{
if(goblinhp == 0)
{
cout<<"You killed it."<<endl;
return 0;
}
if(yourhp == 0)
{
cout<<"You died."<<endl;
return 0;
}
cout<<"What are you going to do? (Type in lower case, attack, or magic."<<endl;
string aom;
cin>>aom;
if(aom == "attack") {
cout<<"You hit the goblin for 2."<<endl;
goblinhp = goblinhp - 2;
cout<<"You get hit by the goblin for 3"<<endl;
yourhp = yourhp - 2;
}
if(aom == "magic") {
cout<<"You hit the goblin with a spell for 3"<<endl;
goblinhp = goblinhp - 3;
cout<<"You get hit by the goblin for 4"<<endl;
yourhp = yourhp - 4;
}
}
}

int main() {
start();
attack();
return 0;
}


Any suggestion on what to add to it to make it better..? Eventually I want to turn it into a full text rpg.

Share this post


Link to post
Share on other sites
In terms of user friendliness, I would make it so that you only have to type "a" to attack, "m" to use magic et cetera. It is annoying to have to type the whole word. I think the convention is to put the key letter in parantheses (a)ttack, (m)agic.

That's probably not very helpful though. Just wait for the pros. They are going to tell you to make a player class and an enemy class, and a room class and if you are going to use different weapons or types of magic, then they will tell you to keep it classy. Maybe you should describe how complex you want it to be. Is it going to be like zork? More complicated or less complicated? What types of features do you want to include?

They might also recommend the Gametutorials tutorial on text rpgs. I wouldn't though if you are trying to use this as a learning experience. Maybe after you're done or if you get stuck. The temptation to just cut and paste their code is too hard to resist.

Edit: Funky and awkward sentence.

Share this post


Link to post
Share on other sites
It's a good idea to define your functions in the bottom of the source file, and just put the prototypes on top. Technically, though, it doesn't matter. It's just good coding practice.

EDIT:
Also, instead of typing cout << "Text yadda yadda" << endl;, you can use shorthand like so: cout << "Text yadda yadda\n";. \n is shorthand for newline.

And, instead of Variable = Variable - 4;, you should Variable -= 4;.

using namespace std; is a bad idea also. It may look like a harmless shortcut, but later down the road, it will become problematic.

Share this post


Link to post
Share on other sites
really, I want to script a map, and enemies, and then parse them in my code, but I dont know how, and I just want it to be a simple rpg, I can see myself making a room class, a person class, and an enemy class.

Share this post


Link to post
Share on other sites
Here is how I set up my person class.

class person {
private:
int level;
int hp;
int mana;
int exp;
public:
void set_lvl() {
int personlevel = level;
}
int get_lvl() {
return personlevel;
}
void set_hp() {
int personhp = hp;
}
int get_hp() {
return personhp;
}
void set_mana() {
int personmana = mana;
}
int get_mana() {
return personmana;
}
void set_exp() {
int personexp = exp;
}
int get_exp() {
return personexp;
}
void set_name(string* name) {
name = &person;
*name;
}

}



Is this right?

edit: I think i did the pointer wrong, as I have said before I'm starting back up after 5 months of being away hehe.

Share this post


Link to post
Share on other sites
I just wanted to know how advanced you were. I think maybe I should just sit this one out :) .

I'm just a beginner. I've gotten past classes, but I wouldn't feel comfortable pontificating on them. Not yet, anyway.

Share this post


Link to post
Share on other sites

class person {
private:
string name;
int level;
int hp;
int mana;
int exp;
public:
void set_lvl(int in_level) {
level = in_level;
}
int get_lvl() {
return level;
}
void set_hp(int in_hp) {
hp = in_hp;
}
int get_hp() {
return hp;
}
void set_mana(int in_mana) {
mana = in_mana;
}
int get_mana() {
return mana;
}
void set_exp(int in_exp) {
exp = in_exp;
}
int get_exp() {
return exp;
}
void set_name(string in_name) {
name = in_name;
}
string get_name(){
return name;
}
}

I redid all your code. I think you can learn more from simply reading it.

An example of how you would use this:

person *visage = new person;
visage->set_name("visage");
visage->set_lvl(10000);

cout<<"Visage's name is: " << visage->get_name() << endl;


I would recommend you read some more up on how classes work. A good C++ book would do.

[Edited by - visage on January 6, 2005 5:09:54 PM]

Share this post


Link to post
Share on other sites
Quote:
Original post by visage

class person {
private:
string name;
int level;
int hp;
int mana;
int exp;
public:
void set_lvl(int in_level) {
int level = in_level;
}
void set_hp(int in_hp) {
int hp = in_hp;
}
void set_mana(int in_mana) {
int mana = in_mana;
}

void set_exp(int in_exp) {
int exp = in_exp;
}




I would recommend you read some more up on how classes work. A good C++ book would do.

Why do you have to declare the variables like exp, hp, mana, and exp twice, they are declared in private and then in each of the individual functions.?

Share this post


Link to post
Share on other sites
That was my bad. I copied your code and didn't notice that. You shouldn't initialize them twice. Only once.

Share this post


Link to post
Share on other sites
Okay, I'm getting alot of errors from this:

#include <iostream>
#include <string>
using namespace std;
class person {
private:
int level;
int hp;
int mana;
int exp;
string name;
public:
void set_lvl(int) {
int personlevel = level;
};
int get_lvl() {
return personlevel;
};
void set_hp(int) {
int personhp = hp;
};
int get_hp() {
return personhp;
};
void set_mana(int) {
int personmana = mana;
};
int get_mana() {
return personmana;
};
void set_exp(int) {
int personexp = exp;
};
int get_exp() {
return personexp;
};
void set_name(string) {
string personname = name;
};
string get_name() {
return personname;
}
};
int main() {
person *dan;
dan->set_lvl(100);
dan->set_name(dan);
cout<<"Here is the person's name: "<<dan->get_name<<endl;
cout<<"Here is his level: "<<dan->get_lvl<<endl;
}


Share this post


Link to post
Share on other sites
Whoo, where to begin. Ill just go through line by ling.


#include <iostream>
#include <string>
using namespace std;
class person {
private:
int level;
int hp;
int mana;
int exp;
std::string name;
public:


Up till here, your code is fine. RIght here, it gets...ugly. What you need to do is study functions and parameters. Look at your code:

void set_lvl(int) {
int personlevel = level;
};


Okay, so you are getting some integer parameter (with no name...). What are you doing with it? Then you are setting a local variable to be equal to the class variable. Okay...but the local variable is deleted when the method ends...it goes out of scope. What you want to do is this:

void set_level(int in_level){
level = in_level;
}

That will set the class variable "level" to be equal to what was passed to the method.

The same goes for all your input methods.

Your next problem is here:

person *dan;
dan->set_lvl(100);

You never initialize dan. All you have is a pointer. You need to create a cooresponding object. Fixed like this:

person *dan = new person;


Your next issue is here:

dan->set_name(dan);

You are passing a "person" variable into a method expecting a string. If you want the name to be "dan", you need to wrap it in quotes. What you want is:

dan->set_name("dan");

Next, in your cout statements:

cout<<"Here is the person's name: "<<dan->get_name<<endl;
cout<<"Here is his level: "<<dan->get_lvl<<endl;

The error lies in the method calls: you forgot the ()s! It should look like this:

std::cout<<"Here is the person's name: "<<dan->get_name()<<endl;
std::cout<<"Here is his level: "<<dan->get_lvl()<<endl;


It is very, very important that you place:

delete dan

at the end to prevent a memory leak. Whenever you new, you should always delete.

Phew. Hope that helps.

Share this post


Link to post
Share on other sites

#include <iostream>
#include <string>
using namespace std;
class person {
private:
int level;
int hp;
int mana;
int exp;
string name;
public:
void set_lvl(int _level) {
level = _level;
};
int get_lvl() {
return level;
};
void set_hp(int _hp) {
hp = _hp;
};
int get_hp() {
return hp;
};
void set_mana(int _mana) {
mana = _mana;
};
int get_mana() {
return mana;
};
void set_exp(int _exp) {
exp = _exp;
};
int get_exp() {
return exp;
};
void set_name(string _name) {
name = _name;
};
string get_name() {
return name;
}
};
int main() {
person *dan;
dan = new person;

dan->set_lvl(100);
dan->set_name("dan");
cout<<"Here is the person's name: "<< dan->get_name()<<endl;
cout<<"Here is his level: "<< dan->get_lvl() <<endl;

delete dan;

cin.get();
return 0;
}



edit: damn, beaten to it :/

Share this post


Link to post
Share on other sites
Wow thanks! Its still telling me that I have undeclared variables, which is quite wierd, since ive clearly declared them as arguments, heres my new code.

#include <iostream>
#include <string>
using namespace std;
class person {
private:
int level;
int hp;
int mana;
int exp;
string name;
public:
void set_lvl(int personlevel) {
level = personlevel;
}
int get_lvl() {
return personlevel;
}
void set_hp(int personhp) {
hp = personhp;
}
int get_hp() {
return personhp;
}
void set_mana(int personmana) {
mana = personmana;
}
int get_mana() {
return personmana;
}
void set_exp(int personexp) {
exp = personexp;
}
int get_exp() {
return personexp;
}
void set_name(string personname) {
name = personname;
}
string get_name() {
return personname;
}
};
int main() {
person *dan = new person;
dan->set_lvl(100);
dan->set_name("dan");
cout<<"Here is the person's name: "<<dan->get_name()<<endl;
cout<<"Here is his level: "<<dan->get_lvl()<<endl;
}

Share this post


Link to post
Share on other sites
int get_lvl()
{
return personlevel;
}

"personlevel" needs to be "level", since level is part of your class, not personlevel.


#include <iostream>
#include <string>
using namespace std;
class person {
private:
int level;
int hp;
int mana;
int exp;
string name;
public:
void set_lvl(int personlevel) {
level = personlevel;
}
int get_lvl() {
return level;
}
void set_hp(int personhp) {
hp = personhp;
}
int get_hp() {
return hp;
}
void set_mana(int personmana) {
mana = personmana;
}
int get_mana() {
return mana;
}
void set_exp(int personexp) {
exp = personexp;
}
int get_exp() {
return exp;
}
void set_name(string personname) {
name = personname;
}
string get_name() {
return name;
}
};
int main() {
person *dan = new person;
dan->set_lvl(100);
dan->set_name("dan");
cout<<"Here is the person's name: "<<dan->get_name()<<endl;
cout<<"Here is his level: "<<dan->get_lvl()<<endl;
}

Share this post


Link to post
Share on other sites
wont that return an error though, because level is private?

edit: tried it and guess not, but I dont understand why

Share this post


Link to post
Share on other sites
functions which belong to the class can access even the private members of that class :)

Share this post


Link to post
Share on other sites
so even when the function is called outside, it is still a member of the class, so its alright.

Share this post


Link to post
Share on other sites
Exactly! Private members of the class can only be accessed by other members of the class. This includes variables and functions! Public members can be access outside of the class, which is why all of your input and output methods are public. If those were private, you would not be able to call them from outside of another class method.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement