so I'm coding a MUD game for a class and I'm trying to figure out how to make getline only read a few lines from the file I'm giving the program at a time. Then when a user selects an option (maybe from a list of switch statements I'm not sure how I'm going to do that yet) it will display the next section of text from the file.
P.S. For reference 3 "~" signifies one block of text at a time. The last while loop I have prints the entire file at once. All I need to do is make getline only read a few lines at once
Thanks for the help!
#include <iostream>
#include <cstdio>
#include <string>
#include <fstream>
using namespace std;
struct Room{
string name;
string description;
int exits[4];
};
int main(int argc, char** argv)
{
//input file stream
ifstream fin;
string temp;
string rooms;
int count, numRooms;
//checking command line arguments count
if (argc != 2)
{
cout << "Error: Usage, must input only 1 file to read." << endl;
exit(1);
}
fin.open(argv[1]);
//checking if file is open.
if (!fin.is_open())
{
cout << "Error: File is not open" << endl;
return 1;
}
count = 0;
//count the ~'s in the file
while (getline(fin, temp))
{
if(temp == "~")
{
count++;
}
}
numRooms = (count / 3);
//allocate the memory for the rooms
Room* room;
room = new Room[numRooms];
//read the room information from file
fin.close();
ifstream git;
git.open(argv[1]);
if (!git.is_open())
{
cout << "Error: File is not open" << endl;
return 1;
}
while (getline(git, rooms))
{
if (rooms != "~" )
{
cout << rooms << endl;
}
}
}
Aucun commentaire:
Enregistrer un commentaire