c++ - I am designing a programming that calculates the odds of profiting if you played several scratch- off lottery games -
this code have written far calculate odds of profiting playing lottery scratchers. have prompt user out output file name (output.txt), formatted table results written. far program output results, not in output file. not sure how or go in code.
#include <iostream> #include <fstream> #include <string> #include <iomanip> using namespace std; int main() { // declaring variables int lowestamountofprofit; char filename[]="scratcher.txt"; string gamename; int costofticket, numberofprizes; int prizevalue, numberoftickets, ticketsnotclaimed; double remainingtickets; double remainingticketsforprofit; double odds; // program ask user enter lowest amount // profit when playing lottery. cout << "enter lowest dollar amount profit: " cin >> lowestamountofprofit; cout << "enter output file name: " cout << "generating report..." //open input file ifstream fin; fin.open(filename); //if input file not exist if (!fin) { // if input file cannot found. cout << "input file not exist." << endl; return 0; } //how output displace formatted. cout << left << setw(25) << "game" << setw(10) << "cost" << setw (10) << "odds" << endl; cout << "-----------------------------------------------" << endl; // reads name of game while (getline(fin, gamename)) { fin >> costofticket; // reads cost of ticket fin >> numberofprizes; // reads number of prizes remainingtickets = 0; remainingticketsforprofit = 0; (int = 0; < numberofprizes; i++) { fin >> prizevalue; // reads prize value fin >> numberoftickets; // reads total number of tickets fin >> ticketsnotclaimed; // reads number tickets not claimed //regardless of prize value*/ // following line computes running sum of number of tickets remaining game. remainingticketsforprofit = remainingticketsforprofit + ticketsnotclaimed; // next line compute sum of number of remaining tickets user profit. if (prizevalue > lowestamountofprofit) { remainingtickets = remainingtickets + ticketsnotclaimed; } } // tells program if there 0 remaining tickets if (remainingtickets==0) { // formats output cout << left << setw(25) << gamename << setw (2) << "$" << costofticket << right << setw(15) << "not possible" << endl; } else { // tells program calculate odds odds = remainingticketsforprofit / remainingtickets; cout << left << setw(25) << gamename << setw (2) << "$" << costofticket << right << setw(15) << "1 in " << setprecision(2) << fixed << odds << endl; } // read blank line string blankline; fin >> blankline; } // close input file fin.close(); return 0;
}
you can output file declaring ofstream
, using cout
. so:
ofstream outputfile; outputfile.open("filename.txt"); cout << "enter first number: "; cin >> num1; outputfile << num1 << endl; outputfile.close();
Comments
Post a Comment