简体   繁体   English

为什么我的程序什么都不做?

[英]Why isn't my program doing anything at all?

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.我尝试放入一些随机的 cout << "testing1" "testing2" 分散在各处,但这些都没有出现。 I put it immediately after the { after int main and it still didn't show up.我把它紧跟在 { after int main 之后,但它仍然没有出现。 What's going on?这是怎么回事?

I tried putting in some random cout << "testing1" "testing2" scattered throughout, but none of those even showed up.我尝试放入一些随机的 cout << "testing1" "testing2" 分散在各处,但这些都没有出现。 I put it immediately after the { after int main and it still didn't show up.我把它紧跟在 { after int main 之后,但它仍然没有出现。

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);
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我的C ++ / Qt程序什么都不做? - Why isn't my C++/Qt program doing anything? 第一次使用函数,不确定为什么我的程序什么都不做吗? - First time using functions, and not sure why my program isn't doing anything? 为什么我的增幅器功能没有设置任何内容? 还是我的构造函数? - Why isn't my mutator function setting anything? Or my constructor? 我在Visual Studio中的程序没有显示任何内容,我的代码有什么问题? - My program in visual studio isn't showing anything, what's wrong with my code? 为什么我的闰年程序的 for 循环不起作用? - Why Isn't my for loop for my leap year program working? 我正在向 ah 文件添加“模板化非成员函数”。 添加以下代码后,为什么我的程序没有输出任何内容: - I'm adding a “templated non-member function” to a .h file. Why isn't my program outputting anything after I've added the code below: 为什么我的 C++ 程序不工作? - Why isn't my C++ program working? 为什么我的程序没有显示我正在输出的整数? - Why isn't my program displaying the integer I'm outputting? 为什么我的文件没有正确读取该程序中的输入? - Why isn't my file reading the inputs correctly in this program? 我的阵列没有任何东西? - My Array isn't holding anything?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM