Solved

Trying To Search A Line of Text To Variables

Posted on 2004-03-31
1
262 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

733 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