The following are the requirements of the program and all code can be written within one C++ source file for the purpose of this assignment.
1. Declare three structs to store Athlete, Event and Registration details. The struct definitions are provided in Appendix 1.
2. Write the driver code (main) and declare an array object of type Registration, assuming you can have a maximum of 20 registrations. Also, declare a pointer for this array object that you can use to access the array elements in your program.
3. In the driver, write code to display a menu for the user with the following options. Each menu item may be selected by the user using the corresponding menu number. The menu should repeat after each selection until user enters 7 for exit.
1 – Read and display registration file
2 – List all athletes by event
3 – List all athletes by country
4 – Read and display result file
5 – List medal counts by country
2 CS112 Assignment 1, 2012
6 – Display medal tally
7 – Exit
Implement each of the above menu options (1-6) in separate functions that you can call from your driver program. Below are the descriptions for each function, with the function prototypes given in Appendix 1 as a guide.
1 - The registration.txt file contains the registration ID, athlete and event details for each participating athlete. The function takes the registration array pointer (declared in step 2) as an input parameter, together with an integer size (passed as reference and used to count the number of records read in the array). The function should first ask the user to enter the complete path and filename for the input file (example C:\registration.txt), which is read and stored in the registration array using the pointer. The function should then display the contents of the input file as read in the registration array.
2 - The function takes the registration array pointer, size and an event ID (which is entered by the user within the driver) as inputs and displays the athlete ID, name and country of all athletes registered for that event.
3 - The function takes the registration array pointer, size and a country name (which is entered by the user within the driver) as inputs and displays the athlete ID, name and event name for all athletes representing that country.
4 - The results.txt file contains the event ID and top three athlete IDs (representing gold, silver and bronze winners for that event). Assume there are no ties, no multiple medal winners and the input file can contain a maximum of 20 records (results) at a time. In your driver, declare a 2-D integer array (size 20x4) that can store the contents of the input file. The function should then take this 2-D array and an integer size (passed as reference and used to count the number of records read in the array) as inputs. It should first ask user to enter the complete path and filename for the input file (example C:\result.txt), which is read and stored in the 2-D array appropriately. The function should then display the contents of the input file as read in the array.
5 - The function takes the registration array pointer, size, 2-D array, size and a country name (which is entered by the user within the driver) as inputs and displays the country name and the total number of gold, silver and bronze medals won by that country.
6 - The function takes the registration array pointer, size, 2-D array and size as inputs and lists all countries who have won a medal and display the total count of gold, silver and bronze medals for each country in some appropriate format (it is not necessary to rank or sort the countries in any particular order).

Can somebody help me with this?
I dont know where to start from. And comments as well for explanations.

Recommended Answers

All 8 Replies

Can somebody help me with this?
I dont know where to start from. And comments as well for explanations.

Start by declaring three structs to store Athlete, Event and Registration details. The struct definitions are given in Appendix 1. Are you able to do this part of the problem? If not, which element of this part of the problem are you having trouble with? Are you comfortable with your understanding of structs?

I am having problems with the functions of this programs.
This is so far i have done.

#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <string>

using namespace std;

const int REGISTRATION = 20; // up to 20 players can register.(array size of 20)

struct Athlete
{
    int aID;
    string aName;
    int aAge;
    char aGender;
    string aCountry;
}
struct Event
{
    int eID;
    string eName;
    string eRecord;
}
struct Registration
{
    int rID;
    Athlete rAthlete;
    Event rEvent;
}

void ReadRegistrations (Registration *regPtr, int &size);
void ListAthletesByEvent (Registration *regPtr, int size, int eID);
void ListAthletesByCountry (Registration *regPtr, int size, string country);
void ReadResults (int results[ ][4], int &size);
void ListMedalsByCountry (Registration *regPtr, int size1, results[ ][4], int size2, string country);
void ListMedalTally (Registration *regPtr, int size1, results[ ][4], int size2);

int main()
{
    Registration game[REGISTRATION];
    int results;


    Registration *regPtr;
    regPtr = game;

    cout << " 1 - Read and display registration file" << endl;
    cout << " 2 - List al athletes by event" << endl;
    cout << " 3 - List all athletes by country" << endl;
    cout << " 4 - Read and display result file" << endl;
    cout << " 5 - List medal counts by country" << endl;
    cout << " 6 - Display meadl tally" << endl;
    cout << " 7 - Exit" << endl;

    char response;

    if (response == 1){
        int i= 0;
        ReadRegistrations (game, i);
    }


    system ("PAUSE");
    return 0;
}

void ReadRegistrations (Registration *regPtr, int &size)
{
    char c;
    string registration;
    ifstream input;

    input.open ("C:\\Documents and Settings\\Krishal\\Desktop\\CS112 Assignment\\registration.txt");

    while (input >> ws && !input.eof())
    {
        input >> (*regPtr).rEvent.eID; // store rID

    }
}
void ListAthletesByEvent (Registration *regPtr, int size, int eID)
{
    for(int i = 0; i < size; i++){
        cout << (*regPtr).rEvent.eID << endl;
    }

}

There is also an error in the first function which is void ReadRegistrations()

I haven't checked your code yet, but what kind of problems are you having? Does this compile? Only, I'd expect to see a semicolon terminator for your structs, which makes me wonder if you're fighting compilation errors. e.g.

struct Athlete
{
    int aID;
    string aName;
    int aAge;
    char aGender;
    string aCountry;
};

Can you elaborate on what errors you're seeing?

In the following lines you've forgotten to provide the type for the results parameter:

void ListMedalsByCountry (Registration *regPtr, int size1, results[ ][4], int size2, string country);
void ListMedalTally (Registration *regPtr, int size1, results[ ][4], int size2);

Here, you're using response before it's been assigned a value:

char response;
if (response == 1){
    int i= 0;
    ReadRegistrations (game, i);
}

Hope that helps.

#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <string>

using namespace std;

const int REGISTRATION = 20; // up to 20 players can register.(array size of 20)
const int RESULTS= 4;


struct Athlete
{
    int aID;
    string aName;
    int aAge;
    char aGender;
    string aCountry;
};
struct Event
{
    int eID;
    string eName;
    string eRecord;
};
struct Registration
{
    int rID;
    Athlete rAthlete;
    Event rEvent;
};

void ReadRegistrations (Registration *regPtr, int &size);
void ListAthletesByEvent (Registration *regPtr, int size, int eID);
void ListAthletesByCountry (Registration *regPtr, int size, string country);
void ReadResults (int results[ ][4], int &size);
void ListMedalsByCountry (Registration *regPtr, int size1, int results[ ][4], int size2, string country);
void ListMedalTally (Registration *regPtr, int size1, int results[ ][4], int size2);

int main()
{
    Registration game[REGISTRATION];
    Registration results[REGISTRATION][RESULTS];
    int i=0;
    int j=0;

    Registration *regPtr;
    regPtr = game;
    char response;
    response = 0;

    cout << " 1 - Read and display registration file" << endl;
    cout << " 2 - List all athletes by event" << endl;
    cout << " 3 - List all athletes by country" << endl;
    cout << " 4 - Read and display result file" << endl;
    cout << " 5 - List medal counts by country" << endl;
    cout << " 6 - Display meadl tally" << endl;
    cout << " 7 - Exit" << endl;
    cin >> response;
    string country;

    if (response == 1){
    ReadRegistrations(game, i);
    }
    regPtr = game;
    ListAthletesByEvent (game, i, response);
    regPtr = game;
    ListAthletesByCountry (game, i, country);
    regPtr = game;

    system ("PAUSE");
    return 0;
}

void ReadRegistrations (Registration *regPtr, int &size)
{

    cout << "Enter file name:" << endl;
    string filename;
    cin >> filename;
    ifstream input;
    input.open ("C:\\Documents and Settings\\Krishal\\Desktop\\registration.txt");
    input.close();

}
void ListAthletesByEvent (Registration *regPtr, int size, int eID)
{
    for(int i = 0; i < size; i++){
        cout << (*regPtr).rEvent.eID << endl;
    }
    *regPtr ++ ;
}

void ListAthletesByCountry (Registration *regPtr, int size, string country)
{
    for(int i = 0; i < size; i++){
        cout << (*regPtr).rAthlete.aCountry << endl;
    }
    *regPtr ++ ;
}
void ReadResults (int results[ ][4], int &size)
{
    cout << "Enter file name:" << endl;
    string filename;
    cin >> filename;
    ifstream input;
    input.open ("C:\\Documents and Settings\\Krishal\\Desktop\\results.txt");
    input.close();
}
void ListMedalsByCountry (Registration *regPtr, int size1, int results[ ][4], int size2, string country)
{
    for(int i = 0; i < size1; i++){
        cout << (*regPtr).rAthlete.aCountry << endl;
        for (int j=0;j< size2 ; j++){
        cout << (*regPtr).rEvent.eRecord << endl;
        }
    }
    *regPtr ++ ;
}
void ListMedalTally (Registration *regPtr, int size1, int results[ ][4], int size2)
{
    for(int i = 0; i < size1; i++){
        cout << (*regPtr).rAthlete.aCountry<< endl;
        for (int j=0;j< size2 ; j++){
        cout << (*regPtr).rEvent.eRecord << endl;
        }
    }
    *regPtr ++ ;
}

This is what i have done last night. Thanks for the semicolon terminator. im so forgetful.
COuld you edit this code snippet for me? so that it could work?

so that it could work?

Post your problem / which part is not working.

Try making response an int, or check for a value of '1' afterwards.

char response;
//...
if (response == 1){
ReadRegistrations(game, i);
}

e.g.

int response;
//...
if (response == 1){
ReadRegistrations(game, i);
}

or

char response;
//...
if (response == '1'){
ReadRegistrations(game, i);
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.