I'm having trouble with the two-dimensional arrays and passing them as parameters. I understand the struct and its purpose, but the arrays are giving me difficulties. I know that two-dimesional arrays produce a table aspect rather than a list (single array). I'm not sure on how to read the data into the arrays or receive it and output it. I'll will attach my program and it has comments on the particular parts I'm having trouble with. Thank you.
//****************************************************************
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?
//****************************************************************
Requirements
Input
Read in a file of 25 records from an external file infile.txt that satisfy the following format:
TYPE OF DATA MEANING RANGES OF VALUES
character string student name up to 24 letters
2 characters dormitory {BT, DG, FT, TU, OP, LN}
integer Number of sports played by this student
character string 1st sport played {FOOTBALL, TENNIS, SOCCER, BASKETBALL}
by this student
character string 2nd sport played {FOOTBALL, TENNIS, SOCCER, BASKETBALL}
by this student (if applicable)
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 infile.txt and place in it a set of 25 records as described above and presented below.
Tony Danza TU 1 FOOTBALL 50.00
Trevor Pryce DG 2 FOOTBALL SOCCER 100.00
Shirley Taylor BT 1 TENNIS 50.00
Matthew Herring FT 2 FOOTBALL TENNIS 100.00
Geroge Watson LN 1 SOCCER 50.00
Gregory Cherry LN 1 SOCCER 50.00
Troy Glover BT 1 FOOTBALL 50.00
Tammy Price OP 1 TENNIS 50.00
Brandon Thompson FT 2 FOOTBALL SOCCER 100.00
Terrance Williams TU 1 FOOTBALL 50.00
Brian Willis OP 2 FOOTBALL TENNIS 100.00
Pamela Willis FT 1 TENNIS 50.00
Cathy Friem OP 1 SOCCER 50.00
Nathaniel Bentley LN 1 FOOTBALL 50.00
Hansel Brogavich BT 1 TENNIS 50.00
Randy Moss TU 2 BASKETBALL FOOTBALL 100.00
Al Wilson OP 1 FOOTBALL 50.00
Precious Jamison DG 1 TENNIS 50.00
DeShawn Jackson OP 2 SOCCER BASKETBALL 100.00
Matthew McConey TU 2 BASKETBALL FOOTBALL 100.00
Ashley Jones TU 2 BASKETBALL TENNIS 100.00
Clinton Portis DG 2 SOCCER FOOTBALL 100.00
Terrance Richards BT 1 SOCCER 50.00
Angel Lewis OP 2 BASKETBALL 50.00
Rashad Jenkins BT 2 FOOTBALL BASKETBALL 100.00
//****************************************************************
Your program should read in this data, store the data in a struct then pass this struct (record) to a function that adds to the appropriate element in a multi-dimensional array (also passed as a parameter).
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.
Use must enumerated data type(s) where appropriate.
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.
Program Output
First print out the your name, course number and title, assignment number, and date, followed by a description of the problem. Then print the input data with appropriate headings, followed by the answer to the problem. All money amounts must be printed as dollar and cents amounts.
//****************************************************************
So it's not letting me attach my cpp file so I have to copy and paste it. Hopefully this doesn't cause a problem. If there's another way to attach my file, please let me know. Thank you.
//****************************************************************
//This program answers corresponding questions
//I'm having output errors
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;
//Enum variable declarations
enum Sport{FOOTBALL, BASKETBALL, TENNIS, SOCCER};
enum Dorm{BT, DG, FT, TU, OP, LN};
struct StudentRecord
//User defined struct to hold the student'd information
{
string firstname;
string lastname;
Dorm dorm_name;
int num_of_sports;
Sport first_sport;
Sport second_sport;
float money_cost;
};
//Function prototypes
void heading();
StudentRecord Read_Student_Record(ifstream&, StudentRecord []);
float Compute_Total_Cost(int[][6], float[][6]);
int main()
{
int table_count[4][6];
float table_money[4][6], total_cost;
int count=0;
StudentRecord studentinfo[25];
ifstream mystudents;
mystudents.open("infile.txt");
heading();
while (mystudents)
{
studentinfo[count++] = Read_Student_Record(mystudents, studentinfo);
}
//Determine the table and set it to 0
for (int sports = 0; sports < 4; sports++)
for (int dorm = 0; dorm < 6; dorm++)
{
table_count[sports][dorm]=0;
table_money[sports][dorm]=0.0;
}
total_cost = Compute_Total_Cost(table_count, table_money);
cout<<total_cost<<endl;
return 0;
}
StudentRecord Read_Student_Record(ifstream& mystudents, StudentRecord student_table[])
{
string DORM, sport;
int count;
for (int count = 0; count < 25; count++)
{
//Read the data
//Having problems with the binary operator
mystudents>>student_table[count].firstname>>student_table[count].lastname>>student_table[count].dorm_name>>
student_table[count].num_of_sports>>student_table[count].first_sport>>student_table[count].second_sport>>
student_table[count].money_cost;
if (DORM == "BT")
student_table[count].dorm_name= BT;
else if (DORM == "DG")
student_table[count].dorm_name= DG;
else if (DORM == "FT")
student_table[count].dorm_name= FT;
else if (DORM == "TU")
student_table[count].dorm_name= TU;
else if (DORM == "OP")
student_table[count].dorm_name= OP;
else if (DORM == "LN")
student_table[count].dorm_name= LN;
if (sport == "FOOTBALL")
student_table[count].first_sport = FOOTBALL;
else if (sport == "TENNIS")
student_table[count].first_sport = TENNIS;
else if(sport == "SOCCER")
student_table[count].first_sport = SOCCER;
else if (sport == "BASKETBALL")
student_table[count].first_sport = BASKETBALL;
}
return student_table[count];
}
float Compute_Total_Cost(int students[4][6], float money[4][6])
{
int count = 0;
StudentRecord studentinfo[25];
cout<<"Question 1: What is the total amount of money spent on each sport?"<<endl;
if (studentinfo[count++].num_of_sports == 2)
{
return studentinfo[count++].money_cost / 2;
}
else
{
return studentinfo[count++].money_cost;
}
cout<<"Football: $"<<endl;
cout<<"Basketball: $"<<endl;
cout<<"Tennis: $"<<endl;
cout<<"Soccer: $"<<endl;
}
This post has been edited by r.stiltskin: 26 April 2012 - 08:08 PM
Reason for edit:: Added code tags. Please learn to use them.

New Topic/Question
Reply



MultiQuote



|