GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 25-May-2007, 15:07
Bren Gun Bren Gun is offline
New Member
 
Join Date: May 2007
Posts: 4
Bren Gun is on a distinguished road
Question

" 'expected' errors "


And lots of 'em. I'm using g++ from MinGW and was able to minimize errors for this little test program, but many "expected some token" errors remain.

In 'chars.h' and 'functions.h' I get them:

chars.h:
Mostly "expected unqualified-id before '.' token" and "expected ',' or ';' before '.' token" for every line from 42 till 71.

functions.h:
In void_welcome_screen() all of them "expected primary expression before '.' token" from line 30 to 66, throughout most of them lines.


Now, it's been a while since I've seen code, and I just suddenly started trying to make a simple text game, going along as I saw fit, so it might be a stupid little thing I forgot or sum' like that (and I'm just doing this for fun, though you might question why would an inexperienced person be learning/doing C++, heh heh). By looking at the code I couldn't see anymore things that are wrong.

Been fooling around with using and #include but no good result yet.

Sidenote: seems I can't declare and simultaneously initialize variables inside a struct? Not so likeable.

Anyway, can't attach .h files, so here's the code in the post.


MAIN.CPP
CPP / C++ / C Code:
#include<iostream>
#include"chars.h"
#include<string>
#include"functions.h"


#define NO_ERROR 0





int main(){
	welcome_screen();
	
	
	return NO_ERROR;
}


CHARS.H
CPP / C++ / C Code:
/*Character Types: different kinds of characters in structs used in
welcome_screen() to determine character choice and initial values*/

// MALES:
struct MaleFast{
	short int hp;
	short int defense;
	short int speed;
	short int power;
	short int agility;
};
struct MaleMedium{
	short int hp;
	short int defense;
	short int speed;
	short int power;
	short int agility;
};
struct MaleSlow{
	short int hp;
	short int defense;
	short int speed;
	short int power;
	short int agility;
};
// FEMALES:
struct FemaleFast{
	short int hp;
	short int defense;
	short int speed;
	short int power;
	short int agility;
};
struct FemaleMedium{
	short int hp;
	short int defense;
	short int speed;
	short int power;
	short int agility;
};
/*Male initial values*/
MaleFast.hp = 100;
MaleFast.defense = 3;
MaleFast.speed = 7;
MaleFast.power = 3;
MaleFast.agility = 5;

MaleMedium.hp = 100;
MaleMedium.defense = 4;
MaleMedium.speed = 5;
MaleMedium.power = 5;
MaleMedium.agility = 4;

MaleSlow.hp = 100;
MaleSlow.defense = 7;
MaleSlow.speed = 2;
MaleSlow.power = 7;
MaleSlow.agility = 2;

/*Female initial values*/
FemaleFast.hp = 100;
FemaleFast.defense = 2;
FemaleFast.speed = 8;
FemaleFast.power = 2;
FemaleFast.agility = 7;

FemaleMedium.hp = 100;
FemaleMedium.defense = 3;
FemaleMedium.speed = 5;
FemaleMedium.power = 3;
FemaleMedium.agility = 6;


FUNCTIONS.H
CPP / C++ / C Code:
using namespace std;

char gender;
string playername;
char charactertype;

void cls(){
	system("cls");
}
void pause(){
	system("pause");
}

void welcome_screen(){
	cls();
	cout<< "\tWelcome to the text adventure.\n"
		<< "\tChoose your gender (M/F): ";
	cin >> gender;
	cout<< "\n\n\tChoose your name: ";
	cin >> playername;
	cout<< "\n\n\tHello, " << playername << ".\n"
		<< "\tNow choose your character type.\n"
		<< "\tYour character will have standard initial values.\n"
		<< "Weak/(f)ast, medium/(m)edium, strong/(s)low: ";
	cin >> charactertype;
	cout<< "\n\n\n";
	
	//if MaleFast:
	if ((gender=='m' || gender=='M') && (charactertype=='f' || charactertype=='F')){
		cout<< "Hit points: " << MaleFast.hp <<"\n"
			<< "Defense: " << MaleFast.defense <<"\n"
			<< "Speed: " << MaleFast.speed <<"\n"
			<< "Power: " << MaleFast.power <<"\n"
			<< "Agility: " <<MaleFast.agility <<"\n";
	}
	//if MaleMedium:
	else if ((gender=='m' || gender=='M') && (charactertype=='m' || charactertype=='M')){
		cout<< "Hit points: " << MaleMedium.hp <<"\n"
			<< "Defense: " << MaleMedium.defense <<"\n"
			<< "Speed: " << MaleMedium.speed <<"\n"
			<< "Power: " << MaleMedium.power <<"\n"
			<< "Agility: " <<MaleMedium.agility <<"\n";
	}
	//if MaleSlow:
	else if ((gender=='m' || gender=='M') && (charactertype=='s' || charactertype=='S')){
		cout<< "Hit points: " << MaleSlow.hp <<"\n"
			<< "Defense: " << MaleSlow.defense <<"\n"
			<< "Speed: " << MaleSlow.speed << "\n"
			<< "Power: " << MaleSlow.power << "\n"
			<< "Agility: " <<MaleSlow.agility <<"\n";
	}
	//if FemaleFast:
	else if ((gender=='f' || gender=='F') && (charactertype=='f' || charactertype=='F')){
		cout<< "Hit points: " << FemaleFast.hp <<"\n"
			<< "Defense: " << FemaleFast.defense << "\n"
			<< "Speed: " << FemaleFast.speed << "\n"
			<< "Power: " << FemaleFast.power <<"\n"
			<< "Agility: " << FemaleFast.agility <<"\n";
	}
	//if FemaleMedium:
	else if ((gender=='f' || gender=='F') && (charactertype=='m' || charactertype=='M')){
		cout<< "Hit points: " << FemaleMedium.hp <<"\n"
			<< "Defense: " <<FemaleMedium.defense <<"\n"
			<< "Speed: " <<FemaleMedium.speed <<"\n"
			<< "Power: " << FemaleMedium.power <<"\n"
			<< "Agility: " << FemaleMedium.agility <<"\n";
	}
}
  #2  
Old 25-May-2007, 15:22
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,688
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: " 'expected' errors "


Quote:
Originally Posted by Bren Gun
And lots of 'em. I'm using g++ from MinGW and was able to minimize errors for this little test program, but many "expected some token" errors remain.

That's not the way to use headers. I mean, that's not the way that people "usually" use headers.

Regardless of "usual" practice, the way that you wrote them and included them is just flat wrong.

To see what the compiler is seeing: paste the stuff from the headers into the main file instead of using #include. What you get is illegal.

For example, chars.h has assignment statements. But you include it in at a point in your source file that is not inside a function. Therefore: that does not compute.

Regards,

Dave

"No one was born knowing this stuff, you know."
---davekw7x
Last edited by davekw7x : 25-May-2007 at 15:57.
  #3  
Old 25-May-2007, 16:52
Bren Gun Bren Gun is offline
New Member
 
Join Date: May 2007
Posts: 4
Bren Gun is on a distinguished road

Re: " 'expected' errors "


Quote:
Originally Posted by davekw7x
That's not the way to use headers. I mean, that's not the way that people "usually" use headers.

Regardless of "usual" practice, the way that you wrote them and included them is just flat wrong.

To see what the compiler is seeing: paste the stuff from the headers into the main file instead of using #include. What you get is illegal.

For example, chars.h has assignment statements. But you include it in at a point in your source file that is not inside a function. Therefore: that does not compute.

Regards,

Dave

"No one was born knowing this stuff, you know."
---davekw7x
What is then the proper and/or usual way to use headers?


No matter what I do, though, and "fix", those 'expected primary expressions before '.' token' remain and 'expected unqualified-id' and 'expected ',' ' ETC., remain as well.

I put the contents of the .h files into main.cpp, fixed some compiler errors, and basically have the same thing left as before, that about the 'expected' stuff.

I've just been at it for a couple hours now, and looking through it any more will probably force me to throw myself out the window and break a neck (maybe mine or somebody else's who breaks my fall, heh heh).

I did read somewhere that it "might have to do" with line endings(?) when you copy and paste pieces of code or some stuff like that...? Vague. I did sometimes copy/cut and paste some code I wrote, but would it really matter? I thought a compiler doesn't care about it.


ANYWAY, uh... help me
  #4  
Old 25-May-2007, 17:50
ubergeek ubergeek is offline
Regular Member
 
Join Date: Jan 2005
Posts: 775
ubergeek is a jewel in the roughubergeek is a jewel in the roughubergeek is a jewel in the rough

Re: " 'expected' errors "


Like Dave said: you can only assign things inside a function. At any rate, you are assigning values, and attempting to access them later, as if there was only one instance of each struct (with the name of the struct as the variable name), which is a misconception. Also, you have defined five different structs (data types) which are all exactly the same. You definitely need to review declaration, purpse and usage of structs.
  #5  
Old 25-May-2007, 19:33
ahbi82 ahbi82 is offline
Member
 
Join Date: Jul 2006
Posts: 114
ahbi82 will become famous soon enough

Re: " 'expected' errors "


Quote:
Originally Posted by ubergeek
Like Dave said: you can only assign things inside a function. At any rate, you are assigning values, and attempting to access them later, as if there was only one instance of each struct (with the name of the struct as the variable name), which is a misconception. Also, you have defined five different structs (data types) which are all exactly the same. You definitely need to review declaration, purpse and usage of structs.

In addtion, i will show a simple example.

CPP / C++ / C Code:
// file : GameChar.h
#ifndef _GAME_CHAR_H_
#define _GAME_CHAR_H_
 
#include <cstdlib>
#include <iostream>
#include <string>
 
using namespace std;
 
// define enumeration of game char gender
typedef enum
{
    Gender_MALE = 0,
    Gender_FEMALE
} Gender;
 
// define enumeration of game char class
typedef enum
{
    CharClass_SLOW = 0, 
    CharClass_MEDIUM, 
    CharClass_FAST
} CharClass;
 
// Structure defintion of game character
typedef struct GameChar_struct
{
    int        ID;
    Gender     gender;
    CharClass  charClass;
    short      hp;
    short      defense;
    short      speed;
    short      power;
    short      agility;

    // Function to get Game gender class in string
    string getGameChar_genderString()
    {
        switch( this->gender )
        {
            case Gender_MALE:
                return "Male";
                break;
                
            case Gender_FEMALE:
                return "Female";
                break;
                
            default:
               return " Invalid Gender";
               break;
        }
    }
    
    // Function to get Game char class in string
    string getGameChar_classString()
    {
        switch( this->charClass )
        {
            case CharClass_SLOW:
                return "Slow";
                break;
                
            case CharClass_MEDIUM:
                return "Medium";
                break;
                
            case CharClass_FAST:
                return "Fast";
                break;
                
            default:
               return " Invalid Class";
               break;
        }
    }
    
    // Function to print Game char status
    void GameChar_PrintStatus( void )
    {
        cout << "Game Character Status" << endl 
             << "---------------------" << endl 
             << "ID       : " << this->ID <<endl 
             << "Gender   : " << this->getGameChar_genderString() << endl 
             << "Class    : " << this->getGameChar_classString() << endl 
             << "HP       : " << this->hp << endl 
             << "Defense  : " << this->defense << endl 
             << "Speed    : " << this->speed << endl 
             << "Power    : " << this->power << endl 
             << "Agility  : " << this->agility << endl;
    }
} GameChar;
#endif

 

CPP / C++ / C Code:
 
// file: main.cpp
 
#include <cstdlib>
#include <iostream>
#include <string>
#include "GameChar.h"
using namespace std;
int main( void)
{
    /* 
       Declare and initialize a char (female) with class of slow
    */
    GameChar femaleSlow = { 1,                //ID
                            Gender_FEMALE,    // gender
                            CharClass_SLOW,   // char class
                            200,              // hp
                            2,                // defense
                            2,                // speed
                            2,                // power
                            7 };              // agility
                            
    /* 
       Declare and initialize a char (male) with class of fast
    */
    GameChar* maleFast = (GameChar*)malloc( sizeof(GameChar) );
    maleFast->ID = 2;
    maleFast->gender = Gender_MALE;
    maleFast->charClass = CharClass_FAST;
    maleFast->hp = 200;
    maleFast->defense = 3;
    maleFast->speed = 8;
    maleFast->power = 2;
    maleFast->agility = 7;
    
    
    // print status of female slow
    femaleSlow.GameChar_PrintStatus();
    
    cout << endl << endl;
    
    // print status of male fast
    maleFast->GameChar_PrintStatus();     
    
    cout << endl << endl;
    
     return EXIT_SUCCESS;
}


Quote:

Output:

Game Character Status
---------------------
ID : 1
Gender : Female
Class : Slow
HP : 200
Defense : 2
Speed : 2
Power : 2
Agility : 7


Game Character Status
---------------------
ID : 2
Gender : Male
Class : Fast
HP : 200
Defense : 3
Speed : 8
Power : 2
Agility : 7

Hope this helps!!!!!!!!!

One point to take note, which is a good programming practice.
Don't de-reference a strucuture field (eg. this->hp). Create a function that return the value would be a better choice. I did that to save some space if not my reply would be lengthy. This applies to setting the value.
  #6  
Old 26-May-2007, 09:28
Bren Gun Bren Gun is offline
New Member
 
Join Date: May 2007
Posts: 4
Bren Gun is on a distinguished road

Re: " 'expected' errors "


The information you all gave for which I'm thankful is kind of overloading and confusing me Especially what ahbi82 gave. Typedef and #ifndef are hardly familiar to me; I've only glanced over them a couple times in the past (and they didn't seem so important to me (at the time)), add the use of -> which I've never used, and those examples are a lot of code to take in for an inexperienced programmer like me.

I will have a shot at it, but I don't expect to get it. Now that I think of it, though, wouldn't it be better to use classes instead of structs and such?

-

From what I understand, ubergeek, you're saying those variables in the structs have the same names (which is not allowed?) and I'm using the names of the structs to distinguish but it's futile? Makes sense. You're right that I should probably have a look at structs use again.

(Sometimes I don't know why I'm putting myself through the pain of C++ (and, before that, C) when I can use a higher-level language probably more easily for what I want to do.)
  #7  
Old 26-May-2007, 12:25
ahbi82 ahbi82 is offline
Member
 
Join Date: Jul 2006
Posts: 114
ahbi82 will become famous soon enough

Re: " 'expected' errors "


Quote:
Originally Posted by Bren Gun
The information you all gave for which I'm thankful is kind of overloading and confusing me Especially what ahbi82 gave. Typedef and #ifndef are hardly familiar to me; I've only glanced over them a couple times in the past (and they didn't seem so important to me (at the time)), add the use of -> which I've never used, and those examples are a lot of code to take in for an inexperienced programmer like me.

Those things that we present are BASICS of C/C++ programming. I purposely did an example to address what you did wrong in your code. Kindly read the comments. If you don't understand the keywords / reserved words of C/C++, read some books or search the net. I suppose we are here to help if you have diffculites in program and not teach you C/C++ programming.

Let me give you an idea of my presented code.

CPP / C++ / C Code:
#ifndef
#define
#endif

Theese are known as preprocessor directive. #define means to define a certain value or a macro For example

#define PI 3.142

tells the comiler that whenever PI is encountered in the program, replace it with 3.142. Note that it is not a variable declaration.

#ifndef _GAME_CHAR_H_
#define _GAME_CHAR_H_


From GameChar.h - the above tells the compiler

#ifndef _GAME_CHAR_H_---> meaning "if not define" this "_GAME_CHAR_H_", then #define _GAME_CHAR_H_. #endif is added at the end of the file

The is done so the file "GameChar.h" is not included multiple times. Anyway you will get an error during compilation if the file is included multiple times.

Now to typedef, which means "type definition". For example i can represent days of week (monday, tues, ....etc) as interger of value 1, 2, 3, 4 ..... respectively. But i want more understandable format, thus i created my own enumeration type..... call WEEK_DAYS
CPP / C++ / C Code:
typedef enum
{
    MONDAY, 
    TUESDAY, 
    WEDNESDAY
} WEEK_DAYS;


To use it, it is same as ur basic type declarations
CPP / C++ / C Code:
WEEK_DAYS    days;    // declaration

days = MONDAY;     // assignment

I even provided 2 different examples of declaring and initializing a structure. As i can see, most of your mistakes are in the structure.

The first is quite easy to understand. The second one, i used a function
CPP / C++ / C Code:
malloc
to set aside a memory space to store the structure GameChar.

I would appologise as i did a malloc, but i did not free it at the end of the program.
CPP / C++ / C Code:
free(maleFast);
return EXIT_SUCCESS;

Hope this gives you a clear picture.

Thanks!
  #8  
Old 26-May-2007, 15:51
Bren Gun Bren Gun is offline
New Member
 
Join Date: May 2007
Posts: 4
Bren Gun is on a distinguished road

Re: " 'expected' errors "


ahbi82, as said I appreciate your efforts and just to be sure I mean no offence

You're right in saying you don't have to teach me basics of the language, its features, but I didn't ask you to either. In that particular context I've only told you I'm unfamiliar with #ifndef, #endif, and typedef (for the simple reason that most sources and explanations I've seen are simply unnecessarily vague, unclear, and trivial, and that simply withholds me from even touching these three for the nth time!). It makes me raise my eyebrows everytime I see them, whether in an example or in a textbook.

I'm just critical to everything I see so I'm questioning if there's an easier way than your example. My purpose was to quickly solve those compiler errors (made in the structs and such, about "expected before tokens") and move on continuing making the thing, without losing time having to sweat through a couple of these little annoying critters again (that I've always loathed as much as I loathe it when my landlord comes at the door) as I've lost hours already(!), if it's possible to go for an easier quick solution.

I'm still checking your examples. Thanks kindly for your help and effort/time!
 
 

Recent GIDBlogToyota - 2008 August Promotion by Nihal

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Converting PHP to C and I need a little help Allenport C Programming Language 4 14-Aug-2006 13:38
Compile Errors due to Default Parameters jdbrine C++ Forum 1 17-Jun-2006 14:45
getting following errors in vc++ angel188 MS Visual C++ / MFC Forum 4 13-Jun-2006 14:10
Compiler errors vital_101 C Programming Language 4 12-Jan-2006 13:53
Help with syntax errors PeteGallo C Programming Language 7 08-Aug-2005 20:30

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 20:45.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.