Can anyone help me with my C++ assignment on structs, arrays and bubblesort? I can't seem to get my code to work. The output should have the AVEPPG from highest to lowest (sorted by bubbesort). The output of my code is messed up. Please help me, thanks.
Here's the input.txt:
Mary 15 10.5
Joseph 32 6.2
Jack 72 8.1
Vince 83 4.2
Elizabeth 41 7.5
The output should be:
NAME UNIFORM# AVEPPG
Mary 15 10.50
Jack 72 8.10
Elizabeth 41 7.50
Joseph 32 6.20
Vince 83 4.20
My Code:
#include <iostream>
#include <iomanip>
#include <fstream>
#include <sstream>
using namespace std;
const int PLAYERS = 25;
struct playerType
{
string name;
int uniformNum;
double averagePoints;
};
void readPlayerData(playerType[], int&,
ifstream&);
void outputResults(playerType[], int);
void bubbleSort(playerType[], int);
int main()
{
ifstream in;
string filename;
playerType playerArray[PLAYERS];
int numPlayers = 0, i = 0, j = 0;
while(1)
{
cout << "Enter the file name:
";
cin >> filename;
in.open(filename.c_str());
if(!in.is_open())
{
cout << "Invalid filename!" << endl;
}
else
{
break;
}
}
readPlayerData(playerArray, numPlayers, in);
bubbleSort(playerArray, numPlayers);
outputResults(playerArray, numPlayers);
in.close();
cout << endl;
return 0;
}
void readPlayerData(playerType playerArray[], int&
numPlayers,ifstream& in)
{
string line;
while (getline(in, line))
{
stringstream ss(line);
string n;
int m;
ss >> n;
if (!n.empty() )
{
n[0] = toupper(n[0]);
for(size_t i = 1; i < n.length(); ++i)
n[i] = tolower(n[i]);
}
numPlayers++;
}
}
void outputResults(playerType playerArray[], int
numPlayers)
{
cout << left << setw(10) << fixed << "NAME"
<< "\t" << right << setw(7) << fixed
<< "UNIFORM#" << "\t" << right << setw(3)
<< fixed << "AVEPPG" << endl;
for (int i = 0; i < numPlayers; i++)
{
cout << playerArray[i].name << fixed;
cout << right << setw(5) <<
playerArray[i].uniformNum;
cout << setprecision(2) << fixed;
cout << right << setw(5) <<
playerArray[i].averagePoints << endl;
}
return;
}
void bubbleSort(playerType playerArray[], int numPlayers)
{
playerType temp;
for (int i = 0; i < numPlayers - 1; i++)
for (int j = 0; j < numPlayers - (i+1);
j++)
if (playerArray[j].averagePoints
> playerArray[j+1].averagePoints)
{
temp = playerArray[j];
playerArray[j] = playerArray[j+1];
playerArray[j+1] = temp;
}
}
We need at least 10 more requests to produce the answer.
0 / 10 have requested this problem solution
The more requests, the faster the answer.
Can anyone help me with my C++ assignment on structs, arrays and bubblesort? I can't seem...
Objectives: The main objective of this assignment is checking students’ ability to implement membership functions. After completing this assignment, students will be able to: implement member functions convert a member function into a standalone function convert a standalone function into a member function call member functions implement constructors use structs for function overloading Problem description: In this assignment, we will revisit Assignment #1. Mary has now created a small commercial library and has managed...
Objectives: The main objective of this assignment is checking students’ ability to implement membership functions. After completing this assignment, students will be able to: implement member functions convert a member function into a standalone function convert a standalone function into a member function call member functions implement constructors use structs for function overloading Problem description: In this assignment, we will revisit Assignment #1. Mary has now created a small commercial library and has managed...
I need help fixing my code: In C++ *************** 1) I want to sum the digits of an n*n matrix 2) find the average I have completed the rest ****Do not use C++ standard library. You must use pointers and pointer arithmetic to represent the matrix and to navigate through it. MY CODE: (I have indicated at which point I need help) #include <iostream> using namespace std; void swap(int *xp, int *yp) { int temp = *xp; *xp = *yp;...
How can I make this compatible with older C++ compilers that DO NOT make use of stoi and to_string? //Booking system #include <iostream> #include <iomanip> #include <string> using namespace std; string welcome(); void print_seats(string flight[]); void populate_seats(); bool validate_flight(string a); bool validate_seat(string a, int b); bool validate_book(string a); void print_ticket(string passenger[], int i); string flights [5][52]; string passengers [4][250]; int main(){ string seat, flight, book = "y"; int int_flight, p = 0, j = 0; int seat_number,...
In c++ please How do I get my printVector function to actually print the vector out? I was able to fill the vector with the text file of names, but I can't get it to print it out. Please help #include <iostream> #include <string> #include <fstream> #include <algorithm> #include <vector> using namespace std; void openifile(string filename, ifstream &ifile){ ifile.open(filename.c_str(), ios::in); if (!ifile){ cerr<<"Error opening input file " << filename << "... Exiting Program."<<endl; exit(1); } } void...
I need a detailed pseudocode for this code in C ++. Thank you #include <iostream> #include <string> #include <iomanip> using namespace std; struct Drink { string name; double cost; int noOfDrinks; }; void displayMenu(Drink drinks[], int n); int main() { const int size = 5; Drink drinks[size] = { {"Cola", 0.65, 2}, {"Root Beer", 0.70, 1}, {"Grape Soda", 0.75, 5}, {"Lemon-Lime", 0.85, 20}, {"Water", 0.90, 20} }; cout <<...
I want to change this code and need help. I want the code to not use parallel arrays, but instead use one array of struct containing the data elements, String for first name, String for last name,Array of integers for five (5) test scores, Character for grade. If you have any idea help would be great. #include #include #include #include using namespace std; const int NUMBER_OF_ROWS = 10; //number of students const int NUMBER_OF_COLUMNS = 5; //number of scores void...
C++
how can I fix these errors
this is my code
main.cpp
#include "SpecialArray.h"
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int measureElementsPerLine(ifstream& inFile) {
// Add your code here.
string line;
getline(inFile, line);
int sp = 0;
for (int i = 0; i < line.size(); i++)
{
if (line[i] == ' ')
sp++;
}
sp++;
return sp;
}
int measureLines(ifstream& inFile) {
// Add your code here.
string line;
int n = 0;
while (!inFile.eof())
{
getline(inFile,...
I need to add something to this C++ program.Additionally I want it to remove 10 words from the printing list (Ancient,Europe,Asia,America,North,South,West ,East,Arctica,Greenland) #include <iostream> #include <map> #include <string> #include <cctype> #include <fstream> #include <iomanip> using namespace std; void addWord(map<std::string,int> &words,string s); void readFile(string infile,map<std::string,int> &words); void display(map<std::string,int> words); int main() { // Declaring variables std::map<std::string,int> words; //defines an input stream for the data file ifstream dataIn; string infile; cout<<"Please enter a File Name :"; cin>>infile; readFile(infile,words);...
Please help fix my code C++, I get 2 errors when running. The code should be able to open this file: labdata.txt Pallet PAG PAG45982IB 737 4978 OAK Container AYF AYF23409AA 737 2209 LAS Container AAA AAA89023DL 737 5932 DFW Here is my code: #include <iostream> #include <string> #include <fstream> #include <vector> #include <cstdlib> #include <iomanip> using namespace std; const int MAXLOAD737 = 46000; const int MAXLOAD767 = 116000; class Cargo { protected: string uldtype; string abbreviation; string uldid; int...