从输入文件创建输出文件但字符串打印不正确

时间:2016-04-23 17:43:53

标签: c++ string file-io getline

用户在开头输入他们想要获利的金额,并为输出创建.txt文件。但是输出没有给出游戏的完整字符串名称,如下所示。

我相信部分代码错误:

#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>

string gameName;
string blankLine;
double dollarAmount;
int costTicket, numPrize;
int prizeValue, totalTicket, notClaimed;
double remainTicket;
double remainTicketProfit;
double odds;


while( getline(fin >> ws, gameName)) 
{
        fin >> costTicket;     
        fin >> numPrize;       

        remainTicketProfit = 0;
        remainTicket = 0;

        for( int i = 0; i < numPrize; i ++)
        {
            fin >> prizeValue;  
            fin >> totalTicket; 
            fin >> notClaimed;  

            remainTicket =  remainTicket + notClaimed;

            if ( prizeValue > dollarAmount)
            {
                remainTicketProfit = remainTicketProfit + notClaimed;
            }
        }


        if(remainTicketProfit == 0)
        {
             myfile << left << setw(25) << gameName << setw(2) << "$" << setw(7) << costTicket << left << setw(17) << "Not possible" << endl;
        }

        else
        {

            odds = remainTicket / remainTicketProfit;

            myfile << left << setw(25) << gameName << setw(2) << "$" << setw(7) << costTicket  << "1 in " << fixed << setprecision(2) << odds << endl;
        }

        fin >> blankLine;
}

输入文件文字:

SMALL BEANS
1 5
10000 10 10
5000 50 8
100 100 6
10 1000 4
1 10000 2

PIRATE'S BOOTY, ARRR
10 6
50000 20 0
10000 100 0
1000 500 0
100 2000 738
10 7500 2945
1 10000 4476

BIG MONEY HU$TLA$
20 7
1000000 10 7
500000 50 29
10000 100 78
1000 500 396
100 2000 1439
20 5000 3218
10 10000 6210

它的输出文件:

Game Cost Odds
-----------------------------------------------
SMALL BEANS $ 2 Not possible   
BOOTY, ARRR $ 5 Not possible   
MONEY HU$TLA$ $ 20 1 in 1625.29

你可以看到字符串gameName没有完全显示,我不确定如何解决它。

0 个答案:

没有答案