Thread: Could someone look at this code?

  1. #1

    Question Could someone look at this code?

    This is a small very bad batle game I made, it is plain text, you just fight and try to kill the other guy. But I am using structures to store the info about the characters but every time i try to access the variable inside the structures i get an error saying:

    syntax error before '.'

    can someone please tell me what is wrong with it?

  2. #2
    "The Oldest Member Here" Xterria's Avatar
    Join Date
    Sep 2001
    Location
    Buffalo, NY
    Posts
    1,039
    1.) Get rid of using namespace std since you are using .h's
    2.) initalize the names as char name[20]
    3.) use strcpy() not = for strings

    Corrected Code:
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <iostream.h>
    #include <conio.h>
    #include <fstream.h>
    #include <stdlib.h>
    
    
    
    int input;
    int menu()
    {
    cout << "Make a choice: " <<endl;
    cout << "1.  fight" <<endl;
    cout << "2.  run" <<endl;
    cout << "3.  save" <<endl;
    return 0;
    }
    
      struct ENEMY
     {
    
     char name[20];
     int hp;
     int atk;
     }baddy;
    
    
    
     struct HERO
     {
    
     char name[20];
     int hp;
     int atk;
     }goodguy;
     
    
    
     void save()
     {
      ofstream jin;
      jin.open("player.txt");
    
      jin << "<NAME> "  << goodguy.name    << endl;
      jin << "<ATK> "   << goodguy.atk     << endl;
      jin << "<HP> "    << goodguy.hp    << endl;
      jin << "<ENEMY> " << baddy.name << endl;
      jin.close();
    
      }
    
    int main(int argc, char *argv[])
    {
    strcpy(goodguy.name, "baddy");
     baddy.hp=10;
     baddy.atk=3;
     strcpy(goodguy.name, "Akira");
     goodguy.hp=10;
     goodguy.atk=3;
    cout << "BATTLE" << endl;
    while(goodguy.hp>0 || baddy.hp>0)
    {
    cout << "What to do: ";
    menu();
    cin >> input;
    
    switch(input)
    {
     case 1:
       {
    
       int hdamage= rand() % goodguy.atk;
       int edamage= rand() % baddy.atk;
       cout << goodguy.name <<"attacked causing" << hdamage <<endl;
       baddy.hp= baddy.hp - hdamage;
       cout << baddy.name<< "attacked causing" << edamage <<endl;
       goodguy.hp= goodguy.hp - edamage;
       cout << "Your health is now: " << goodguy.hp;
       cout << endl << baddy.name<<"'s health is now" << baddy.hp <<endl;
       getchar();
    
       }
       break;
    
     case 2:
        {
        cout << "Your Coward";
        getchar();
        }
        break;
    
      case 3:
      {
     save();
      }
      break;
    
      default:
       cout << "Please make a choice" << endl;
    }
    
    
    }
    
    
      return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Extended ASCII Characters in an RTF Control
    By JustMax in forum C Programming
    Replies: 18
    Last Post: 04-03-2009, 08:20 PM
  2. Enforcing Machine Code Restrictions?
    By SMurf in forum Tech Board
    Replies: 21
    Last Post: 03-30-2009, 07:34 AM
  3. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 04:17 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 0
    Last Post: 02-21-2002, 06:05 PM