PDA

View Full Version : I need help with this program?


splackavellie69
12-19-2006, 02:29 AM
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>

using namespace std;


struct dormgames
{
string firstname;
string lastname;
string dorm;
string sport1;
string sport2;
float money;
};

float total1;
float total2;
float total3;
float total4;
float total5;
float total6;
float money1;
float money2;
ifsream infile;

int main()
{
infile.open(“sports.dat”)

while (infile)
{
infile >> dormgames.firstname >> dormgames.lastname >> dormgames.dorm
>> dormgames.sport1 >> dormgames.sport2 >> dormgames.money;



}

return 0;
}

Problem: Your brother has heard that you have learned to write computer programs. Assuming that you can do anything, he has told his friend that you can help him. His friend is the director of inter-dormitory sports here at Howard University. So, last night his friend called you and asked you to write a computer program that will answer the following questions:

1. What is the total amount of money spent on each sport ?
2. What is the total amount of money spent at each of the dormitories?
3. What is the total amount of money spent on each of the sports at each of the
dormitories?
4. What is the total number students participating in each of the sports?
5. What is the total number students participating at each of the dormitories?
6. What is the total number students participating in each of the sports at each of the
dormitories?

Input: Read in a file of 25 records from an external file sports.dat that satisfy the following format:

type of datum meaning range of values

character string student name up to 24 letters

2 characters dormitory {BT, DG, FT, TU, OP, LN}

character string 1st sport played {FOOTBALL, TENNIS, SOCCER,
by this student BASKETBALL}


character string 2nd sport played {FOOTBALL, TENNIS, SOCCER,
by this student BASKETBALL}

Float amount of money $0 – $100.00
spent on this student
(divide by 2 for each
sport if student has
played 2 sports)

To test your program, create an external file named sports.dat and place in it a set of 25 records as described above. Your program should read in this data, store the data in a struct them pass this struct (record) to a function that adds to the appropriate element in a multi-dimensional array (also passed as a parameter). If the student has two sports then split the money in half.

Output: Calculate the total amount in dollars and cents and the total number of students as described above. Provide an output listing that is appropriately formatted and that presents the answers to the questions along with your input data.

Extra Requirements: Use enumerated data type(s). Use a void function to write out your name, course number and title, assignment number, and date, followed by a description of the problem. Use a function somewhere in your calculation that has a struct and a multi-dimensional array as parameters.

i am completely confused i understand the strut and how to use it i just dont kno how to get my program to answer the quwstions needed can someone please help???

oracleguy
12-19-2006, 03:55 AM
OK, well a struct is like a type of object (similar to a variable). Since you have a maximum of 25 records and you'll want one copy of the struct for each record, you'll want to use an array. e.g.
dormgames records[25];

Now using this, you can read in all the records in the file using a while loop like you are already doing and incrementing the array index each time.

Once you have your program loaded with all the data, you can calculate all the requested data.

For example, the first one, you can loop through the array of records (not to the maximum upper bound but to how many you read in from the file) and if the sport is say tennis and there are two sports, divide the money by two and add it to a variable for the total money going to tennis. And have a variable for each sport and then when you are done looping, you can output the amount of money spent on the different sports.

You can make the program more efficient and calculate all the needed information by only looping through the records once. But once you get your program reading in the records, start which each piece of information needed and add that in and test it. Once it works, then add the next piece and so on.

Also in the future, please put code you post in the [code ] [/code ] brackets (without the space before the ] of course though).

splackavellie69
12-19-2006, 05:08 AM
i'm not fully understanding how you are wanting me to store the values of the money with the sports i was thinking of using a switch statement but i dont think that would work that well

oracleguy
12-19-2006, 05:37 AM
Well first focus on reading the data from the input file, without that, you can't really do the calculating part.

Here is the basic pseudo code for reading the data in:
Open input file.
Use integer variable x as the array index and start it at 0

While there are records in the file
Read a record from the file and store it in the array at index x
Increment x
Loop

Save the value of x as the number of records read in
Close the input file