GIDForums  

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

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 11-Aug-2005, 22:53
BobTheHydra BobTheHydra is offline
New Member
 
Join Date: Aug 2005
Posts: 3
BobTheHydra is on a distinguished road
Question

Problem with structure arrays


I'm having problems adding values to a structure in an array:

CPP / C++ / C Code:
struct areaData
{
       int aid;
       char shortName[10];
       char longName[20];
       char desc[200];
       int obid[5];
};

struct areaData area[100];

area[1].aid = 1001; // This is the problem

From everything I read this seems to be the correct way to store a value. I recieve the following error upon trying to compile:

syntax error before '.' token

Am I doing it incorrectly? Thank you for your time.
  #2  
Old 11-Aug-2005, 23:34
maprich maprich is offline
Member
 
Join Date: May 2005
Posts: 163
maprich has a spectacular aura aboutmaprich has a spectacular aura about
Can you post more of your code, please.
Because the only problem I can think of is that maybe your line "area[1].aid = 1001;" is someplace such in your overall code that it is out of the visibility range of the declaration of the area array. Just speculating of course. Because to me it seems that there is nothing wrong from what is visible in your posting so maybe it is something that is not visible.
  #3  
Old 12-Aug-2005, 00:30
BobTheHydra BobTheHydra is offline
New Member
 
Join Date: Aug 2005
Posts: 3
BobTheHydra is on a distinguished road
Here is the full code I have thuse far. Pretty ashamed of it really as I'm still quite the novice and its no where near anything functional.

CPP / C++ / C Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "misc.h"

void login();                  // Start screen
void createCharacter();  // Start new game
void loadGame();           // Load saved game
void score();                // Show score card
void firstRun();             // Get everything started
int scanArea(int ID);      // Display the surrounding area

int gold = 100;

struct areaData             // structure to hold data pertaining to rooms
{
       int aid;
       char shortName[10];
       char longName[20];
       char desc[200];
       int obid[5];
};

struct areaData area[100]; // Array to store the room information 

area[0].aid = 1001;

struct character   // Structure to store various npc and pc data
{
       char name[10];
       int hp;
       int maxHP;
       int sp;
       int maxSP;
       int strength; 
       int dexterity;
       int wisdom;
       int constitution;
       int level;
       int exp;
};

struct character pc; // The player's structure

int main(int argc, char *argv[])
{
  login();	
  return 0;
}

void login() // Display the start screen
{
     system("cls");
     int choice;
     
     printf("*****************************************\n");
     printf("*           Crimson Streams             *\n");
     printf("*                                       *\n");
     printf("*                                       *\n");
     printf("*                                       *\n");
     printf("*****************************************\n\n");
     printf("\t1 - New game\n\t2 - Load Game\n\t3 - Quit\n");
     printf(">>");
     scanf("%d", &choice);
     
     switch(choice)
     {
                   case 1 :
                        createCharacter();
                        break;
                   case 2 :
                        loadGame();
                        break;
                   case 3 :
                        break;
                   default :
                        printf("Ivalid selection");
                        login();
                        break;
     }  
}

void loadGame() // Where file I/O should go
{
     printf("Not yet implimented... Thank you."); 
}

void createCharacter() 
{
     seed();
     system("cls");
     printf("Enter a name.>>");
     scanf("%s", &pc.name);
     pc.maxHP = 30;
     pc.hp = 30;
     pc.maxSP = 20;
     pc.sp = 20;
     pc.strength = 8;
     pc.dexterity = 8;
     pc.wisdom = 8;
     pc.constitution = 8;
     pc.level = 1;
     pc.exp = 0;
     score();
     firstRun();    
}

void score() // Show the score card
{
     printf("+-------------------------------------------------+\n");
     printf("| Name: %s\tHP: %d/%d\tSP: %d/%d         |\n", pc.name, pc.hp, pc.maxHP, pc.sp, pc.maxSP);
     printf("| STR: %d\tDEX: %d\t CON: %d\t WIS: %d           |\n", pc.strength, pc.dexterity, pc.constitution, pc.wisdom);
     printf("|                                                 |\n");
     printf("| Gold: %d\tLevel: %d\tExp: %d            |\n",gold, pc.level, pc.exp);
     printf("+-------------------------------------------------+\n");
}

void firstRun()       // Temporary function to get it all going
{
     
     scanArea(1001);
     PAUSE           // Pause to view output
}

int scanArea(int ID) 
{
     struct areaData temp; // Temp structure to hold the upcoming information
     int check;
     
     for(check = 0; area[check].aid == ID; check++)
     {
               if(area[check].aid == ID)   // Find the structure with the matching ID
               {
                                 temp = area[check]; // Assign the proper values to temp
               }
     }
     
     printf("%s", temp.longName);
     printf("\n");
     printf("%s", temp.desc);
}

It's something I'm adding to as I learn new things. I'm scratching my head over this. I'm not quite sure what exactly you meant about out of the visibility range.
  #4  
Old 12-Aug-2005, 00:43
Evil Requiem Evil Requiem is offline
New Member
 
Join Date: Aug 2005
Posts: 24
Evil Requiem will become famous soon enough
It must be done inside a function or a constructor (which is basically a function too). Move the line in main( ) and you'll be fine.
  #5  
Old 12-Aug-2005, 01:15
BobTheHydra BobTheHydra is offline
New Member
 
Join Date: Aug 2005
Posts: 3
BobTheHydra is on a distinguished road
Quote:
Originally Posted by Evil Requiem
It must be done inside a function or a constructor (which is basically a function too). Move the line in main( ) and you'll be fine.

Along that vein it worked, thanks a lot. Though trying to assign string values doesn't seem to work.

CPP / C++ / C Code:
area[0].longName = "A Room";

That results in:
incompatible types in assignment

Just not my day. Obviously I'm doing something wrong, I just don't see it. Maybe I shouldn't mess with structures just yet.
  #6  
Old 12-Aug-2005, 01:17
Evil Requiem Evil Requiem is offline
New Member
 
Join Date: Aug 2005
Posts: 24
Evil Requiem will become famous soon enough
You must use strcpy( ) which is used as follow: strcpy(destination, source); A better alternative, though, is strncpy( ). It also takes the size of the buffer in parameters. It will protect you from buffer overflow. strncpy(destination, source, size);
  #7  
Old 12-Aug-2005, 02:46
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,258
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
Quote:
Originally Posted by Evil Requiem
You must use strcpy( ) which is used as follow: strcpy(destination, source); A better alternative, though, is strncpy( ). It also takes the size of the buffer in parameters. It will protect you from buffer overflow. strncpy(destination, source, size);
strncpy() will also cause the string to be unterminated and cause a buffer overflow later... Stick with strcpy().
__________________

Got a cough? Go home tonight and eat a whole box of Ex-Lax. Tomorrow, you'll be afraid to cough.
-- Pearl Williams
  #8  
Old 12-Aug-2005, 09:49
Evil Requiem Evil Requiem is offline
New Member
 
Join Date: Aug 2005
Posts: 24
Evil Requiem will become famous soon enough
Then you could modify it a little bit by making your own.
CPP / C++ / C Code:
void my_strncpy(char* dest, const char* src, unsigned int size) {
    strncpy(dest, src, size-1);
    dest[size-1] = '\0';
}
 
 

Recent GIDBlogUS Elections and the ?Voter?s Responsibility? by crystalattice

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
Airport Log program using 3D linked List : problem reading from file batrsau C Programming Language 11 29-Feb-2008 08:44
Repetition structure problem and question brookeville C++ Forum 17 29-Oct-2004 18:48
problem with arrays melas C Programming Language 5 12-Oct-2004 05:18
[CONTEST?]Data Structure Test dsmith C Programming Language 2 06-Jun-2004 16:13

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

All times are GMT -6. The time now is 05:53.


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