首页 > 解决方案 > 在文件(&语法)、比较结构和 c 字符串之间移动的问题

问题描述

提示第 1 部分

提示第 2 部分

该计划的要求在照片中。请注意,我只能使用我已经在我的代码中实现的库。我整天都在尝试,我知道这很混乱,不是最好的方法,但我想不出任何其他方法来获得我想要的东西。(请不要对我太苛刻,我很清楚我不是很聪明。这就是我问你的原因)这就是我所拥有的

#include <iostream>
#include<fstream>
#include <cstring>

using namespace std;

struct Move{
    char name[50];
    int damage;
};
struct Character{
    char name[50];
    char char_class[50];
    int hitPoints;
    int armorClass;
    bool isAlive;
    char moveName[50];
};
struct Sesh{
    char char1[50];
    char moveName[50];
    char target[50];
    int roll;
};
void session( Character c, ofstream& out);
int main() {
    Character c;
    int M, N, K;          //# of moves, # of characters
    Character *character;
    Move *moves;
    Sesh *sesh;
    /* To open an input file, that is, a file that contains data we need to
      * read into our program, a la cin, we need a variable of type ifstream
      */
    ifstream in1, in2;

    // The next step is to open the input file using the ifstream variable
    in1("character".c_str());

    // If, for some reason, we cannot open the file, print an error message
    if (!in1) {
        cout << "Error opening character.txt" << endl;
    } else {


        // file was opened successfully.
        // We can now use the ifstream variable just like cin, to read from the file
        //Read in number of moves
        in1 >> M;

        //declare the dynamic Move array of size 'M'
        moves = new Move[M];
        in1.ignore();// to get rid of preceding newline

        for (int i = 0; i < M; i++) {
            char tab;  // a char to read the separator character
            getline(in1, moves[i].name, '\t');
            in1 >> moves[i].damage;                          //do i need a \n here
            in1 >> '\n';
        }
        in1 >> N;
        character = new Character[N];
        in1.ignore();
        for (int i = 0; i < N; i++) {
            char tab;  // a char to read the separator character
            getline(in1, character[i].name, '\t');
            getline(in1, character[i].char_class, '\t');
            in1 >> character[i].hitPoints;
            in1 >> tab;
            in1 >> character[i].armorClass;
            in1 >> tab;
            getline(in1, character[i].moveName, '\n');
            //       isAlive=true;
        }
        cout << "Inputs read in successfully" << endl;
    }


    in2("session".c_str());

    if (!in2) {
        cout << "Error opening session.txt" << endl;
    } else {

        in2 >> K;                      //numAttacks

        sesh = new Sesh[K];
        in2.ignore();
        for (k = 0; k < 15; k++) {       // k<K keep getting error for infinity loop?
            char tab;  // a char to read the separator character
            getline(in2, character[k].name, '\t');
            getline(in2, sesh[k].moveName, '\t');
            getline(in2, sesh[k].target, '\t');
            in2 >> sesh[k].roll;
            in2 >> '\n';
        }
        for (i = 0; i < 9; i++) {
            for (k = 0; k < 15; k++) {
                if ((sesh[k].char1 == character[i].name) && (sesh[k].moveName == character[i].moveName)) {
                    for (int j = 0; j < N; j++) {
                        if (character[j].name == sesh[k].target) {
                            if (character[j].armorClass <= sesh[k].roll) {
                                for (int x = 0; x < M; x++) {
                                    if (moves[x].name == character[i].moveName) {
                                        character[j].hitPoints = character[j].hitPoints - moves[x].damage;
                                        if (character[j].hitPoints < 0) {
                                            character[j].isAlive = false;
                                        }
                                    }
                                }
                            }
                        }
                    }

                }


            }

        }
        ofstream out;
        out(alive.c_string);
        if (!out)
            cout << "error opening MovieOutputs.txt" << endl;
        else {
            //output file was opened successfully
            // We can use the ofstream variable just like cout, to print to the file
            for (int i = 0; i < N; i++)
                if (character[i].isAlive == 0) {         //0 is true?
                    print(character[i], out); // Use the print function to print
                    cout << "Wrote to output file";
                }

        }
        // once we are done with the file, we close it.
        in1.close();
        in2.close();

    }


    // We're done with the output file. Close it.
    out.close();
    //delete the dynamic array
    delete[] character;
    delete[] moves;
    delete[] sesh;
    // To run programs with files, we need to run using debugging.
    // The cin.get() makes the program wait for us to press a key before it terminates.
    cin.get();
}


    void print(Character c, ofstream &out)
    {
        // Just use the oftstream variable like cout.

        out << c.name <<  c.hitPoints << endl;
    }

标签: c++structfile-iostructurec-strings

解决方案


推荐阅读