【问题标题】:Pick a number from two columns and calculate从两列中选择一个数字并计算
【发布时间】: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


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

      你可以在代码末尾看到结果

      #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;
          }
      

      【讨论】:

      • 感谢您的代码,但最后出现了一些小问题。例如,如果我选择 09 是折腾数,winAmount = 10*70-15-21 = 664,而不是 700-15-21。如果投掷数为13也有问题,结果将打印出-101050-21。此外,如果文件在同一列中有超过 3 个数字怎么办。你知道如何解决这些问题吗?
      • 代码已编辑。我认为它会帮助你:) @TranNguyen
      • 非常感谢您的帮助。现在我试图弄清楚如何让代码在同一列中读取超过 3 个数字。你知道怎么做吗?另外,你能对代码做一些注释吗?
      • 超过 3 个数字是什么意思?是不是像 15 号一样具体?
      • 按数组读取文件时,大小固定。我们可以使用矢量来读取文件吗?我的意思是,如果文件ticket.txt 没有特定大小怎么办
      猜你喜欢
      • 2013-07-17
      • 1970-01-01
      • 2013-10-29
      • 1970-01-01
      • 2013-03-18
      • 1970-01-01
      • 2014-01-13
      • 1970-01-01
      • 2011-09-12
      相关资源
      最近更新 更多