程序未正确输出到输出文件

Program is not outputting correctly to the output file

本文关键字:输出 文件 程序      更新时间:2023-10-16

我对这个网站相当陌生,编程不是我的强项,所以如果我的措辞方式很难理解,我深表歉意。下面是我编写的代码,用于计算从玩彩票刮刮乐器中获利的几率。假设将结果输出到.txt文件。我能够让它输出到该文件,并且输出文件中的所有内容都是正确的,除了第二个游戏的名称。它缺少一个完整的单词。我的输出文件的外观如下所示。

Game                     Cost      Odds      
-----------------------------------------------
SMALL BEANS              $ 1          1 in 1.67
 BOOTY, ARRR             $ 10   Not possible
 MONEY HU$TLA$           $ 20          1 in 99.80

第二场和第三场比赛在开始之前都有一个空间,我不知道为什么。此外,第二场比赛应该说"海盗的战利品,Arrr"。我不明白为什么缺少一个完整的单词。有关如何解决此问题的任何帮助将不胜感激。我的代码如下。

#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
int main()
{
// Declaring Variables
int Profit;                         // The lowest dollar amount you want to profit
int CostOfTicket;                   // The cost of the ticket in the dollar amount
int NumberOfPrizes;                 // The number of possible prizes that can be won
int PrizeValue;                     // The value of the prize in dollars
int NumberOfTickets;                // The total number of tickets that were printed with that prize
int TicketsNotClaimed;              // The number of tickets with that prize that have not yet been claimed
double RemainingTickets;            // Total number of tickets that are remaining
double RemainingTicketsForProfit;   // The total number of tickets for a profit that are remaining
double Odds;                        // The odds of winning the game
string game;                        // The name of each game that can be played
string output;                      // The name of output file the user chooses (.txt)
// Open the input text file
ifstream inputfile ("scratcher.txt");  // Open the input file called "scratcher.txt"
// The program will ask the user to enter the lowest amount they would like to profit by when playing one of the lottery games.
// The games include "Small Beans," "Pirate's Booty, Arrr," and "Big Money Hu$tla$."
cout << "Enter the lowest dollar amount that you would like to profit: ";
cin >> Profit;
cout << "Enter the output file name: ";
cin >> output; //name of output file user chooses
ofstream outputfile (output.c_str()); //creates an output file with the name user chose for output
cout << "Generating report...";
// How the output will be formatted
outputfile << left << setw(25) << "Game" << setw(10) << "Cost" << setw (10) << "Odds" << endl;
outputfile << "-----------------------------------------------" << endl;
// Reads the name of the game
while (getline(inputfile, game))
{
    inputfile >> CostOfTicket; // Reads the cost of the ticket
    inputfile >> NumberOfPrizes; // Reads the number of prizes
    RemainingTickets = 0;
    RemainingTicketsForProfit = 0;
    for (int i = 0; i < NumberOfPrizes; i++)
    {
        inputfile >> PrizeValue; // Reads the value of the prize
        inputfile >> NumberOfTickets; // Reads the total number of tickets
        inputfile >> TicketsNotClaimed; // Reads the number of tickets that are not claimed

        RemainingTicketsForProfit = RemainingTicketsForProfit + TicketsNotClaimed;
        // The next line will compute a sum of the number of the remaining tickets where the user would profit
        if (PrizeValue > Profit)
        {
            // The following line computes the running sum of the number of tickets remaining for that game.
            RemainingTickets = RemainingTickets + TicketsNotClaimed;
        }
    }
    // Tells the program what to do if there are no tickets remaining
    if (RemainingTickets == 0)
    {
        // Formats the output
        outputfile << left << setw(25) << game << setw (2) << "$" << CostOfTicket << right << setw(15) << "Not possible"  << endl;
    }
    else
    {
        // Tells the program to calculate the odds.
        Odds = RemainingTicketsForProfit / RemainingTickets;
        outputfile << left << setw(25) << game << setw (2) << "$" << CostOfTicket << right << setw(15) << "1 in " << setprecision(2) << fixed << Odds << endl;
    }
    string blankLine;
    inputfile >> blankLine;
}
// Closes the input and output text file
inputfile.close();
outputfile.close();
return 0;

}

string blankLine;
inputfile >> blankLine;

不知道你为什么这样做,但它正在吞噬你下一行的第一个单词。

请记住,operator>>进入string跳过空格,然后恰好吃掉一个单词。

无论您想在跳过空白行方面做什么,这都不是该怎么做!