Error in terminal of VS Code:
/tmp/ccU4qzPn.o: In function main':
azul.cpp:(.text+0xa7e): undefined reference toMosaic::Mosaic()'
azul.cpp:(.text+0xa86): undefined reference to `Mosaic::printMosaic()'
collect2: error: ld returned 1 exit status*
azul.cpp
#include "Mosaic.h"
#include <iostream>
// int menuOption;
std::string playerOne;
std::string playerTwo;
void printMenu() {
std::cout << " " << std::endl;
std::cout << "*-*-*-*-*-*-*-*-*-*-*-*-*-*-*" << std::endl;
std::cout << "| |" << std::endl;
std::cout << "| Welcome to Azul |" << std::endl;
std::cout << "| |" << std::endl;
std::cout << "*-*-*-*-*-*-*-*-*-*-*-*-*-*-*" << std::endl;
std::cout << "| ----------- |" << std::endl;
std::cout << "| Main Menu |" << std::endl;
std::cout << "| ----------- |" << std::endl;
std::cout << "| 1. New Game |" << std::endl;
std::cout << "| 2. Load Game |" << std::endl;
std::cout << "| 3. Credits |" << std::endl;
std::cout << "| 4. Help |" << std::endl;
std::cout << "| 5. Quit |" << std::endl;
std::cout << "| |" << std::endl;
std::cout << "*-*-*-*-*-*-*-*-*-*-*-*-*-*-*" << std::endl;
std::cout << " " << std::endl;
}
int main(void) {
printMenu();
std::cout << "Enter Your Choice: " << std::endl;
char menuOption;
std::cin >> menuOption;
if (menuOption == '1') {
std::cout << "\n\nStarting a new game..." << std::endl;
std::cout << "\nEnter Name for Player One:" << std::endl;
std::cin >> playerOne;
std::cout << "\nEnter Name for Player Two:" << std::endl;
std::cin >> playerTwo;
std::cout << "\n\nLet's play!\n" << std::endl;
std::cout << "-----------" << std::endl;
std::cout << "Start Round" << std::endl;
std::cout << "-----------" << std::endl;
std::cout << "\nMosaic for " << playerOne << ":" << std::endl;
Mosaic* mosaic = new Mosaic();
mosaic->printMosaic();
}
return EXIT_SUCCESS;
};
Mosaic.cpp
#include "Mosaic.h"
#include <iostream>
Mosaic::Mosaic(){
}
Mosaic::~Mosaic(){
}
void Mosaic::printMosaic() {
for (int i = 1; i < 6; i++)
{
std::cout << i << ". ";
for (int j = 6; j > i; j--)
{
std::cout << " ";
}
for (int j = 0; j < i; j++)
{
std::cout << ".";
}
std::cout << "|| . . . . ." << std::endl;
}
}
Mosaic.h
class Mosaic
{
public:
Mosaic();
~Mosaic();
void printMosaic();
};
I think the problem is at the pointer
Mosaic* mosaic = new Mosaic();
Related
#define MYSQLPP_MYSQL_HEADERS_BURIED
#include <mysql++/mysql++.h>
#include <iostream>
#include "/home/sulli313/Project4/Film.h"
void Film::showList(){
std::cout << "\n-----------------------------------" << std::endl;
std::cout << " Query Application " << std::endl;
std::cout << "-----------------------------------" << std::endl;
std::cout << " 1 All letter name actors" << std::endl;
std::cout << " 2 First # PG-13 and Above" << std::endl;
std::cout << " 3 All active/inactive users by store" << std::endl;
std::cout << " 4 Actor Movie titles and ids" << std::endl;
std::cout << "-1 Exit" << std::endl;
std::cout << "-----------------------------------" << std::endl;
std::cout << ">> Enter your choice:" << std::endl;
}
void Film::showOne(){
mysqlpp::Connection myDB("cse278F2022", "localhost", "cse278F2022",
"raspberrySeltzer");
// Create a query
mysqlpp::Query query = myDB.query();
std::cout << "Please enter a Letter A-Z!" << std::endl;
std::string letter;
std::cin >> letter;
///////////////////////////////////Do this part/////////////////////////////////////////////
//if(letter)
query << "SELECT first_name, last_name "
<< "FROM actor "
<< "WHERE first_name "
<< "LIKE '" + letter + "%'";
// << "WHERE code = \"IST\"";
query.parse();
mysqlpp::StoreQueryResult result = query.store();
std::cout << "Here is your selection!\n" << std::endl;
std::cout << "--First/Last Names of Actors Whos First ";
std::cout << "Name Stars With " + letter + "--\n" << std::endl;
std::cout << std::left << std::setw(12) << "First Name" <<
std::setw(10) << "Last Name" << std::endl;
std::cout << "------------------------" << std::endl;
for (const auto & row : result) {
std::cout << std::left << std::setw(12) << row[0].c_str();
std::cout << std::setw(5) << row[1] << std::endl;
}
std::cout << "\n" << "to continue, press enter..." << std::endl;
showList();
}
void Film::showTwo(){
mysqlpp::Connection myDB("cse278F2022", "localhost", "cse278F2022",
"raspberrySeltzer");
//Selecting the second option's query
std::cout << "Please type a limit 1-30!" << std::endl;
std::string limit;
std::cin >> limit;
mysqlpp::Query query = myDB.query();
query << "SELECT title "
<< "FROM film "
<< "WHERE rating = 'PG-13' "
<< "OR rating = 'R' "
<< "OR rating = 'NC-17' "
<< "LIMIT " + limit + " ";
query.parse();
mysqlpp::StoreQueryResult result = query.store();
std::cout << "Here is your selection!\n" << std::endl;
std::cout << "--First "+ limit +" Titles PG-13 to NC-17--\n" << std::endl;
std::cout << std::left <<std::setw(20)<< "Title" << std::endl;
std::cout << "------------------------------" <<std::endl;
for (const auto & row : result) {
std::cout << std::left << std::setw(20) << row[0].c_str()<< std::endl;
}
std::cout << "\n" << "to continue, press enter..." << std::endl;
showList();
}
void Film::showThree(){
mysqlpp::Connection myDB("cse278F2022", "localhost", "cse278F2022",
"raspberrySeltzer");
//bind variable
std::cout << "Please type 1 for active and 0 for inactive!" << std::endl;
std::string active;
std::cin >> active;
if(active != "1" && active != "0"){
std::cout << "Wrong!" << std::endl;
std::cout << "Please type 1 for active and 0 for inactive!" << std::endl;
std::cin >> active;
}
//Selecting the second option's query
mysqlpp::Query query = myDB.query();
query << "SELECT Count(*) "
<< "FROM customer "
<< "WHERE active = '"+ active + "' "
<< "GROUP BY store_id";
query.parse();
mysqlpp::StoreQueryResult result = query.store();
std::cout << "Here is your selection!\n" << std::endl;
if(active == "1"){
std::cout << "--Count of All Active Users Grouped by Store Id--\n" << std::endl;
std::cout << std::left <<std::setw(20)<< "Active User Ids" << std::endl;
std::cout << "--------------------" <<std::endl;
} else {
std::cout << "--Count of All Inactive Users Grouped by Store Id--\n" << std::endl;
std::cout << std::left <<std::setw(20)<< "Inactive User Ids" << std::endl;
std::cout << "--------------------" <<std::endl;
}
for (const auto & row : result) {
std::cout << std::left << std::setw(20) << row[0].c_str()<< std::endl;
}
std::cout << "\n" << "to continue, press enter..." << std::endl;
showList();
}
void Film::showFour(){
mysqlpp::Connection myDB("cse278F2022", "localhost", "cse278F2022",
"raspberrySeltzer");
//bind variable
std::cout << "Please type an actor id that is 1-200!" << std::endl;
std::string act_id;
std::cin >> act_id;
//Selecting the second option's query
mysqlpp::Query query = myDB.query();
query << "SELECT film.title, film.film_id, film_actor.actor_id "
<< "FROM film, film_actor "
<< "WHERE film_actor.actor_id = "+ act_id +" "
<< "GROUP BY title "
<< "LIMIT 20";
query.parse();
mysqlpp::StoreQueryResult result = query.store();
std::cout << "Here is your selection!\n" << std::endl;
std::cout << "--First 20 Titles and IDs of Actor Id 25--\n" << std::endl;
std::cout << std::left <<std::setw(22)<< "Title" <<
std::setw(10)<< "Film_id" << std::setw(0)<<"Actor_id" << std::endl;
std::cout << "----------------------------------------" <<std::endl;
for (const auto & row : result) {
std::cout << std::left<<std::setw(22)<< row[0].c_str() << std::setw(10) << row[1] << std::setw(0) << row[2].c_str() << std::endl;
}
std::cout << "\n" << "to continue, press enter..." << std::endl;
showList();
}
#ifndef FILM_H
#define FILM_H
#include <iostream>
#include <string>
class Film {
public:
void showList();
void showOne();
void showTwo();
void showThree();
void showFour();
private:
};
#endif
// Copyright
// Purpose: Project 4
// Date 11/25/2022
// Author: Colton Sullivan
#define MYSQLPP_MYSQL_HEADERS_BURIED
#include <mysql++/mysql++.h>
#include <iostream>
#include "/home/sulli313/Project4/Film.h"
int main() {
int choice;
showList();
std::cin >> choice;
if (choice == -1) {
std::cout << "Bye!" << std::endl;
}
while (choice != -1) {
while ( choice > 4 || choice < -1 || choice == 0 ) {
std::cout << "The wrong choice!!!" << std::endl;
std::cout << "" << std::endl;
std::cout << "to continue, press enter...";
showList();
std::cin >> choice;
}
if ( choice == 1 ) {
showOne();
}
if ( choice == 2 ) {
showTwo;
}
if ( choice == 3 ) {
showThree;
}
if ( choice == 4 ) {
showFour;
}
std::cin >> choice;
if ( choice == -1 ) {
std::cout << "Bye!" << std::endl;
}
}
}
When trying to switch the original program to object oriented programming, I ran into the problem of the error "Invalid use of non-static member function" popping up for the showOne, showTwo, showThree, showFour and showList functions.
If there is a way to access the functions that are created in the Film.cpp/Film.h files and use them in the QueryApp.cpp file to run that as the main, please let me know.
I have tried switching it from Film::showOne, Film::showTwo...etc to showOne(); and Film::showOne(); but either the same Invalid use of non-static member function error will show or it will say that it has not be declared in this scope.
Your showSomething() functions are all member functions of the Film type. In particular, because they are not marked as static, they are non-static member functions. That means they operate on an instance of Film:
class C {
public:
void a(); // <- non-static member function
static void b(); // <- static member function
};
void foo() {
C::b(); // okay, doesn't need an object
C::a(); // not okay, needs an object
C object; // instance of C
object.a(); // okay
object.b(); // also okay
}
Typically you would give your object some state information or it raises the question why you have a non-static member function to begin with. If you don't need state, make them free functions (i.e., functions that are not member functions) or make them static.
In your case, I suppose it would be helpful to create a database connection in your Film's constructor so you can use the database member in all the functions that need a database connection to function.
Something like this:
class Film {
private:
mysqlpp::Connection myDB;
public:
Film() : myDB("x", "localhost", "y", "z") {}
// ...
};
I've recently started learning c++ and for the life of me, I can't seem to get the syntax of using ostream in a class and what arguments should I pass. Here's the code:
This is the class in question:
#include <iostream>
#include <string>
using namespace std;
class Pokemon{
friend ostream& operator<<(ostream&, Pokemon);
public:
string name, level, cp;
Pokemon(string x="Pikachu", string y="5", string z="1000"){
name = x;
level = y;
cp = z;
}
Pokemon name(){
return this->name;
}
Pokemon level(){
return this->level;
}
Pokemon cp(){
return this->cp;
}
Pokemon display_stats(){
cout << this-> name << "stats are:" << endl;
cout << " " << "Attack: 2716.05" << endl;
cout << " " << "Defence: 1629.63" << endl;
cout << " " << "HP: 1086.42" << endl;
}
};
template<typename TYPE> //i dont understand this and the things i've written down here are only based on samples i've seen
ostream& operator<<(ostream& os, Pokemon & c){
os << "The level of " << c.name << " is" << c.level << " with cp of " << c.cp;
}
As you could see, I already tried constructing the ostream thing but I don't really understand how it works. This is my main function:
int main()
{
Pokemon a, b, c, d;
a = Pokemon();
b = Pokemon("Weezing");
c = Pokemon("Nidoking", 100);
d = Pokemon("Mewtwo", 50, 5432.1);
cout << a << endl;
cout << b << endl;
cout << c << endl;
cout << d << endl;
cout << "Jessie: You are no match to me! Go " << b.name << "!" << endl;
cout << "Gary: Go lvl " << c.level << " " << c.name << "! Crush them" << endl;
cout << "Ash: " << a.name << " can do it even thouh he is only level " << a.level << endl;
cout << "Jessie: Hahaha! My " << b.name << " CP is " << b.cp << endl;
cout << "Gary: "<< c.name << " CP is " << c.cp << endl;
cout << "Ash: " << a.name << " CP is " << a.cp << endl;
cout << "Giovanni: Behold " << d.name << " is here." << endl;
d.display_stats();
return 0;
}
I'm getting errors of:
no instance of constructor "Pokemon::Pokemon" matches the argument list -- argument types are: (const char [9], int) //on line c = Pokemon("Nidoking", 100);
no instance of constructor "Pokemon::Pokemon" matches the argument list -- argument types are: (const char [7], int, double) //on line d = Pokemon("Mewtwo", 50, 5432.1);
All of your Pokemon class methods are returning the wrong type. And your main() is not calling any of the methods correctly at all.
Change your Pokemon class to look more like this:
#include <iostream>
#include <string>
using namespace std;
class Pokemon {
private:
string m_name;
int m_level;
double m_cp;
friend ostream& operator<<(ostream&, const Pokemon&);
public:
Pokemon(string x="Pikachu", int y=5, double z=1000) {
m_name = x;
m_level = y;
m_cp = z;
}
string name() const {
return m_name;
}
int level() const {
return m_level;
}
double cp() const {
return m_cp;
}
void display_stats() const {
cout << m_name << " stats are:" << endl;
cout << " " << "Attack: 2716.05" << endl;
cout << " " << "Defense: 1629.63" << endl;
cout << " " << "HP: 1086.42" << endl;
}
};
ostream& operator<<(ostream& os, const Pokemon &c) {
os << "The level of " << c.m_name << " is " << c.m_level << " with cp of " << c.m_cp;
return os;
}
And then change main() to look more like this:
int main()
{
Pokemon a;
Pokemon b("Weezing");
Pokemon c("Nidoking", 100);
Pokemon d("Mewtwo", 50, 5432.1);
cout << a << endl;
cout << b << endl;
cout << c << endl;
cout << d << endl;
cout << "Jessie: You are no match to me! Go " << b.name() << "!" << endl;
cout << "Gary: Go lvl " << c.level() << " " << c.name() << "! Crush them" << endl;
cout << "Ash: " << a.name() << " can do it even though he is only level " << a.level() << endl;
cout << "Jessie: Hahaha! My " << b.name() << " CP is " << b.cp() << endl;
cout << "Gary: " << c.name() << " CP is " << c.cp() << endl;
cout << "Ash: " << a.name() << " CP is " << a.cp() << endl;
cout << "Giovanni: Behold " << d.name() << " is here." << endl;
d.display_stats();
return 0;
}
Live Demo
I have just more or less finished my first C++ Project, it is a Hangman Game and so far everything works fine. The only Problem is that i need to have spaces between the underlines (_) that represent the hidden word. If anyone could help me on this i would really appreciate it.
// UNCOMMENT THE FOLLOWING LINE (REMOVE THE TWO SLASHES AT THE BEGINNING) TO RUN AUTOMATIC TESTS
#include "tests.h"
#include <iostream>
#include <string>
#include "hangman.h"
int main(){
using namespace std;
// display the hidden word
std::string word_to_guess = chooseWord();
int misses = 0;
std::string displayed_word = word_to_guess;
for(int i=0; i< displayed_word.length(); i++)
displayed_word[i] = '_';
int attempts = 6;
std::cout << "Attempts left:" << attempts << std::endl;
std::cout << "[ " << displayed_word << " ]" << std::endl;
//check for correct letter
while(1){
std::cout << "Your guess";
std::cout << ":";
char guess;
std::cin >> guess;
bool Correct = false;
for(int i=0; i< word_to_guess.length(); i++)
if (guess == word_to_guess[i]) {
displayed_word[i] = word_to_guess[i];
Correct = true;
}
if (!Correct)
attempts--;
if (!Correct)
std::cout << "Attempts left:" << attempts << std::endl;
if (!Correct)
std::cout << "[ " << displayed_word << " ]" << std::endl;
if (Correct)
std::cout << "Attempts left:" << attempts << std::endl;
if (Correct)
std::cout << "[ " << displayed_word << " ]" << std::endl;
//check for win or lose
if (attempts==0)
std::cout << "The word was: " << word_to_guess << std::endl << "You lost!";
if (attempts==0)
return 0;
if (!word_to_guess.find(displayed_word))
std::cout << "You won!";
if (!word_to_guess.find(displayed_word))
return 0;
}
}
First, you can simplify this
if (!Correct)
std::cout << "Attempts left:" << attempts << std::endl;
if (!Correct)
std::cout << "[ " << displayed_word << " ]" << std::endl;
if (Correct)
std::cout << "Attempts left:" << attempts << std::endl;
if (Correct)
std::cout << "[ " << displayed_word << " ]" << std::endl;
by this
std::cout << "Attempts left:" << attempts << std::endl;
std::cout << "[ " << displayed_word << " ]" << std::endl;
Now, about your question, I think a best solution is replacing
std::cout << "[ " << displayed_word << " ]" << std::endl;
by this
std::cout << "[";
for(int i = 0; i < displayed_word.length(); i++) {
if(i == 0 || displayed_word[i] == '_')
std::cout << " ";
std::cout << displayed_word[i];
if(i == displayed_word.length()-1 || (displayed_word[i] == '_' && displayed_word[i+1] != '_'))
std::cout << " ";
}
std::cout << "]" << std::endl;
Explaination:
We put spaces at the beginning and the end, and also around underscores, but we make sure to put only one space between two underscores.
I have this code that I have been working on for fun, it is as basic as it gets, becuase I am a beginner, and it works fine, but I can't seem to be able to figure out how to make it show the least and the most amount of pancakes ate. Thank you a lot in advance.
#include <iostream>
using namespace std;
int main(){
int pancakes[10];
int x,i;
cout << "Hello user!" << endl;
cout << endl;
cout << "Please enter how many pancakes did each of the 10 people eat:" << endl;
cout << endl;
for (i=0;i<10;i++ ){
cin >> x;
pancakes[i]=x;
}
cout << "1st person ate" << " " << pancakes[0] << " " << "pancakes" << endl;
cout << "2nd person ate" << " " << pancakes[1] << " " << "pancakes" << endl;
cout << "3rd person ate" << " " << pancakes[2] << " " << "pancakes" << endl;
cout << "4th person ate" << " " << pancakes[3] << " " << "pancakes" << endl;
cout << "5th person ate" << " " << pancakes[4] << " " << "pancakes" << endl;
cout << "6th person ate" << " " << pancakes[5] << " " << "pancakes" << endl;
cout << "7th person ate" << " " << pancakes[6] << " " << "pancakes" << endl;
cout << "8th person ate" << " " << pancakes[7] << " " << "pancakes" << endl;
cout << "9th person ate" << " " << pancakes[8] << " " << "pancakes" << endl;
cout << "10th person ate" << " " << pancakes[9] << " " << "pancakes" << endl;
return 0;
}
Since you are a beginner, I will put a simple solution using a loop.
int max = 0;
for(i = 0; i < 10; i++) {
if(pancakes[i] > max) max = pancakes[i];
}
cout << "Most amount of pancakes eaten by a single person: " << max << endl;
You can use min_element and max_element from the standard library to do this:
#include <algorithm>
cout << "The smallest number of pancakes was " << *min_element(pancakes, pancakes + 10) << endl;
cout << "The largest number of pancakes was " << *max_element(pancakes, pancakes + 10) << endl;
Firstly, instead of having around 10 cout's, you can use a loop to print them :
for (i=0;i<10;i++ ){
if(i==0)
cout <<i+1<< "st person ate" << " " << pancakes[i] << " " << "pancakes" << endl;
else if(i==1)
cout << i+1<<"nd person ate" << " " << pancakes[i] << " " << "pancakes" << endl;
else if(i==2)
cout << i+1<<"rd person ate" << " " << pancakes[i] << " " << "pancakes" << endl;
else
cout <<i+1<< "th person ate" << " " << pancakes[i] << " " << "pancakes" << endl;
}
Secondly, you can directly enter values into your array, no need for an intermediate variable x:
for (i=0;i<10;i++ ){
cin >> pancakes[i];
}
For your max and min problem, take two variables, say - max and min. Initialise them to any arbitrary smallest (say 0, if you are not dealing with negative numbers) and largest value (say, INT_MAX) respectively. Alternatively, you can initialise them to the first element of your array.
For finding max and min, you can traverse the entire array, while checking if the elements are greater or lesser than your max and min variables. If they are, then assign them to your variables:
for (i=0;i<10;i++ ){
if(pancakes[i]>max)
max = pancakes[i];
if(pancakes[i]<min)
min = pancakes[i];
}
You can add std::cout inside for loop like below,
for (int i = 0; i < 10; i++ )
{
std::cin >> pancakes[i];
std::cout << i+1 <<"st person ate" << " " << pancakes[i] << " " << "pancakes" << std::endl;
}
int maxpancakes = pancakes[0];
int minpancakes = pancakes[0];
for(int i = 0; i < pancakes.length(); i++ )
{
if( pancakes[i] < minpancakes )
minpancakes = pancakes[i];
if( pancakes[i] > maxpancakes )
maxpancakes = pancakes[i];
}
std::cout << "The smallest pan cake had is :" << minpancakes << std::endl;
std::cout << "The max pan cake had is :" << maxpancakes << std::endl;
I'm trying to sort alphabetically by team for my program, but not having any luck. If there is any hints or advice out there I would appreciate it. Below is the program I have minus the data input. Basically I just want to know how I would go about specifically sorting only by team in alphabetical order.
nflrecievers data[100];
ifstream fin;
fin.open("data.txt");
ofstream fout;
fout.open("validationReport.txt");
int i = 0;
while(!fin.eof())
{
fin >> data[i].fname >> data[i].lname >> data[i].team >> data[i].rec >> data[i].yards >> data[i].avgyrds_percatch >> data[i].tds >> data[i].longest_rec >> data[i].recpasttwenty_yrds >> data[i].yrds_pergame >> data[i].fumbles >> data[i].yac >> data[i].first_dwns ;
i = i + 1;
}
int a;
int b;
cout << " Select NFL Receivers Statistics. Input 1-4 " << endl;
cout << " 1) Receivers with 25+ Rec and 300+ Yards. " << endl;
cout << " 2) Recievers with 3+ TDs and 3+ Rec over 20 Yards. " << endl;
cout << " 3) Recievers with 100+ Yards per game and 15+ First Downs. " << endl;
cout << " 4) Veiw Total Recievers Statistics. " << endl;
cin >> a;
int c = 0;
if (a==1)
{
cout << " Receivers with 25+ Rec and 300+ Yards. " << endl;
while( c < i-1)
{
if(data[c].rec > 25 && data[c].yards > 300)
{
cout << endl;
cout << data[c].fname << " " << data[c].lname << " " << data[c].team << endl;
cout << " Rec: " << data[c].rec << " Yards: " << data[c].yards << endl;
cout << endl;
}
c++;
}
}
else if(a==2)
{
cout << " Recievers with 3+ TDs and 3+ Receptions past 20 Yards. " << endl;
while( c < i-1)
{
if(data[c].tds > 3 && data[c].recpasttwenty_yrds > 3)
{
cout << endl;
cout << data[c].fname << " " << data[c].lname << " " << data[c].team << endl;
cout << " TDs: " << data[c].tds << " Receptions past 20 Yards: " << data[c].recpasttwenty_yrds << endl;
cout << endl;
}
c++;
}
}
else if(a==3)
{
cout << " Recievers who average over 100+ yards per game and 15+ First Downs. " << endl;
while( c < i-1)
{
if(data[c].yrds_pergame > 100 && data[c].first_dwns > 15)
{
cout << endl;
cout << data[c].fname << " " << data[c].lname << " " << data[c].team << endl;
cout << " Average Yards per game: " << data[c].yrds_pergame << " First Downs: " << data[c].first_dwns << endl;
cout << endl;
}
c++;
}
}
else if(a==4)
{
cout << " Select a Reciever: " << endl;
while( c < i-1)
{
cout << c << ") " << data[c].fname << " " << data[c].lname << endl;
c++;
}
cout << " Total NFL Receivers Statistics. " << endl;
cin >> b;
cout << data[b].fname << " " << data[b].lname << endl;
cout << " Team: " << data[b].team << endl;
cout << " Receptions: " << data[b].rec << endl;
cout << " Yards: " << data[b].yards << endl;
cout << " Average Yards Per Catch: " << data[b].avgyrds_percatch << endl;
cout << " Longest Reception: " << data[b].longest_rec << endl;
cout << " Receptions over 20 Yards: " << data[b].recpasttwenty_yrds << endl;
cout << " Yards per game " << data[b].yrds_pergame << endl;
cout << " Fumbles: " << data[b].fumbles << endl;
cout << " Average Yards After Catch " << data[b].yac << endl;
cout << " Total First Downs: " << data[b].first_dwns << endl;
}
return 0;
}
std::sort(std::begin(data), std::end(data),
[](const nflrecievers& a, const nflrecievers& b) { return a.team < b.team; });
I would use std::sort
bool compare_teams(const nflrecievers &a, const nflrecievers &b) {
return a.team < b.team;
}
int i = 0;
while(!fin.eof())
{
fin >> data[i].fname >> data[i].lname >> data[i].team >> data[i].rec >> data[i].yards >> data[i].avgyrds_percatch >> data[i].tds >> data[i].longest_rec >> data[i].recpasttwenty_yrds >> data[i].yrds_pergame >> data[i].fumbles >> data[i].yac >> data[i].first_dwns ;
i = i + 1;
}
std::sort(data, data+i, compare_teams);
for (int i=0;i<c-1;i++)
for (int j=0;j<c-1;j++)
if (data[j].team>data[j+1].team)
{
nflrecievers temp = data[j];
data[j] = data[j+1];
data[j+1] = temp;
}
another solution:
int compare(const void *v1, const void *v2)
{
nflrecievers p1 = *(nflrecievers *)v1, p2 = *(nflrecievers *)v2;
return strcmp(p1.team,p2.team);
}
qsort(data,c,sizeof(data),compare);