指定されたファイルには、のペアが含まれています。その後、2桁の数字(Xと呼ぶ)を取り上げ、勝敗を計算します。勝敗ルールは、入力番号がXと一致した場合に勝ち、勝利合計は(金額* 70)です。それ以外の場合は、( - 量)の損失です。固定サイズの配列をベクトルに変更する
For example: [ticket.txt] 09 10 13 15 25 21
トスアップ数は、チケットの勝利/損失量は、09であれば(10 * 70から15 - = 664 21)
toss-場合チケットの勝敗は(-10 - 15 - 21 = -46)である。
ファイルを配列で読み取るのは固定サイズですが、つまり、ファイルticket.txt
にサイズが指定されていないとどうなりますか?誰かが配列をベクトルに読み替えたり、サイズを固定していないものを変更したりできますか?
For example: [ticket.txt] 09 10 13 15 25 21 .. ..
#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 = 0, lostAmount = 0, result = 0;
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;
}
int myArray[3][2];
for(int i = 0; i < 3; i++)
for(int j = 0; j < 2; j++)
inFile >> myArray[i][j];
cout << "Numbers from File: " << endl;
for(int i = 0; i < 3; i++)
{
for(int j = 0; j < 2; j++)
{
cout << myArray[i][j] << " ";
}
cout << "\n";
}
cout << endl;
cout << "Enter the toss-up number: "; // enter the win number
cin >> winNum;
for(int i = 0; i< 3;i++)
{
if (myArray[i][0] == winNum)
{
winAmount = myArray[i][1] * 70; // number user choose = win number, winAmount = winAmount * 70 - lostAmount
}
else
{
lostAmount = lostAmount + myArray[i][1]; //number user choose != win number, the amount will be -lost amounts
}
}
result = winAmount - lostAmount;
cout << result;
cout << endl << endl;
system("pause");
return 0;
}