Link to home
Start Free TrialLog in
Avatar of Rapieno
Rapieno

asked on

Trying To Search A Line of Text To Variables

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
ASKER CERTIFIED SOLUTION
Avatar of shalmak
shalmak

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial