2016-04-24 42 views
-2

私はこのサイトをかなり新しくしていて、私の強い訴訟ではなくプログラミングしています。以下は、私が宝くじスクラッチャーをすることから利益を上げる確率を計算するために書いたコードです。結果を.txtファイルに出力すると仮定します。私はそれをそのファイルに出力することができます。出力ファイルのすべては、2番目のゲームの名前を除いて正しいものです。それは完全な言葉がありません。私の出力ファイルの外観は以下の通りです。出力ファイルにプログラムが正しく出力されていない

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

第2ゲームと第3ゲームの両方には、開始前に空き領域があり、理由はわかりません。また、2番目のゲームは、 "海賊の戦利品、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; 

}

+1

なぜ入力ファイルの内容がこの質問に答えることに関係していると思わなかったのですか? –

答えて

0
string blankLine; 
inputfile >> blankLine; 

あなたはこれをしなかったが、それはあなたの次の行の最初の単語を食べている理由はわかりません。

覚えておいてください、operator>>を入力してstringをスキップすると、空白文字は正確に1語を食べます。

空白行をスキップするために何をしようとしていても、これを行う方法ではありません!

+0

私はそれがなければ、それは最初の行(小さな豆)を印刷するだけだったので、次の2つのゲームを印刷する方法を理解できませんでした –

+0

推測によるプログラミングはうまくいかないでしょう。 –

+0

私はあまりプログラミングに堪能ではありません。それは私のために非常に厳しいと私はベストを尽くしています。 –

関連する問題