Solved

Trying To Search A Line of Text To Variables

Posted on 2004-03-31
1
261 Views
Last Modified: 2012-08-13
Ok Here Is What I have got so far I need to get a line of text from a file and use the data for variables in the program. The text is on multiple lines and looks like this.

S1 2 3 5 5
S2 1 3 4 8
S3 1 2 3 4

I need the program to search the file and extract the data if it exists if not ask the user to search again for another file. I need after the game is finished to delete the savegame file. When asking to save I need the program to search to make sure that the line does not already exist. This is my program so far. I need some help accomplishing this thank you.

// Include Statements
#include <iostream>
#include <stdio.h>
#include <fstream>
#include <iostream>
#include <conio.h>
#include <iomanip>
#include <process.h>
#include <string>
using namespace std;

int main () {
//Declarations
char answer1 = 'n';
char answer2 = 'n'; //Answers
int player = 1; //Player
int pile[3] = {3, 5, 8}; //Piles
int selpile = 1, selnumber = 1; // pile/number
char save[20]; //save
int savenum;

//File Declarations
ofstream outfile("NIMM.txt", ios::app);
ifstream infile("NIMM.txt");

//Title
cout << "Welocome To The Game of NIMM ;) By Greg Rapien." << endl;
cout << "|||||    |||| |||||||||||| |||||         ||||| |||||         |||||\n";
cout << " || ||    ||       ||       || ||       || ||   || ||       || ||\n";
cout << " ||  ||   ||       ||       ||  ||     ||  ||   ||  ||     ||  ||\n";
cout << " ||   ||  ||       ||       ||   ||   ||   ||   ||   ||   ||   ||\n";
cout << " ||    || ||       ||       ||    || ||    ||   ||    || ||    ||\n";
cout << "||||     |||| |||||||||||| ||||    |||    |||| ||||    |||    ||||\n";

//Rules of NiMM
cout << endl << "Rules : " << endl;
cout << "1.) Do not take the last stone." << endl;
cout << "2.) You must take stones each turn." << endl;
cout << "3.) You cannot take more stones then are in the pile." << endl;

//Options
cout << endl << "Options : " << endl;
cout << "1.) You can save your game." << endl;
cout << "2.) You can save after each turn." << endl;
cout << endl;

//Offer To Open A Save Game
cout << "Would you like to open a previous game? (y/n)" << endl;
cin >> answer1;

//Checking Whether To Load
if (answer1 == 'y' || answer1 == 'Y') {
    cout << "What Save Game Slot Would Like to Load From?" << endl;
    cout << "*Note It will be in S1, S2, S3... format." << endl;
    cin >> save;
//Need Help Here
    cout << endl;
    cout << "You Have Loaded From Save Game Slot " << save << "." <<endl;
}


//Beginning Game Loop

while ((pile[0] != 0 || pile[1] != 0 || pile[2] != 0) && answer2 == 'n') {
    cout << "It is player " << player << " turn." << endl;
    cout << "Select a Pile" << endl;
    cout << "1.) Pile1 : " << pile[0] << endl;
    cout << "2.) Pile2 : " << pile[1] << endl;
    cout << "3.) Pile3 : " << pile[2] << endl;
   
    do {
        cin >> selpile;
    } while (selpile < 1 || selpile > 3);
   
    selpile = selpile - 1;
    cout << "Enter the Number of Cards you want to take out of the pile." << endl;
   
    do {
        cin >> selnumber;
    } while (pile[selpile] - selnumber < 0 || selnumber <= 0);
   
    pile[selpile] = pile[selpile] - selnumber;
    cout << "It is the next players turn\n";
     
    if (player == 1) {
     player = 2;
    }
   
    if (player == 2) {
     player = 1;
    }
   
    cout <<"Would you like to save the game (y/n)\n";
    cin >> answer2;
    if (answer2 == 'y') {
        cout <<"Enter The File Number You Want To Save To :" << endl;
        outfile << "S" << savenum << " " << player << " " << pile[0] << " " << pile[1] << " " << pile[2];
        outfile.close();        
    }
 }  

    cout << "1.) Pile1 : " << pile[0] << endl;
    cout << "2.) Pile2 : " << pile[1] << endl;
    cout << "3.) Pile3 : " << pile[2] << endl;





system("PAUSE");
}
 

Thank You Inadvance
0
Comment
Question by:Rapieno
1 Comment
 
LVL 1

Accepted Solution

by:
shalmak earned 200 total points
ID: 10727112
enjoy.
I fixed some small bugs in addition to the file stuff (you didnt input the savenum, player never really changes to 2).

I didnt delete the file because you exit the program immediately after saving, so if you delete the file then nothing is saved...

// Include Statements
#include <iostream>
#include <stdio.h>
#include <fstream>
#include <iostream>
#include <conio.h>
#include <iomanip>
#include <process.h>
#include <string>
using namespace std;

struct GameSlot
{
      int slot_num;
      int player;
      int pile[3];
};

class SavedGames
{
public:
      explicit SavedGames(const char *filename)
      : m_filename(filename)
      {

      }

      ~SavedGames()
      {
            // delete the file here
      }

      GameSlot read_game(int slot_num)
      {
            GameSlot slot;

            ifstream infile(m_filename);
            const unsigned int buffer_size = 1000;
            char buffer[buffer_size];
            bool stop = false;
            do
            {
                  infile.getline(buffer, buffer_size);
                  int rc = sscanf(buffer, "S%d %d %d %d %d\n",
                        &slot.slot_num, &slot.player, &slot.pile[0], &slot.pile[1], &slot.pile[2]);

                  stop = rc < 5;

            }
            while (!stop && (slot.slot_num != slot_num));

            return slot;
      }

      int save_game(const GameSlot &slot)
      {
            // check if already exists
            GameSlot temp = read_game(slot.slot_num);

            if (temp.slot_num == slot.slot_num)
                  return -1;

            ofstream outfile(m_filename, ios::app);
            outfile << "S" << slot.slot_num << " " 
                  << slot.player << " " 
                  << slot.pile[0] << " " 
                  << slot.pile[1] << " " 
                  << slot.pile[2] << endl;

            return 0;
      }

private:
      const char *m_filename;
};

int main () {
      //Declarations
      char answer1 = 'n';
      char answer2 = 'n'; //Answers

      GameSlot game;
      game.player = 1; //Player
      game.pile[0] = 3;      //Piles
      game.pile[1] = 5;
      game.pile[2] = 8;
      
      int selpile = 1, selnumber = 1; // pile/number

      //File Declarations
      SavedGames saved_games("NIMM.txt");

      //Title
      cout << "Welocome To The Game of NIMM ;) By Greg Rapien." << endl;
      cout << "|||||    |||| |||||||||||| |||||         ||||| |||||         |||||\n";
      cout << " || ||    ||       ||       || ||       || ||   || ||       || ||\n";
      cout << " ||  ||   ||       ||       ||  ||     ||  ||   ||  ||     ||  ||\n";
      cout << " ||   ||  ||       ||       ||   ||   ||   ||   ||   ||   ||   ||\n";
      cout << " ||    || ||       ||       ||    || ||    ||   ||    || ||    ||\n";
      cout << "||||     |||| |||||||||||| ||||    |||    |||| ||||    |||    ||||\n";

      //Rules of NiMM
      cout << endl << "Rules : " << endl;
      cout << "1.) Do not take the last stone." << endl;
      cout << "2.) You must take stones each turn." << endl;
      cout << "3.) You cannot take more stones then are in the pile." << endl;

      //Options
      cout << endl << "Options : " << endl;
      cout << "1.) You can save your game." << endl;
      cout << "2.) You can save after each turn." << endl;
      cout << endl;

      //Offer To Open A Save Game
      cout << "Would you like to open a previous game? (y/n)" << endl;
      cin >> answer1;

      //Checking Whether To Load
      if (answer1 == 'y' || answer1 == 'Y') {
            int slot_num;
            do
            {
                  cout << "What Save Game Slot Would Like to Load From?" << endl;
                  cout << "*Note It will be in S1, S2, S3... format." << endl;
                  cin >> slot_num;
                  game = saved_games.read_game(slot_num);
                  cout << endl;
                  if (game.slot_num != slot_num)
                        cout << "Slot not found, please try again" << endl;
            }
            while (game.slot_num != slot_num);

            cout << "You Have Loaded From Save Game Slot " << game.slot_num << "." <<endl;
      }


      //Beginning Game Loop

      while ((game.pile[0] != 0 || game.pile[1] != 0 || game.pile[2] != 0) && answer2 == 'n') {
            cout << "It is player " << game.player << " turn." << endl;
            cout << "Select a Pile" << endl;
            cout << "1.) Pile1 : " << game.pile[0] << endl;
            cout << "2.) Pile2 : " << game.pile[1] << endl;
            cout << "3.) Pile3 : " << game.pile[2] << endl;
   
            do {
                  cin >> selpile;
            } while (selpile < 1 || selpile > 3);
   
            selpile = selpile - 1;
            cout << "Enter the Number of Cards you want to take out of the pile." << endl;
   
            do {
                  cin >> selnumber;
            } while (game.pile[selpile] - selnumber < 0 || selnumber <= 0);
   
            game.pile[selpile] = game.pile[selpile] - selnumber;
            cout << "It is the next players turn\n";
     
            switch (game.player)
            {
            case 1:
                  game.player = 2;
                  break;
            case 2:
                  game.player = 1;
                  break;
            }
   
            cout <<"Would you like to save the game (y/n)\n";
            cin >> answer2;
            if (answer2 == 'y') {
                  int rc;
                  do
                  {
                        cout <<"Enter The File Number You Want To Save To :" << endl;
                        cin >> game.slot_num;
                        rc = saved_games.save_game(game);
                        if (rc != 0)
                              cout << "Slot already taken" << endl;
                  }
                  while (rc != 0);
            }
       }  

    cout << "1.) Pile1 : " << game.pile[0] << endl;
    cout << "2.) Pile2 : " << game.pile[1] << endl;
    cout << "3.) Pile3 : " << game.pile[2] << endl;





      system("PAUSE");

      return 0;
}
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
MSVCR80.dll crash 2 141
Excel/Word Add-in in what language? 5 128
Template syntax for variable length arrays 9 75
How to convert Structure to vector of byte and vice versa 5 24
When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question