-1

I'm sorry for the vague title, but I don't know what else to say. Here is my program:

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

#include <vector>
using namespace std;
int
main (int argc, char **argv)
{
//string fin;
  string ttl, dscp, ext;
  string west = "w";
  string east = "e";
  string north = "n";
  string south = "s";
  int numRooms;
//string argv[1] = filename;
  class Room
  {
    public:string title;
    string description;
    string exits;
    int exitWest = -1;
    int exitNorth = -1;
    int exitEast = -1;
    int exitSouth = -1;
    int numExits;
  };

  ifstream fin;
  fin.open (argv[1]);
//cin.ignore();
  int t = 0;
  while (fin)
    {
      string tilde;
      string tester;
      tester = "~";
      cin >> tilde;
      if (tilde == tester)
    {
      t = t + 1;

    }
      (numRooms = t / 3);
    }


  Room *roomArrayPtr = new Room[numRooms];
  fin.clear ();
  while (fin)
    {
      for (int l = 0; l < numRooms; l++)
    {
      getline (fin, ttl, '~');
      roomArrayPtr[l].title = ttl;
      getline (fin, dscp, '~');
      roomArrayPtr[l].description = dscp;
      getline (fin, ext, '~');
      stringstream sin;
      sin << ext;
      string x;
      int y;
      while (sin >> x >> y)
        {
          if (x == west)
        {
          roomArrayPtr[l].exitWest = y;
        }
          if (x == south)
        {
          roomArrayPtr[l].exitSouth = y;
        }
          if (x == north)
        {
          roomArrayPtr[l].exitNorth = y;
        }
          if (x == east)
        {
          roomArrayPtr[l].exitEast = y;
        }

        }
      sin.clear ();
      int numext;
      numext = (ext.size ()) / 3;
      roomArrayPtr[l].numExits = numext;
    }}
//(read in file again populate roomarrayptr w while loop and getline)
//roomArrayPtr[index].title = line;
//roomArrayPtr[index].description=line;

//close files, while loop that reads in user input from stdin, delete pointers w (delete[] roomArrayPtr;)
//if (exitsarray[i].size() > 2){
  char command;
  char newcommand;
  cout << ">";
  cin >> command;
  int currentroom = 0;
  int newroom;
  bool keepgoing = true;
  //string dir1;
  while (keepgoing)
    switch (command)
      {
    // stringstream ss;
      case 'l':
    cout << roomArrayPtr[currentroom].title << endl;
    cout << roomArrayPtr[currentroom].description << endl;
    cout << endl;
//if (roomArrayPtr[currentroom].numExits < 2) {
    cout << "Exits: ";
    if (roomArrayPtr[currentroom].exitWest > -1)
      {
        cout << "w";
      }
    if (roomArrayPtr[currentroom].exitNorth > -1)
      {
        cout << "n";
      }
    if (roomArrayPtr[currentroom].exitSouth > -1)
      {
        cout << "s";
      }
    if (roomArrayPtr[currentroom].exitEast > -1)
      {
        cout << "e";
      }
/*else {
    cout << "Exits: " ;
    for (int k = 0; k < numExits; k++){
        cout << exitdirection << " ";
    }
}*/
//string newcommand;
    cin >> newcommand;
//int newroom;
    switch (newcommand)
      {
      case 'w':
        if (roomArrayPtr[currentroom].exitWest == -1)
          {
        cout << "You can't go WEST!" << endl;
          }
        else
          {
        newroom = roomArrayPtr[currentroom].exitWest;
        cout << "You moved WEST." << endl;
          }

        break;
      case 'e':
        if (roomArrayPtr[currentroom].exitEast == -1)
          {
        cout << "You can't go EAST!" << endl;
          }
        else
          {
        newroom = roomArrayPtr[currentroom].exitEast;
        cout << "You moved EAST." << endl;
          }

        break;
      case 'n':
        if (roomArrayPtr[currentroom].exitNorth == -1)
          {
        cout << "You can't go NORTH!" << endl;
          }
        else
          {
        newroom = roomArrayPtr[currentroom].exitNorth;
        cout << "You moved NORTH." << endl;
          }

        break;
      case 's':
        if (roomArrayPtr[currentroom].exitSouth == -1)
          {
        cout << "You can't go SOUTH!" << endl;
          }
        else
          {
        newroom = roomArrayPtr[currentroom].exitSouth;
        cout << "You moved SOUTH." << endl;
          }

        break;
      }
    break;
      case 'q':
    keepgoing = false;
    return 0;
    break;
    currentroom = newroom;
      }

}

Whenever I run it, it compiles, but nothing happens. I tried putting in some random cout << "testing1" "testing2" scattered throughout, but none of those even showed up. I put it immediately after the { after int main and it still didn't show up. What's going on?

I tried putting in some random cout << "testing1" "testing2" scattered throughout, but none of those even showed up. I put it immediately after the { after int main and it still didn't show up.

Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
killak
  • 1
  • Did you really mean to declare a class inside `main`? – Robert Harvey Nov 30 '22 at 01:11
  • 1
    The most awesome programming tool, right after the compiler, is the debugger. It allows you to run your program at your speed and watch exactly what it does as it does it. All you need to do is pay close attention for anything you didn't expect. The unexpected is almost always a bug, and the rest of the time it means your expectations are wrong. Both cases need to be fixed. – user4581301 Nov 30 '22 at 01:12
  • 2
    you mean `fin >> tilde;` not ` cin >> tilde;` – pm100 Nov 30 '22 at 01:12
  • 2
    Consistent indentation makes your code much easier to read and understand. – Some programmer dude Nov 30 '22 at 01:13
  • 2
    Ahh, the curse of having two identifiers that differ by only one character. I avoid this like the plague because it is a to spot. – user4581301 Nov 30 '22 at 01:14
  • 2
    Also note that `while (fin)` is the same as `while (fin.good())` which is *almost* as b[ad as `while (!fin.eof())`](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons) – Some programmer dude Nov 30 '22 at 01:14
  • For your next question, please take some time to read [the help pages](http://stackoverflow.com/help), take the SO [tour], read [ask], as well as [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). And learn how to create a [mre], with emphasis on the *minimal* part. – Some programmer dude Nov 30 '22 at 01:16
  • 1
    As a simple way to solve your problem: [Why is "using namespace std;" considered bad practice?](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) – Some programmer dude Nov 30 '22 at 01:17
  • In addition to the problem with `while (fin)` -- after that you later call `fin.clear ();` which resets `std::ios_base::goodbit` but doesn't rewind the file-position, so you are still at the end of file. – David C. Rankin Nov 30 '22 at 01:59

1 Answers1

1

you are reading the wrong file

  int t = 0;
  while (fin)
    {
      string tilde;
      string tester;
      tester = "~";
      cin >> tilde; <<<<===== i assume you mean fin
      if (tilde == tester)
    {
      t = t + 1;

    }
      (numRooms = t / 3);
    }
pm100
  • 48,078
  • 23
  • 82
  • 145