c++ convert code help I feel really dumb and bad for the kind person who bothere
ID: 3574573 • Letter: C
Question
c++ convert code help
I feel really dumb and bad for the kind person who bothered to answer my question but their codes are with stdio.h and stdlib.h libraries. I don't understand since I'm a beginner and just started learning strictly in iostream. Can anyone please convert them to what I can understand? Again I feel really bad for having to ask what is essentially an answer into another answer. But I really need this done ASAP!
I'm supposed to be reading an input file into array of structs and also utilize dynamic memory allocation for the arrays. The input file I'm supposed to be reading from are in this format:
The first line is a single integer, representing the number of lines. The rest of format goes like this:
team name, win count, loss count, playoffs (1 or 0)
I don't know if these background info helps to convert the codes but there you go!
The libraries I am supposed to be using are:
iostream
iomanip
fstream
cctype
cstdlib
cstring
...Thank you so, SO MUCH for helping me, to the kind person who wrote this code initially, as well as whoever is going to answer this question.
Code:
//Include libraries
#include <stdio.h>
#include <stdlib.h>
//Define structure
struct teamInfo
{
//Declare variables
char teamName[50];
int wins;
int losses;
int playoffs;
int diff;
};
//Define printWinLoss()
void printWinLoss(struct teamInfo records[], int recordNum )
{
//Declare variable
int count=0;
//Loop until count
for (count=0; count<recordNum; count++)
{
//Display message
printf(" %s %i-%i", records[count].teamName, records[count].wins, records[count].losses);
}
//Display newline
printf(" ");
}
//Define showWins()
void showWins(struct teamInfo records[], int recordNum)
{
//Declare variable
int minWins = 0;
//Receive user input
printf(" Please enter number of least wins: ");
//store value
scanf("%i", &minWins);
//Declare variable
int count=0;
//Loop until count
for (count=0; count<recordNum; count++)
{
//If wins exceed minimum wins
if (records[count].wins >= minWins)
//Display result
printf(" %s ", records[count].teamName);
}
//Display newline
printf(" ");
}
//Define listPlayoffs()
void listPlayoffs(struct teamInfo records[], int recordNum)
{
//Declare variable
int count=0;
//Loop until count
for (count=0; count<recordNum; count++)
{
//If playoffs greater than 0
if (records[count].playoffs > 0)
//Display result
printf(" %s ", records[count].teamName);
}
//Display newline
printf(" ");
}
//Define printDiff()
void printDiff(struct teamInfo records[], int recordNum)
{
//Declare variable
int count=0;
//Loop until count
for (count=0; count<recordNum; count++)
{
//If difference is greater than 0
if (records[count].diff > 0)
//Display result
printf(" %s +%i ", records[count].teamName, records[count].diff);
//Otherwise
else
//Display result
printf(" %s %i ", records[count].teamName, records[count].diff);
}
//Display newline
printf(" ");
}
//Define mainMenu()
void mainMenu(struct teamInfo records[], int recordNum )
{
//Declare variable
int opt = 0;
//Define loop
do
{
//Display menu
printf(" Choose an option: ");
printf(" 1 - View team Win/Loss records ");
printf(" 2 - Show teams with at least x wins ");
printf(" 3 - List teams that made the playoffs ");
printf(" 4 - List teams and their win/loss differential ");
printf(" 5 - Sort teams by Win/Loss differential (extra credit) ");
printf(" 0 - Exit program ");
printf(" ");
//store value
scanf("%i", &opt);
//Define switch statement
switch (opt)
{
//Define case 1
case 1 : printWinLoss(records, recordNum);
//Break
break;
//Define case 2
case 2 : showWins(records, recordNum);
//Break
break;
//Define case 3
case 3 : listPlayoffs(records, recordNum);
//Break
break;
//Define case 4
case 4 : printDiff(records, recordNum);
//Break
break;
}
}
//Loop until opt is not 0
while (opt != 0);
}
//Define main method
int main()
{
//Define file pointer
FILE* fp;
//Declare characetr array
char filename[50];
//Receive file name
printf(" Please enter the file name: ");
//Store name
scanf("%s", filename);
//Display message
printf(" The file name you entered is %s ", filename);
//Set file pointer
fp = fopen(filename, "r");
//Loop until fp is Null
while (fp == NULL)
{
//Display message
printf(" Can't open %s, try again: ", filename);
//Store name
scanf("%s", filename);
//Open file
fp = fopen(filename, "r");
}
//Declare variable
int recordNum;
//If not end of file
if (!feof(fp))
//Store value
fscanf(fp, "%i ", &recordNum);
//Otherwise
else
{
//Display message
printf(" Error: file is empty! ");
//Exit
exit(0);
}
//Declare variables
struct teamInfo records[100];
int count = 0;
//Loop until end of file and count less than recordNum
while (!feof(fp) && count < recordNum)
{
//If value is not 4
if (fscanf(fp, "%[^,]%*c%i,%i,%i ", records[count].teamName, &records[count].wins, &records[count].losses, &records[count].playoffs) != 4 )
{
//Display message
printf(" Error: file is in wrong format.. ");
//Exit
exit(0);
}
//Set difference values
records[count].diff = records[count].wins - records[count].losses;
//Increment count
count++;
}
//If count is less than recordNum
if (count < recordNum)
{
//Display message
printf(" Error: too few records.. ");
//Exit
exit(0);
}
//Close file
fclose(fp);
//Call mainMenu()
mainMenu(records, recordNum );
}
Explanation / Answer
//Include libraries
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
//Define structure
struct teamInfo
{
//Declare variables
string teamName;
int wins;
int losses;
int playoffs;
int diff;
};
//Define printWinLoss()
void printWinLoss(teamInfo records[], int recordNum )
{
//Declare variable
int count=0;
//Loop until count
for (count=0; count<recordNum; count++)
{
//Display message
cout << endl << records[count].teamName << " " << records[count].wins << "-" << records[count].losses;
}
//Display newline
cout << endl;
}
//Define showWins()
void showWins(teamInfo records[], int recordNum)
{
//Declare variable
int minWins = 0;
//Receive user input
cout << " Please enter number of least wins: ";
//store value
cin >> minWins;
//Declare variable
int count=0;
//Loop until count
for (count=0; count<recordNum; count++)
{
//If wins exceed minimum wins
if (records[count].wins >= minWins)
//Display result
cout << " " << records[count].teamName;
}
//Display newline
cout << endl;
}
//Define listPlayoffs()
void listPlayoffs(teamInfo records[], int recordNum)
{
//Declare variable
int count=0;
//Loop until count
for (count=0; count<recordNum; count++)
{
//If playoffs greater than 0
if (records[count].playoffs > 0)
//Display result
cout << " " << records[count].teamName;
}
//Display newline
cout << endl;
}
//Define printDiff()
void printDiff(teamInfo records[], int recordNum)
{
//Declare variable
int count=0;
//Loop until count
for (count=0; count<recordNum; count++)
{
//If difference is greater than 0
if (records[count].diff > 0)
//Display result
cout << " " << records[count].teamName << " +" << records[count].diff;
//Otherwise
else
//Display result
cout << " " << records[count].teamName << " +" << records[count].diff;
}
//Display newline
cout << endl;
}
//Define mainMenu()
void mainMenu(teamInfo records[], int recordNum )
{
//Declare variable
int opt = 0;
//Define loop
do
{
//Display menu
cout << " Choose an option: ";
cout << " 1 - View team Win/Loss records ";
cout << " 2 - Show teams with at least x wins ";
cout << " 3 - List teams that made the playoffs ";
cout << " 4 - List teams and their win/loss differential ";
cout << " 5 - Sort teams by Win/Loss differential (extra credit) ";
cout << " 0 - Exit program ";
cout << " ";
//store value
cin >> opt;
//Define switch statement
switch (opt)
{
//Define case 1
case 1 : printWinLoss(records, recordNum);
//Break
break;
//Define case 2
case 2 : showWins(records, recordNum);
//Break
break;
//Define case 3
case 3 : listPlayoffs(records, recordNum);
//Break
break;
//Define case 4
case 4 : printDiff(records, recordNum);
//Break
break;
}
}
//Loop until opt is not 0
while (opt != 0);
}
//Define main method
int main()
{
//Declare characetr array
char filename[50];
//Receive file name
cout << " Please enter the file name: ";
//Store name
cin >> filename;
//Display message
cout << " The file name you entered is " << filename << " ";
//Set file pointer
ifstream in(filename);
//Loop until fp is Null
while (!in.is_open())
{
//Display message
cout << " Can't open " << filename << ", try again: ";
//Store name
cin >> filename;
//Open file
in.open(filename);
}
//Declare variable
int recordNum;
in >> recordNum;
//Declare variables
teamInfo records[100];
int count = 0;
//Loop until end of file and count less than recordNum
while (!in.eof() && count < recordNum)
{
//If value is not 4
if (!(in >> records[count].teamName, records[count].wins, records[count].losses, records[count].playoffs))
{
//Display message
cout << " Error: file is in wrong format.. ";
//Exit
exit(0);
}
//Set difference values
records[count].diff = records[count].wins - records[count].losses;
//Increment count
count++;
}
//If count is less than recordNum
if (count < recordNum)
{
//Display message
cout << " Error: too few records.. ";
//Exit
exit(0);
}
//Close file
in.close();
//Call mainMenu()
mainMenu(records, recordNum );
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.