Question

In: Computer Science

int main() {    footBallPlayerType bigGiants[MAX];    int numberOfPlayers;    int choice;    string name;   ...

int main()
{
   footBallPlayerType bigGiants[MAX];
   int numberOfPlayers;
   int choice;
   string name;
   int playerNum;
   int numOfTouchDowns;
   int numOfcatches;
   int numOfPassingYards;
   int numOfReceivingYards;
   int numOfRushingYards;
   int ret;
   int num = 0;
   ifstream inFile;
   ofstream outFile;
   ret = openFile(inFile);
   if (ret)
       getData(inFile, bigGiants, numberOfPlayers);
   else
       return 1;
   /// replace with the proper call to getData
   do
   {
       showMenu();
       cin >> choice;
       cout << endl;
       switch (choice)
       {
       case 1:
           cout << "Enter player's name: ";
           cin >> name;
           cout << endl;
           playerNum = searchData(bigGiants, num, name);
           printPlayerData(bigGiants, num, playerNum);
           break;
       case 2:
           printData(bigGiants, num);
           break;
       case 3:
           cout << "Enter player name: ";
           cin >> name;
           cout << endl;
           playerNum = searchData(bigGiants, num, name);
           cout << "Enter number of touch downs to be added: ";
           cin >> numOfTouchDowns;
           cout << endl;
           /// replace with call to update TouchDowns from group 1
           break;
       case 4:
           cout << "Enter player name: ";
           cin >> name;
           cout << endl;
           playerNum = searchData(bigGiants, num, name);
           cout << "Enter number of catches to be added: ";
           cin >> numOfcatches;
           cout << endl;
           break;
       case 5:
           cout << "Enter player name: ";
           cin >> name;
           cout << endl;
           playerNum = searchData(bigGiants, num, name);
           cout << "Enter number of passing yards to be added: ";
           cin >> numOfPassingYards;
           cout << endl;
           /// replace with call to updatePassingYards from group 3
           break;
       case 6:
           cout << "Enter player name: ";
           cin >> name;
           cout << endl;
           playerNum = searchData(bigGiants, num, name);
           cout << "Enter number of receiving yards to be added: ";
           cin >> numOfReceivingYards;
           cout << endl;
           /// replace with call to update Receiving Yards from group 4
           break;
       case 7:
           cout << "Enter player name: ";
           cin >> name;
           cout << endl;
           playerNum = searchData(bigGiants, num, name);
           cout << "Enter number of rushing yards to be added: ";
           cin >> numOfRushingYards;
           cout << endl;
           /// replace with call to update Rushing Yards from group 5
           break;
       case 99:
           break;
       default:
           cout << "Invalid selection." << endl;
       }
   } while (choice != 99);
   char response;
   cout << "Would you like to save data: (y,Y/n,N) ";
   cin >> response;
   cout << endl;
   if (response == 'y' || response == 'Y')
       saveData(outFile, bigGiants, num);
   inFile.close();
   outFile.close();
   return 0;
}
/// If file cannot be opened, a 1 is returned.
/// Parameter: ifstream
int openFile(ifstream& in) {
   string filename;
   cout << "Please enter football data file name: ";
   cin >> filename;
   in.open(filename.c_str());
   if (!in)
   {
       cout << filename << " input file does not exist. Program terminates!" << endl;
       return 1;
   }
   return 0;
}
/// Function requests file name from the user and opens file.
/// Post condition: If no error encountered, file is opened.
/// If file cannot be opened, a 0 is returned.
/// Parameter: ofstream
int openOutFile(ofstream& out) {
   string filename;
   cout << "Please enter the name of the output file: ";
   cin >> filename;
   out.open(filename.c_str());
   if (!out)
   {
       return 0;
   }
   return 1;
}
void showMenu()
{
   cout << "Select one of the following options:" << endl;
   cout << "1: To print a player's data" << endl;
   cout << "2: To print the entire data" << endl;
   cout << "3: To update a player's touch downs" << endl;
   cout << "4: To update a player's number of catches" << endl;
   cout << "5: To update a player's passing yards" << endl;
   cout << "6: To update a player's receiving yards" << endl;
   cout << "7: To update a player's rushing yards" << endl;
   cout << "99: To quit the program" << endl;
}
/// Reads data into the structure array
/// Precondition: ifstream is open, howMany initialized to 0
/// Postcondition: the structure array contains data from the input file
/// the howMany parameter contains the number of rows read
/// Parameters: ifstream, structure array, int file read counter
void getData(ifstream& inf, footBallPlayerType list[], int& howMany)
{
   howMany = 0;
   while (inf)
   {
       inf >> list[howMany].name >> list[howMany].position >> list[howMany].touchDowns >> list[howMany].catches >> list[howMany].passingYards >> list[howMany].receivingYards >> list[howMany].rushingYards;
       howMany++;
   }
}

/// Prints statistics for a selected player
/// Precondition: structure array contains data, length contains number of
void printPlayerData(footBallPlayerType list[], int length, int playerNum)
{
   if (0 <= playerNum && playerNum < length)
       cout << "Name: " << list[playerNum].name
       << " Position: " << list[playerNum].position << endl
       << "Touch Downs: " << list[playerNum].touchDowns
       << "; Number of Catches: " << list[playerNum].catches << endl
       << "Passing Yards: " << list[playerNum].passingYards
       << "; Receiving Yards: " << list[playerNum].receivingYards
       << "; Rushing Yards: " << list[playerNum].rushingYards << endl << endl;
   else
       cout << "Invalid player number." << endl << endl;
}
void printData(footBallPlayerType list[], int length)
{
   cout << left << setw(15) << "Name"
       << setw(14) << "Position"
       << setw(12) << "Touch Downs"
       << setw(9) << "Catches"
       << setw(12) << "Pass Yards"
       << setw(10) << "Rec Yards"
       << setw(12) << "Rush Yards" << endl;
   for (int i = 0; i < length; i++)
       cout << left << setw(15) << list[i].name
       << setw(14) << list[i].position
       << right << setw(6) << list[i].touchDowns
       << setw(9) << list[i].catches
       << setw(12) << list[i].passingYards
       << setw(10) << list[i].receivingYards
       << setw(12) << list[i].rushingYards << endl;
   cout << endl << endl;
}
/// Saves updated data to file name entered by user
/// Precondition: structure array contains data, length of array is filled
/// Postcondition: If requested file is opened, updated data is written to the
/// Parameters: ofstream, structure array, int length of array
void saveData(ofstream& outF, footBallPlayerType list[], int length)
{
   int ret;
   ret = openOutFile(outF);
   if (!ret) {
       cout << "Output file did not open...data will not be output to a file. " << endl;
       return;
   }
   for (int i = 0; i < length; i++)
       outF << list[i].name
       << " " << list[i].position
       << " " << list[i].touchDowns
       << " " << list[i].catches
       << " " << list[i].passingYards
       << " " << list[i].receivingYards
       << " " << list[i].rushingYards << endl;
}
/// Finds a football player by name
int searchData(footBallPlayerType list[], int length, string n)
{
   for (int i = 0; i < length; i++)
       if (list[i].name == n)
           return i;
   return -1;
}

I need help with my errors

The header file below

#include <iostream>#include <fstream>#include <string>#include <iomanip>using namespace std;struct footBallPlayerType{ string name; string position; int touchDowns; int catches; int passingYards; int receivingYards; int rushingYards;};const int MAX = 30;int openFile(ifstream&);int openOutFile(ofstream& out);void showMenu();void getData(ifstream& inf, footBallPlayerType list[], int& length);void printPlayerData(footBallPlayerType list[], int length, int playerNum);void printData(footBallPlayerType list[], int length);void saveData(ofstream&

Solutions

Expert Solution

  • There was no compile time error. However, the outputs produced were not the correct one.

i have added the following modificaiton to the code:

  • A change in getData() to check for end of file
  • A change in main() to make the program enter into the while. It was returning from before entering the loop
  • Statements to update information about players.

modified program:

  • Modification made are given in bold letters. It is for the reader's reference. It is better option to copy adn paste this in your editor.

#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
struct footBallPlayerType
{
string name;
string position;
int touchDowns;
int catches;
int passingYards;
int receivingYards;
int rushingYards;
};
const int MAX = 30;
int openFile(ifstream&);
int openOutFile(ofstream& out);
void showMenu();
void getData(ifstream& inf, footBallPlayerType list[], int& length);
void printPlayerData(footBallPlayerType list[], int length, int playerNum);
void printData(footBallPlayerType list[], int length);
void saveData(ofstream& outF, footBallPlayerType list[], int length);
int searchData(footBallPlayerType list[], int length, string n);
int main()
{
footBallPlayerType bigGiants[MAX];
int numberOfPlayers;
int choice;
string name;
int playerNum;
int numOfTouchDowns;
int numOfcatches;
int numOfPassingYards;
int numOfReceivingYards;
int numOfRushingYards;
int ret;
int num = 0;
ifstream inFile;
ofstream outFile;
ret = openFile(inFile);
if (ret==0)
getData(inFile, bigGiants, num);
else
return 1;
cout<<num<<"\n";
/// replace with the proper call to getData
do
{
showMenu();
cin >> choice;
cout << endl;
switch (choice)
{
case 1:
cout << "Enter player's name: ";
cin >> name;
cout << endl;
playerNum = searchData(bigGiants, num, name);
printPlayerData(bigGiants, num, playerNum);
break;
case 2:
printData(bigGiants, num);
break;
case 3:
cout << "Enter player name: ";
cin >> name;
cout << endl;
playerNum = searchData(bigGiants, num, name);
cout << "Enter number of touch downs to be added: ";
cin >> numOfTouchDowns;
bigGiants[playerNum].touchDowns += numOfTouchDowns;
cout << endl;
/// replace with call to update TouchDowns from group 1
break;
case 4:
cout << "Enter player name: ";
cin >> name;
cout << endl;
playerNum = searchData(bigGiants, num, name);
cout << "Enter number of catches to be added: ";
cin >> numOfcatches;
bigGiants[playerNum].catches += numOfcatches;
cout << endl;
break;
case 5:
cout << "Enter player name: ";
cin >> name;
cout << endl;
playerNum = searchData(bigGiants, num, name);
cout << "Enter number of passing yards to be added: ";
cin >> numOfPassingYards;
bigGiants[playerNum].passingYards += numOfPassingYards;
cout << endl;
/// replace with call to updatePassingYards from group 3
break;
case 6:
cout << "Enter player name: ";
cin >> name;
cout << endl;
playerNum = searchData(bigGiants, num, name);
cout << "Enter number of receiving yards to be added: ";
cin >> numOfReceivingYards;
bigGiants[playerNum].receivingYards = numOfReceivingYards;
cout << endl;
/// replace with call to update Receiving Yards from group 4
break;
case 7:
cout << "Enter player name: ";
cin >> name;
cout << endl;
playerNum = searchData(bigGiants, num, name);
cout << "Enter number of rushing yards to be added: ";
cin >> numOfRushingYards;
bigGiants[playerNum].rushingYards = numOfRushingYards;
cout << endl;
/// replace with call to update Rushing Yards from group 5
break;
case 99:
break;
default:
cout << "Invalid selection." << endl;
}
} while (choice != 99);
char response;
cout << "Would you like to save data: (y,Y/n,N) ";
cin >> response;
cout << endl;
if (response == 'y' || response == 'Y')
saveData(outFile, bigGiants, num);
inFile.close();
outFile.close();
return 0;
}
/// If file cannot be opened, a 1 is returned.
/// Parameter: ifstream
int openFile(ifstream& in) {
string filename;
cout << "Please enter football data file name: ";
cin >> filename;
in.open(filename.c_str());
if (!in)
{
cout << filename << " input file does not exist. Program terminates!" << endl;
return 1;
}
return 0;
}
/// Function requests file name from the user and opens file.
/// Post condition: If no error encountered, file is opened.
/// If file cannot be opened, a 0 is returned.
/// Parameter: ofstream
int openOutFile(ofstream& out) {
string filename;
cout << "Please enter the name of the output file: ";
cin >> filename;
out.open(filename.c_str());
if (!out)
{
return 0;
}
return 1;
}
void showMenu()
{
cout << "Select one of the following options:" << endl;
cout << "1: To print a player's data" << endl;
cout << "2: To print the entire data" << endl;
cout << "3: To update a player's touch downs" << endl;
cout << "4: To update a player's number of catches" << endl;
cout << "5: To update a player's passing yards" << endl;
cout << "6: To update a player's receiving yards" << endl;
cout << "7: To update a player's rushing yards" << endl;
cout << "99: To quit the program" << endl;
}
/// Reads data into the structure array
/// Precondition: ifstream is open, howMany initialized to 0
/// Postcondition: the structure array contains data from the input file
/// the howMany parameter contains the number of rows read
/// Parameters: ifstream, structure array, int file read counter
void getData(ifstream& inf, footBallPlayerType list[], int& howMany)
{
howMany = 0;
while (inf>> list[howMany].name )
{
inf >> list[howMany].position >> list[howMany].touchDowns >> list[howMany].catches >> list[howMany].passingYards >> list[howMany].receivingYards >> list[howMany].rushingYards;
howMany++;
}

}
/// Prints statistics for a selected player
/// Precondition: structure array contains data, length contains number of
void printPlayerData(footBallPlayerType list[], int length, int playerNum)
{
if (0 <= playerNum && playerNum < length)
cout << "Name: " << list[playerNum].name
<< " Position: " << list[playerNum].position << endl
<< "Touch Downs: " << list[playerNum].touchDowns
<< "; Number of Catches: " << list[playerNum].catches << endl
<< "Passing Yards: " << list[playerNum].passingYards
<< "; Receiving Yards: " << list[playerNum].receivingYards
<< "; Rushing Yards: " << list[playerNum].rushingYards << endl << endl;
else
cout << "Invalid player number." << endl << endl;
}
void printData(footBallPlayerType list[], int length)
{
cout << left << setw(15) << "Name"
<< setw(14) << "Position"
<< setw(12) << "Touch Downs"
<< setw(9) << "Catches"
<< setw(12) << "Pass Yards"
<< setw(10) << "Rec Yards"
<< setw(12) << "Rush Yards" << endl;
for (int i = 0; i < length; i++)
cout << left << setw(15) << list[i].name
<< setw(14) << list[i].position
<< right << setw(6) << list[i].touchDowns
<< setw(9) << list[i].catches
<< setw(12) << list[i].passingYards
<< setw(10) << list[i].receivingYards
<< setw(12) << list[i].rushingYards << endl;
cout << endl << endl;
}
/// Saves updated data to file name entered by user
/// Precondition: structure array contains data, length of array is filled
/// Postcondition: If requested file is opened, updated data is written to the
/// Parameters: ofstream, structure array, int length of array
void saveData(ofstream& outF, footBallPlayerType list[], int length)
{
int ret;
ret = openOutFile(outF);
if (!ret) {
cout << "Output file did not open...data will not be output to a file. " << endl;
return;
}
for (int i = 0; i < length; i++)
outF << list[i].name
<< " " << list[i].position
<< " " << list[i].touchDowns
<< " " << list[i].catches
<< " " << list[i].passingYards
<< " " << list[i].receivingYards
<< " " << list[i].rushingYards << endl;
}
/// Finds a football player by name
int searchData(footBallPlayerType list[], int length, string n)
{
for (int i = 0; i < length; i++)
if (list[i].name == n)
return i;
return -1;
}

output:

input file used:

output file:


Related Solutions

class Main { public static void main(String[] args) {        int[] array = {1,2,3,4,5};   ...
class Main { public static void main(String[] args) {        int[] array = {1,2,3,4,5};        //Complexity Analysis //Instructions: Print the time complexity of method Q1_3 with respect to n=Size of input array. For example, if the complexity of the //algorithm is Big O nlogn, add the following code where specified: System.out.println("O(nlogn)"); //TODO }    public static void Q1_3(int[] array){ int count = 0; for(int i = 0; i < array.length; i++){ for(int j = i; j < array.length;...
Consider the following class: class Person {         String name;         int age;        ...
Consider the following class: class Person {         String name;         int age;         Person(String name, int age){                this.name = name;                this.age = age;         } } Write a java program with two classes “Teacher” and “Student” that inherit the above class “Person”. Each class has three components: extra variable, constructor, and a method to print the student or the teacher info. The output may look like the following (Hint: you may need to use “super”...
public class Main { public static void main(String [] args) { int [] array1 = {5,...
public class Main { public static void main(String [] args) { int [] array1 = {5, 8, 34, 7, 2, 46, 53, 12, 24, 65}; int numElements = 10; System.out.println("Part 1"); // Part 1 // Enter the statement to print the numbers in index 5 and index 8 // put a space in between the two numbers and a new line at the end // Enter the statement to print the numbers 8 and 53 from the array above //...
---------------------------------------------------------------------------- public class Main { public static void main(String[] args) { int[] A = {11, 12,...
---------------------------------------------------------------------------- public class Main { public static void main(String[] args) { int[] A = {11, 12, -10, 13, 9, 12, 14, 15, -20, 0}; System.out.println("The maximum is "+Max(A)); System.out.println("The summation is "+Sum(A)); } static int Max(int[] A) { int max = A[0]; for (int i = 1; i < A.length; i++) { if (A[i] > max) { max = A[i]; } } return max; } static int Sum(int[] B){ int sum = 0; for(int i = 0; i --------------------------------------------------------------------------------------------------------------------------- Convert...
// 1. System.out.println("1."); int max = 5; for (int n = 1; n <= max; n++)...
// 1. System.out.println("1."); int max = 5; for (int n = 1; n <= max; n++) { System.out.println(n); } System.out.println(); // 2. System.out.println("2."); int total = 25; for (int number = 1; number <= (total / 2); number++) { total = total - number; System.out.println(total + " " + number); } System.out.println(); // 3. System.out.println("3."); for (int i = 1; i <= 2; i++) { for (int j = 1; j <= 3; j++) { for (int k = 1;...
int bintodec(string); // converts a binary number (represented as a STRING) to decimal int hextodec(string); //...
int bintodec(string); // converts a binary number (represented as a STRING) to decimal int hextodec(string); // converts a hexadecimal number (represented as a STRING) to decimal string dectobin(int); // converts a decimal number to binary (represted as a STRING) string dectohex(int); // converts a decimal number to hexadecimal (represted as a STRING) //the addbin and addhex functions work on UNsigned numbers string addbin(string, string); // adds two binary numbers together (represented as STRINGS) string addhex(string, string); // adds two hexadecimal...
import javax.swing.JOptionPane; public class RandomGuess { public static void main(String[] args) { int guess; int result;...
import javax.swing.JOptionPane; public class RandomGuess { public static void main(String[] args) { int guess; int result; String msg; final int LOW = 1; final int HIGH = 10; result = LOW + (int)(Math.random() * HIGH); guess = Integer.parseInt(JOptionPane.showInputDialog(null, "Try to guess my number between " + LOW + " and " + HIGH)); if(guess == result) msg = "\nRight!"; else if(guess < result) msg = "\nYour guess was too low"; else msg = "\nYour guess was too high"; JOptionPane.showMessageDialog(null,"The number...
Given: class Monster {     private:     string name;     int dangerLevel;     public:     Monster(sting, int);     virtual void hunt()
Given: class Monster {     private:     string name;     int dangerLevel;     public:     Monster(sting, int);     virtual void hunt() = 0;     virtual void fight(Monster&);     string getName() const; }; class GiantMonster : public Monster {     protected:         int height;          public:         GiantMonster(string, int, int);         virtual void trample(); }; class Dinosaur : public GiantMonster {     public:     Dinosaur(string, int, int);     void hunt();     void roar(); }; class Kraken : protected GiantMonster {     public:     Kraken(string, int, int);     virtual void hunt();     void sinkShip(); }; Indicate if the code snippets below are...
class Main { public static void main(String[] args) { int[] array = {1,2,3,4,5}; //Complexity Analysis //Instructions:...
class Main { public static void main(String[] args) { int[] array = {1,2,3,4,5}; //Complexity Analysis //Instructions: Print to n=Size of input array. For example, if the complexity of the //algorithm is Big O nlogn, add the following code where specified: System.out.println("O(nlogn)"); //code here } public static void (int[] array) { int count = 0; for(int i = 0; i < array.length; i++) { for(int j = i; j < array.length; j++) { for(int k = j; k < array.length; k++)...
#include <iostream> #include <string> #include <sstream> using namespace std; int main() { string userInput; getline(cin, userInput);...
#include <iostream> #include <string> #include <sstream> using namespace std; int main() { string userInput; getline(cin, userInput); // Declaring base int N = 30; if (userInput.length() > 10) { cout << 0 << endl; } else { int finalTotal = 0; //Iterates through userInput for(int i = 0; i < 10; i++){ char convertedInput = userInput[i]; // ASCII decimal value of each character int asciiDec = int(convertedInput); //Casts char value from input to int value stringstream chr; chr << convertedInput; int...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT