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 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.
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?
How can I make this program able to implement listener for the editable text field: ///////////////////////////////...
How can I make this program able to implement listener for the editable text field: /////////////////////////////// KiloConverter.java ////////////////////////// import java.awt.BorderLayout; import java.awt.Color; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextField; public class KiloConverter extends JFrame { // constant for converting km to miles static final double KM_TO_MILES = 0.6214; // important components that needs to be accessed throughout the program private JTextField input, output; // constructor initializing GUI public KiloConverter() { // passing title...
This program is supposed to identify whether I inputted a number or a letter through enumerated...
This program is supposed to identify whether I inputted a number or a letter through enumerated types and an array of messages. I'm aware that you can't compare strings and enums but I'm out of ideas. What method do you suggest. #include <iostream> #include <iomanip> using namespace std; int main() {    enum letters { A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT