从两列中选择一个数字并计算

Pick a number from two columns and calculate

给定的文件包含 <two-digit number, amount> 对。然后取一个抛出的两位数(称为 X),并计算 win/loss 的数量。 win/loss 规则是如果输入的数字与 X 匹配,则中奖,中奖总数为(数量 * 70);否则,损失 (-amount).

For example: [ticket.txt] 09 10 13 15 25 21

如果中奖号码为09,则win/loss的彩票数量为(10 * 70 - 15 - 21)

如果中奖号码为42,则win/loss的彩票数量为(-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;
    }

当您测试 line1[num] == winNum(以及之后执行的所有操作)时,您使用的是用 ++num; 修改的 num 的值,这意味着您为 line1line2 使用空值或无意义的值。例如,如果使用 "ticket.txt" 中显示的 3 行值,它们将存储在数组的位置 0、1 和 2,而 num 的末尾值为 4。

如果我理解你想要实现的目标,你应该将 if-else 语句放在从 0 到 num 的 for 循环中,然后对 line1line2 应该用循环变量作为索引来完成。 此外,如果您只想显示总金额,请将 cout 移到循环之后。

代码末尾可以看到结果

#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; 
    ifstream inFile2; 
    int rowNumber = 0;
    string line;
    inFile.open("Ticket.txt"); //open File
    inFile2.open("Ticket.txt");
    if (inFile.fail())
    {
        cout << "Fail to open the file" << endl;      
        return 1;
    }

     while (getline(inFile2, line))
        ++rowNumber;
    cout << "Number of lines in text file: " << rowNumber << "\n";


    int myArray[rowNumber][2];
    for(int i = 0; i < rowNumber; i++)
        for(int j = 0; j < 2; j++)
            inFile >> myArray[i][j];

    cout << "Numbers from File: " << endl;

    for(int i = 0; i < rowNumber; 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< rowNumber; 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;
    }