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

Started by
41 comments, last by Meta Adam 19 years, 3 months ago
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]
Meta AdamOne of a million noob C++ programmers.
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)
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.
Meta AdamOne of a million noob C++ programmers.
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.
skulldrudgery--A tricky bit of toil
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.
.:<<-v0d[KA]->>:.
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.
Meta AdamOne of a million noob C++ programmers.
Pardon me, but do you mean that you don't know how to do classes yet or that you don't know how to structure the program?
skulldrudgery--A tricky bit of toil
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.
Meta AdamOne of a million noob C++ programmers.
I dont know how to script, is that what you mean?
Meta AdamOne of a million noob C++ programmers.
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.
skulldrudgery--A tricky bit of toil

This topic is closed to new replies.

Advertisement