输出文件时遇到问题

时间:2015-10-13 05:28:41

标签: c++ fstream

我为我的c ++类制作了一个简单的骰子游戏,由于某种原因,txt文件在游戏完成时无法写入。一切都应该顺利进行,我不知道这些代码有多少相关,但底部的主要功能是打开/写入/关闭文件的代码。打印屏幕功能几乎肯定不会导致它,但我离开它以防运行代码测试它有帮助。

#include <iostream>
#include <ctime>
#include <iomanip>
#include <cmath>
#include <fstream>
#include <cstdlib>
#include <string>
using namespace std;
int dice, tempScore, devilTempScore, wins, losses, devilTurnNum;
char c;
bool devilSwap;
int score = 0;
int devilScore = 0;

char input() {
    bool valid = 0;

    while (!valid) {
        cout << "[p]ass, [r]oll, or [f]orfeit: ";
        cin >> c;
        if (c == 'p' || c == 'r' || c == 'f') valid = true;
        else cout << "Input invalid\n";
    }
    return c;
}
void roll() {
    dice = (rand() % 6) + 1;
}

void devilScoreAdd() {
    devilScore = devilTempScore;
    devilSwap = false;
    }

void devilRoll() {
    if (devilTurnNum <= 3){
        roll();
        if (dice == 1) {
            devilTempScore = devilScore;
            devilScoreAdd();
            }
        else {
             devilTempScore += dice;
                devilTurnNum += 1;
            }

        }
    else {
        devilTurnNum = 0;
        devilScoreAdd();
    }
}

void devilsTurn() {
    while (devilSwap == true) {
        devilRoll();
    }
    return;
}

void scoreAdd() {
    score = tempScore;
    if (score < 100) {
        devilSwap = true;
        devilsTurn();
    }
    else { wins += 1;
    c = 'win';
    }
    return;
}

void printScreen() {

    cout << "         Player               Devil \n";
    cout << "          100                  100 \n";
    cout << "       "; if (tempScore <= 99 && tempScore >= 95) { cout << tempScore << ">"; }
    else { cout << "   "; }cout << "95                   95"; if (devilTempScore <= 99 && devilTempScore >= 95) { cout << "<" << devilTempScore << " \n"; }
    else { cout << " \n"; }
    cout << "       "; if (tempScore <= 94 && tempScore >= 90) { cout << tempScore << ">"; }
    else { cout << "   "; }cout << "90        die        90"; if (devilTempScore <= 94 && devilTempScore >= 90) { cout << "<" << devilTempScore << " \n"; }
    else { cout << " \n"; }
    cout << "       "; if (tempScore <= 89 && tempScore >= 85) { cout << tempScore << ">"; }
    else { cout << "   "; }cout << "85        ___        85"; if (devilTempScore <= 89 && devilTempScore >= 85) { cout << "<" << devilTempScore << " \n"; }
    else { cout << " \n"; }
    cout << "       "; if (tempScore <= 84 && tempScore >= 80) { cout << tempScore << ">"; }
    else { cout << "   "; }cout << "80        |" << dice << "|        80"; if (devilTempScore <= 84 && devilTempScore >= 80) { cout << "<" << devilTempScore << " \n"; }
    else { cout << " \n"; }
    cout << "       "; if (tempScore <= 79 && tempScore >= 75) { cout << tempScore << ">"; }
    else { cout << "   "; }cout << "75        ___        75"; if (devilTempScore <= 79 && devilTempScore >= 75) { cout << "<" << devilTempScore << " \n"; }
    else { cout << " \n"; }
    cout << "       "; if (tempScore <= 74 && tempScore >= 70) { cout << tempScore << ">"; }
    else { cout << "   "; }cout << "70                   70"; if (devilTempScore <= 74 && devilTempScore >= 70) { cout << "<" << devilTempScore << " \n"; }
    else { cout << " \n"; }
    cout << "       "; if (tempScore <= 69 && tempScore >= 65) { cout << tempScore << ">"; }
    else { cout << "   "; }cout << "65                   65"; if (devilTempScore <= 69 && devilTempScore >= 65) { cout << "<" << devilTempScore << " \n"; }
    else { cout << " \n"; }
    cout << "       "; if (tempScore <= 64 && tempScore >= 60) { cout << tempScore << ">"; }
    else { cout << "   "; }cout << "60                   60"; if (devilTempScore <= 64 && devilTempScore >= 60) { cout << "<" << devilTempScore << " \n"; }
    else { cout << " \n"; }
    cout << "       "; if (tempScore <= 59 && tempScore >= 55) { cout << tempScore << ">"; }
    else { cout << "   "; }cout << "55                   55"; if (devilTempScore <= 59 && devilTempScore >= 55) { cout << "<" << devilTempScore << " \n"; }
    else { cout << " \n"; }
    cout << "       "; if (tempScore <= 54 && tempScore >= 50) { cout << tempScore << ">"; }
    else { cout << "   "; }cout << "50                   50"; if (devilTempScore <= 54 && devilTempScore >= 50) { cout << "<" << devilTempScore << " \n"; }
    else { cout << " \n"; }
    cout << "       "; if (tempScore <= 49 && tempScore >= 45) { cout << tempScore << ">"; }
    else { cout << "   "; }cout << "45                   45"; if (devilTempScore <= 49 && devilTempScore >= 45) { cout << "<" << devilTempScore << " \n"; }
    else { cout << " \n"; }
    cout << "       "; if (tempScore <= 44 && tempScore >= 40) { cout << tempScore << ">"; }
    else { cout << "   "; }cout << "40                   40"; if (devilTempScore <= 44 && devilTempScore >= 40) { cout << "<" << devilTempScore << " \n"; }
    else { cout << " \n"; }
    cout << "       "; if (tempScore <= 39 && tempScore >= 35) { cout << tempScore << ">"; }
    else { cout << "   "; }cout << "35                   35"; if (devilTempScore <= 39 && devilTempScore >= 35) { cout << "<" << devilTempScore << " \n"; }
    else { cout << " \n"; }
    cout << "       "; if (tempScore <= 34 && tempScore >= 30) { cout << tempScore << ">"; }
    else { cout << "   "; }cout << "30                   30"; if (devilTempScore <= 34 && devilTempScore >= 30) { cout << "<" << devilTempScore << " \n"; }
    else { cout << " \n"; }
    cout << "       "; if (tempScore <= 29 && tempScore >= 25) { cout << tempScore << ">"; }
    else { cout << "   "; }cout << "25                   25"; if (devilTempScore <= 29 && devilTempScore >= 25) { cout << "<" << devilTempScore << " \n"; }
    else { cout << " \n"; }
    cout << "       "; if (tempScore <= 24 && tempScore >= 20) { cout << tempScore << ">"; }
    else { cout << "   "; }cout << "20                   20"; if (devilTempScore <= 24 && devilTempScore >= 20) { cout << "<" << devilTempScore << " \n"; }
    else { cout << " \n"; }
    cout << "       "; if (tempScore <= 19 && tempScore >= 15) { cout << tempScore << ">"; }
    else { cout << "   "; }cout << "15                   15"; if (devilTempScore <= 19 && devilTempScore >= 15) { cout << "<" << devilTempScore << " \n"; }
    else { cout << " \n"; }
    cout << "       "; if (tempScore <= 14 && tempScore >= 10) { cout << tempScore << ">"; }
    else { cout << "   "; }cout << "10                   10"; if (devilTempScore <= 14 && devilTempScore >= 10) { cout << "<" << devilTempScore << " \n"; }
    else { cout << " \n"; }
    cout << "       "; if (tempScore <= 9 && tempScore >= 5) { cout << tempScore << " >"; }
    else { cout << "   "; }cout << " 5                   5 "; if (devilTempScore <= 9 && devilTempScore >= 5) { cout << "< " << devilTempScore << " \n"; }
    else { cout << " \n"; }
    cout << "       "; if (tempScore <= 4 && tempScore >= 0) { cout << tempScore << " >"; }
    else { cout << "   "; }cout << " 0                   0 "; if (devilTempScore <= 4 && devilTempScore >= 0) { cout << "< " << devilTempScore << " \n"; }
    else { cout << " \n"; }

}


int main() {
    srand(time(nullptr));
    fstream fin, fout;
    wins = 0;
    losses = 0;
    fin.open("devilDiceRecord.txt");
    if (fin) {
        fin >> wins;
        fin >> losses;
        fin.close();
    }

    while (c != 'f' && score <= 100 && devilScore <= 100 && c != 'win') {

        printScreen();

        input();
        if ( c == 'r') {
            roll();
            if (dice == 1) {
                tempScore = score;
                scoreAdd();
            }
            else { tempScore += dice; }
        }
        if (c == 'p') {
            scoreAdd();
        }
    }
    if (c == 'f' || devilScore >= 100) {
        losses += 1;
    }
    if (c == 'win') {
        cout << "Congradulations, you won!";
        }
    fout.open("devilsDiceRecord.txt");
    fout << wins << "\n";
    fout << losses;
    fout.close();
}

0 个答案:

没有答案