#include <iostream>
#include <fstream>
#include <string>

using namespace std;


int main()
{
    ifstream fin;
    string firstName;
    string lastName;
    int horseNumber;
    int betAmount;
    
    fin.open("jockey.txt");
    if (!fin.good())
    {
        cerr << "File not found\n";
		return 1;
    }
	
    for (int i=0; i<10; i++)

	{
        fin >> firstName >> lastName >> horseNumber >> betAmount; 
        cout << "Name is : " << firstName << " " <<lastName << " " << horseNumber<< " " << betAmount <<endl;   

    }
        
    fin.close();
    system("pause");
    return 0;
}
"jockey.txt"
Peter Pan 1 33
Robin Hood 1 41
Jack Sparrow 2 90
Michael Jackson 3 47
Snoop Dogg 4 29
Peter Pan 2 12
Robin Hood 3 71
Jack Sparrow 3 49
Michael Jackson 3 47
Snoop Dogg 4 29
...
Robin Hood 3 5

hi all... got a assignment that needs some help... i'm trying to read from a text file called "jockey.txt" and calculate the bet amount total received from 4 different customers for 5 different jockeys... the 1st field is first name, followed by last name of the jockey... 3rd field is customer number and 4th is the amount...

their name may appear more than once in the txt file... the expected output would be something like this:


======== Cust 1_Cust 2_Cust 3_Cust 4_Total========
Peter Pan_____33_____0_____0_____0___33
Robin Hood___ 41_____0_____5_____0___46 (ignore all the underscores... they're suppose to be spaces...)

Most popular jockey: xxxx amount received xxxx
total amount received in all: xxxx

i'm really out of wits on how to do this... i can only store the input into variables at the moment only (the one i posted)... any pointers or advice would be greatly appreciated...! thanks in advance... :confused:

Do you know structures yet? If you do then you should create a structure that hold the information for each jockey. Read a line, search the array of structures to see if it is already in the array. If not, then add a new entry. If it is then just update it with the new information.

>> for (int i=0; i<10; i++)
Wrong way to write that loop. Here's a better way

while( fin >> firstName >> lastName >> horseNumber >> betAmount)
{
      cout << "Name is : " << firstName << " " <<lastName << " " << horseNumber<< " " << betAmount << "\n"; 
}

With the above it doesn't matter how many lines are in the file.

ok thanks a lot Ancient Dragon... your way is definitely better way of doing it... anyway I'm really stuck at the above codes... =( is there any example i can refer to... i dunno what command or codes to use... what code to use if i want to compare the first name and last name of the jockey, then add the bet amount based on the 4 different customers...

This article has been dead for over six months. Start a new discussion instead.