• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 241
  • Last Modified:

text based game assistance (note:homework)

I am working on a school project that is a text based game.  I need to read in from three data files.
I think I am calling the other functions from main in correctly... I have no idea...


#include <iostream>
#include <fstream>
#include <string>
#include <iostream>
#include <iomanip>

using namespace std;


void ReadRoomsFile(structRoom Rooms[])
  {
  ifstream inFileRooms;
  inFileRooms.open("rooms.txt");
  if (!inFileRooms)
        cout <<"Error... file not found";
  else
  {
        for (int n=0; n<5; n++)
        {
              string endlthing;
          inFileRooms>>                  Rooms[n].Room_number;
          inFileRooms>>                  Rooms[n].sound;
          getline (inFileRooms, endlthing);                  // this is required to grab the endl between int and getline
          getline (inFileRooms, Rooms[n].Room_desc_short);

          for (int i = 0; i<6; i++)
              inFileRooms >>Rooms[n].exit[i];
       
          string s ="";
          getline (inFileRooms, endlthing);                  // this is required to grab the endl between int and getline
          do
          {
                    getline (inFileRooms,s);
                    if (s!="")
                    {
                          Rooms[n].Room_desc_long = Rooms[n].Room_desc_long + s;
                  Rooms[n].Room_desc_long = Rooms[n].Room_desc_long + "\n";
                    }
              }
        
          while (s!="");

          cout << Rooms[n].Room_number            <<endl;
              cout << Rooms[n].sound                  <<endl;
          cout << Rooms[n].Room_desc_short      <<endl;

          for (int i = 0; i<6; i++)
              cout <<Rooms[n].exit[i]<<" ";
              cout <<endl;

          cout <<Rooms[n].Room_desc_long      <<n<<endl<<endl;
        }
  }
  }


void ReadSoundsFile(structSound Sounds[])
  {
        ifstream inFileSounds;
  inFileSounds.open("sounds.txt");
  if (!inFileSounds)
        cout <<"Error... Sound file not found";
  else
  {
        for (int n=0; n<2; n++)
        {
              cout<< "testing";
          inFileSounds>>                  Sounds[n].Sound_number;
          //getline (inFileRooms, endlthing);                  // this is required to grab the endl between int and getline
          getline (inFileSounds, Sounds[n].Sound_description);

         

          cout << Sounds[n].Sound_number                        <<endl;
              cout << Sounds[n].Sound_description                  <<endl;
         
        }
  }
  }

void ReadItemsFile(structItem Items[])
{
  ifstream inFileItems;
  inFileItems.open("items.txt");
  if (!inFileItems)
        cout <<"Error... Item file not found";
  else
  {
        for (int n=0; n<2; n++)
        {
      
          inFileItems>>                  Items[n].Item_number;
              inFileItems>>                  Items[n].Item_sound;
              inFileItems>>                  Items[n].Item_location;
          //getline (inFileRooms, endlthing);                  // this is required to grab the endl between int and getline
          getline (inFileItems, Items[n].Item_description);
   

          cout << Items[n].Item_number                        <<endl;
              cout << Items[n].Item_description                  <<endl;
         
        }
  }
}


int main ()
{
  struct structRoom
  {
        int            Room_number;
        int            sound;
        string      Room_desc_short;
        int            exit[6];
        string      Room_desc_long;
      
  };
  struct structSound
  {
        int            Sound_number;
        string      Sound_description;
  };
  struct structItem
  {
        int            Item_number;
        int            Item_sound;
        int            Item_location;
        string      Item_description;
  };
 
  structRoom      Rooms[30];
  structSound      Sounds[15];
  structItem      Items[25];

 ReadRoomsFile      (structRoom            Rooms[]);
 ReadSoundsFile      (structSound      Sounds[]);
 ReadItemsFile      (structItem            Items[]);
 
    int x;
    cin >> x;
    return (0);
}
 

0
chad
Asked:
chad
  • 2
1 Solution
 
chadAuthor Commented:
He is a partial list of the errors I am getting.....

c:\adventure\adventrue.cpp(10): error C2065: 'structRoom' : undeclared identifier
c:\adventure\adventrue.cpp(10): error C2146: syntax error : missing ')' before identifier 'Rooms'
c:\adventure\adventrue.cpp(10): error C2182: 'ReadRoomsFile' : illegal use of type 'void'
c:\adventure\adventrue.cpp(10): error C2059: syntax error : ')'
c:\adventure\adventrue.cpp(11): error C2143: syntax error : missing ';' before '{'
c:\adventure\adventrue.cpp(11): error C2447: '{' : missing function header (old-style formal list?)
c:\adventure\adventrue.cpp(57): error C2065: 'structSound' : undeclared identifier
c:\adventure\adventrue.cpp(57): error C2146: syntax error : missing ')' before identifier 'Sounds'
c:\adventure\adventrue.cpp(57): error C2182: 'ReadSoundsFile' : illegal use of type 'void'
0
 
jkrCommented:
There were several errors - 1st of all, you need to declare the structs outside of 'main()', the you need not specify the type name when calling the functions - the following compiles:

#include <iostream>
#include <fstream>
#include <string>
#include <iostream>
#include <iomanip>

using namespace std;

  struct structRoom
 {
      int          Room_number;
      int          sound;
      string     Room_desc_short;
      int          exit[6];
      string     Room_desc_long;
     
 };
 struct structSound
 {
      int          Sound_number;
      string     Sound_description;
 };
 struct structItem
 {
      int          Item_number;
      int          Item_sound;
      int          Item_location;
      string     Item_description;
 };


void ReadRoomsFile(structRoom Rooms[])
 {
 ifstream inFileRooms;
 inFileRooms.open("rooms.txt");
 if (!inFileRooms)
      cout <<"Error... file not found";
 else
 {
      for (int n=0; n<5; n++)
      {
           string endlthing;
         inFileRooms>>               Rooms[n].Room_number;
         inFileRooms>>               Rooms[n].sound;
         getline (inFileRooms, endlthing);               // this is required to grab the endl between int and getline
         getline (inFileRooms, Rooms[n].Room_desc_short);

         for (int i = 0; i<6; i++)
             inFileRooms >>Rooms[n].exit[i];
     
          string s ="";
         getline (inFileRooms, endlthing);               // this is required to grab the endl between int and getline
         do
         {
                getline (inFileRooms,s);
                if (s!="")
                {
                     Rooms[n].Room_desc_long = Rooms[n].Room_desc_long + s;
                 Rooms[n].Room_desc_long = Rooms[n].Room_desc_long + "\n";
                }
           }
     
          while (s!="");

         cout << Rooms[n].Room_number          <<endl;
           cout << Rooms[n].sound               <<endl;
         cout << Rooms[n].Room_desc_short     <<endl;

         for ( i = 0; i<6; i++)
             cout <<Rooms[n].exit[i]<<" ";
           cout <<endl;

         cout <<Rooms[n].Room_desc_long     <<n<<endl<<endl;
      }
 }
 }


void ReadSoundsFile(structSound Sounds[])
 {
      ifstream inFileSounds;
 inFileSounds.open("sounds.txt");
 if (!inFileSounds)
      cout <<"Error... Sound file not found";
 else
 {
      for (int n=0; n<2; n++)
      {
           cout<< "testing";
         inFileSounds>>               Sounds[n].Sound_number;
         //getline (inFileRooms, endlthing);               // this is required to grab the endl between int and getline
         getline (inFileSounds, Sounds[n].Sound_description);

         

         cout << Sounds[n].Sound_number                    <<endl;
           cout << Sounds[n].Sound_description               <<endl;
         
      }
 }
 }

void ReadItemsFile(structItem Items[])
{
 ifstream inFileItems;
 inFileItems.open("items.txt");
 if (!inFileItems)
      cout <<"Error... Item file not found";
 else
 {
      for (int n=0; n<2; n++)
      {
   
          inFileItems>>               Items[n].Item_number;
           inFileItems>>               Items[n].Item_sound;
           inFileItems>>               Items[n].Item_location;
         //getline (inFileRooms, endlthing);               // this is required to grab the endl between int and getline
         getline (inFileItems, Items[n].Item_description);
   

         cout << Items[n].Item_number                    <<endl;
           cout << Items[n].Item_description               <<endl;
         
      }
 }
}


int main ()
{


  structRoom     Rooms[30];
 structSound     Sounds[15];
 structItem     Items[25];

ReadRoomsFile     (          Rooms);
ReadSoundsFile     (     Sounds);
ReadItemsFile     (          Items);

    int x;
   cin >> x;
   return (0);
}
0
 
chadAuthor Commented:
Dang JKr, I need you on speed dial
those changes worked perfectly.
now I can move on to the next step.. oh bother
:-)
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now