【发布时间】:2023-03-25 18:21:02
【问题描述】:
给定文件包含成对的<two-digit number, amount>。然后取一个折腾的两位数(称为 X),并计算赢/输金额。输赢规则是如果输入的数字与X匹配,则为中奖,中奖总数为(金额*70);否则就是损失(-amount)。
For example: [ticket.txt] 09 10 13 15 25 21
如果投掷号码为09,则该彩票的赢/输金额为(10 * 70 - 15 - 21)
如果投掷数为42,则该票的赢/输金额为(-10 - 15 - 21)。
这是我的初学者项目。我坚持计算获胜金额和损失金额。 This is my problem
#include <iostream>
#include <fstream>
using namespace std;
int line1[100]; // array that can hold 100 numbers for 1st column
int line2[100]; // array that can hold 100 numbers for 2nd column
int main()
{
int winNum, winAmount, lostAmount;
int num = 0; // num start at 0
ifstream inFile;
inFile.open("Ticket.txt"); //open File
if (inFile.fail())
{
cout << "Fail to open the file" << endl;
return 1;
}
cout << "Numbers from File: " << endl;
while (!inFile.eof()) // read File to end of file
{
inFile >> line1[num]; // read first column, the first column is the number that user choosing
inFile >> line2[num]; // read second column, the second column is the amount of money that user paying
cout << "\n" << line1[num] << "\t" << line2[num];
++num;
}
inFile.close();
cout << endl;
cout << "Enter the toss-up number: "; // enter the win number
cin >> winNum;
if (line1[num] == winNum)
{
winAmount = line2[num] * 70; // number user choose = win number, winAmount = winAmount * 70 - lostAmount
cout << winAmount;
}
else
{
lostAmount =- line2[num]; //number user choose != win number, the amount will be -lost amounts
cout << lostAmount;
}
cout << endl << endl;
system("pause");
return 0;
}
【问题讨论】:
标签: c++ arrays while-loop fstream calculated-columns