Solved

Trying To Search A Line of Text To Variables

Posted on 2004-03-31
1
259 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
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.

895 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now