mardi 1 août 2017

Matching the first two or the last two digits of the 8-digit number

Given pairs of <2 digit-number, amount>, and an 8-digit number. It’s a win when a 2 digit number matches the first two or the last two digits of the 8-digit numbers.

For example: [ticket.txt] 
 (Number)  (Amount)
  09         10 
  13         15 
  25         21

1) Win number: 42341213

The win/loss amount of the ticket is (15 * 70 - 10 - 21 = 1019)

2) Win number: 13242213. If both the last and the first two digits match, the winning amount is doubled

The win/loss amount of the ticket is ((15 * 70 - 10 - 21) * 2 = 2038)

3) Win number: 12345678. If both the last and the first two digits don't match.

The win/loss amount of the ticket is (-10-15-21 = -46)

Extra: extend the solution to take in any number of digits.

My problem in this code that I could not match the number from [ticket.txt] with 8-digit numbers.

#include<iostream>
#include<vector>
#include<fstream>

using namespace std;

vector<vector<int>> readNumberFromFile(const string& filename)
{
    ifstream inFile(filename);
    vector<int> temprow(2); // input two pairs of columns into vector
    vector<vector<int>> myArray{}; // input 2d vector

    // count the numbers in row
    while (inFile >> temprow[0] >> temprow[1])
    {
        myArray.push_back(temprow);
    }

    return myArray;
}

int computeWinLoss(vector<vector<int>> number,int tossUpNum)
{
    int win = 0, lost = 0, result;
    for(int i = 0; i< number.size(); i++)
    {
        if (number[i][0] == tossUpNum)
        {
            win = number[i][1] * 70;
        }
        else
        {
            lost = lost + number[i][1];
        }
    }
    result = win - lost;
    return result;
}


int main(){
    int winNum;
    vector<vector<int>> myArray = readNumberFromFile("Ticket.txt");

    // display the numbers from file on screen
    cout << "Numbers from file: " << endl;
    for (int i = 0; i < myArray.size(); i++){
        for (int j = 0; j < 2; j++){
            std::cout << myArray[i][j] << ' ';
        }
        cout << endl;
    }
    cout << endl;
    cout << "Enter the toss-up number: "; // enter the win number
    cin >> winNum;

    int result = computeWinLoss(myArray, winNum);
    cout << "Your result: " << result;

    cout << endl << endl;
    system("pause");
    return 0;
}

Aucun commentaire:

Enregistrer un commentaire