Question

In: Computer Science

how can I get my program to read one of 4 text files (chosen/ inputted by...

how can I get my program to read one of 4 text files (chosen/ inputted by user, game1.txt, game2, game3, or game4.txt). and place its information into variables, one of which is an array. I sort of understand this, but I don't know how to make my program know which parts of the textfile go into which variables.

my 4 textfiles are in the format: the list of e4-bc need to be entered into my array.

B
35
e4 e5 Bc4 Nc6 Nf3 Bc5 c3 Nf6
h3 Nxe4 d4 d5 Bb3 exd4 Nbd2 Nxf2
e2+ Qe7 Qxe7+ Nxe7 Kxf2 dxc3+ Kf1 Nf5
bxc3 Ng3+ Ke1 Nxh1 Bxd5 O-O g4 Nf2
Rb1 c6 Bc

my variables that those need to be inputted into are in this abstract data type class:

class Game

{

public:

char outcome; //(whether black or white team won)

int numMoves;

string arrayMoves;}

Solutions

Expert Solution

Given below is the code for the question. Please do rate the answer if it helped. Thank you.


#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;

class Game{
   public:
       char outcome;
       int numMoves;
       string moves[200];
       bool loadFromFile(string filename){
           ifstream in(filename.c_str());
           if(in.fail()){
               cout << "ERROR: could not load file " << filename << endl;
               return false;
           }
          
           in >> outcome;
           in >> numMoves;
           for(int i = 0; i < numMoves; i++)
               in >> moves[i];
           in.close();
           return true;
       }
      
       void print(){
           if(outcome == 'B')
               cout << "Black wins" << endl;
           else
               cout << "White wins" << endl;
          
           cout << "No. of moves: " << numMoves << endl;
           for(int i = 0; i < numMoves; i++){
               if(i % 8 == 0) //print 8 moves per line
                   cout << endl;
               cout << setw(8) << moves[i] ;
           }
       }
          
};

int main(){
   string filename;
  
   cout << "Enter game filename: ";
   cin >> filename;
  
   Game game;
   if(game.loadFromFile(filename))
   {
       cout << "Loaded game from file " << filename << endl;
       game.print();
   }
   return 0;
}


Related Solutions

I am Writing a C-Program to read and write files. but none of my code is...
I am Writing a C-Program to read and write files. but none of my code is working like it should be. Please fix all code and supply output response. Please try to use existing code and code in comments. But if needed change any code that needs to be changed. Thank you in advance //agelink.c //maintains list of agents //uses linked list #include <stdio.h> #include <stdlib.h> #define TRUE 1 void listall(void); void newname(void); void rfile(void); void wfile(void); struct personnel {...
I need to write a java program (in eclipse) that will read my text file and...
I need to write a java program (in eclipse) that will read my text file and replace specific placeholders with information provided in a second text file. For this assignment I am given a text file and I must replace <N>, <A>, <G>, with the information in the second file. For example the information can be John 22 male, and the template will then be modified and saved into a new file or files (because there will be multiple entries...
How do I do this: Write a program that can read a text file of numbers...
How do I do this: Write a program that can read a text file of numbers and calculate the mean and standard deviation of those numbers. Print the result in another text file. Put the result on the computer screen. EACH LINE OF THE PROGRAM MUST BE COMMENTED!
How do you use header files on a program? I need to separate my program into...
How do you use header files on a program? I need to separate my program into header files/need to use header files for this program.Their needs to be 2-3 files one of which is the menu. thanks! #include #include #include using namespace std; const int maxrecs = 5; struct Teletype { string name; string phoneNo; Teletype *nextaddr; }; void display(Teletype *); void populate(Teletype *); void modify(Teletype *head, string name); void insertAtMid(Teletype *, string, string); void deleteAtMid(Teletype *, string); int find(Teletype...
How do I get my program to ask the user to re enter the correct information...
How do I get my program to ask the user to re enter the correct information if the information entered does not match the database records? When I run my program it does let me know that the information entered does not match the database records but it does not ask me to enter the information again. Please add code (in bold) in the proper area with short explanation package MailMeSQL; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import...
How do I get my program to ask the user to re enter the correct information...
How do I get my program to ask the user to re enter the correct information if the information entered does not match the database records? When I run my program it does let me know that the information entered does not match but it does not ask me to enter the information again. package MailMeSQL; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.Scanner; import com.mysql.cj.jdbc.JdbcConnection; public class mailmeMain {    public static void...
Java Programming: Can I get an example of a program that will allow 4 options? Example...
Java Programming: Can I get an example of a program that will allow 4 options? Example Output: Choose on of the options below: 1. Display all items in CSV file. (CSV file includes for example brand of phone, size, price) 2. Pick an item by linear searching 3. Pick an item by bineary searching 4. Quit Program
I am trying to write a java program that determines if an inputted year is a...
I am trying to write a java program that determines if an inputted year is a leap year or not, but I am not able to use if else statements how would I do it. I don't need the code just advice.
Java Linked Lists I want a simple program that reads two text files that contains an...
Java Linked Lists I want a simple program that reads two text files that contains an integers matrix and store each file into a linked lists matrix so I can later preform operations such as addition and subtraction on the matrices an example of the input text files: sample a 2 6 2 6 2 18 17 11 20 sample b 3 13 5 4 11 20 13 18 20
I have extra columns with data showing up in my pivot table. How can I get...
I have extra columns with data showing up in my pivot table. How can I get rid of the extra columns?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT