1) You cannot define friend function at the place of declaration.
2) output operator should be non-member
3) it should take object to output by const reference
4) Herb Sutter and Scott Meyers advise you to refrain of friend functions and do not make output operator one
5) Good job so far Bunch of gotos are prime example of good job, sure.
I would suggest to remade your print function so it would really just print everything and not "assign and print" like now
//
/
#include <iostream>
#include <ostream>
#include <string>
#include <fstream>
#include <iterator>
#include <vector>
#include "database.h"
usingnamespace std;
int main()
{
string a;
int exit;
string letter;
int i = 1;
int n_l=0, n_r=0, n_e=0;
string line;
string firstname;
string lastname;
string email_;
string age_;
string gender_;
string optionn;
player dname;
//***the main question***//
thestart:
cout<< "1-Add a player" << endl; // program's question
cout<< "2-list players" << endl;
cout<< "3-play game" << endl;
cout<< "Q-Quit" << endl;
cin >> optionn;
option:
if (optionn== "1") // if the variable equation equals 1
{
goto addplayer ; // going to the option 1 add aplayer
}
elseif (optionn== "2") // going to the option 2 list player
{
goto listplayer ;
}
elseif (optionn== "3") // going to the option 3 playgame
{
goto playgame;
}
elseif (optionn== "q") // exit
{
goto end;
}
else
{
cout <<"Error:Invalid option."<<endl;
goto thestart; //going to the beginning and repeating all the previous steps
}
//*** option one***//
{
addplayer: // lable of goto command
player get_datam;
get_datam.get_data();
//***** player information ***//
//*** connect to the class that deal with player's information ***//
player data;
data.print();
database savem;
savem.save(dname);
goto thestart;
}
//**************************//
//****option two***//
//*** list all plyers who has been saved in the text file listplayers ***//
{listplayer:
database savem;
savem.save(dname);
goto thestart;
//**************************//
}
//*** option three ***//
//*** program game ***//
{
playgame:
cout<<i<< ".Enter a letter if Left or Right is greater/equal: L E R" << endl; // program's question
cout<< "Q:Quit" << endl; // i is the number of the question that start from 1 to the last number of a right input(l,e,or r)
cin >> letter;
if (letter=="l"||letter=="L"||letter=="r"||letter=="R"||letter=="e"||letter=="E") // if the variable letter equals l,L,r,R,e,or E do the next command
{
cout<< "Answer:"<<letter<< endl;
i = i + 1 ; // changing the number of the question to the next number
if (letter=="l"||letter=="L") //counting how many the letter l has been entered
{
n_l = n_l + 1;
}
elseif (letter=="r"||letter=="R") //counting how many the letter r has been entered
{
n_r = n_r+1 ;
}
else //counting how many the letter e has been entered
{
n_e = n_e + 1 ;
}
goto playgame ; // going to the playgame and repeating all the previous steps
}
elseif (letter=="q"||letter=="Q") // print the result and exit from the program
{
i = i-1 ; // uncounting the the question if the answer is q
cout <<i<<"questions: "<<n_l<<"L "<<n_e<<"E "<<n_r<<"R"<<endl;
cout << "Enter any thing to quit" << endl;
cout << "bye" << endl;
cin>> exit;
}
else
{
cout <<"Error:Invalid option. Select L-E-R"<<endl;
goto playgame; //going to the beginning and repeating all the previous steps
}
}
//***********************//
end:
return 0;
}
#include <iostream>
#include <ostream>
#include <string>
#include <vector>
#include <iterator>
#include <fstream>
#include "player.h"
usingnamespace std;
class database
{
private:
player firstname;
public:
void save (player);
};
void database::save(player dname)
{
cout<<"vector"<<endl;
firstname = dname;
vector<player>playersave;
playersave.push_back(firstname);
vector<player>::iterator it;
for( it = playersave.begin(); it != playersave.end(); ++it )
{
it -> print();
}
}
but i got an empty output
1-Add a player
2-list players
3-play game
Q-Quit
1
enter name
aaaaaaaaaaa
enter email
aaaaaaaaaaaaa
enter age
aaaaaaaaaaaaa
enter gender
aaaaaaaaaaaaaaa
name:
email:
age:
gender:
vector
name:
email:
age:
gender:
1-Add a player
2-list players
3-play game
Q-Quit
thank you so much that works fine, but the vector saves and shows just one player I need the the vector show all players when I choose add player more than one time
class database
{
private:
player firstname;
public:
void save (player);
};
void database::save(player dname)
{
firstname = dname;
vector<player>playersave;
playersave.push_back(firstname);
vector<player>::iterator it;
for( it = playersave.begin(); it != playersave.end(); ++it )
{
cout<<*it;
}
}
You were attempting to send objects of player to the output stream method. If so, you would need to befriend the overloaded stream method.
I cannot tell you how to implement your program nor will I do it for you, but the above will suffice for your original implementation. They're risks, however, as that anyone can print the data of your private data members using the "common" befriend function cout<<; thus countering the purpose of originally privatizing the data members.
@MiiNiPaa:
I do not find it necessary to scrap another persons work simply to give him a solution. If you choose to, that would be your choice.
If so, you would need to befriend the overloaded stream method.
Friendship is the strongest relationship, second only to the inheritance. It is best to avoid it. output operator usually implemented by member print function which either writes into passed stream or simply returns a string.
I do not find it necessary to scrap another persons work
Can you point where I "scrapped another person work"? All what I done is suggested to divide his function which does too many things into two according to one responcibility principle, also he fixed interface problem with it. Resulting code is cleaner, reusable and he haven't written any code aside from he already done.
Please, notice that he solved his output problem. Now he had another problem which will be eventually solved just by moving several lines around without any scrapping.
he haven't written any code aside from he already done
We tend to say things we do not mean, including myself, I apologize for "scrap another persons work", however my intentions were:
He had to modify his: main function, player class, and database class.
class database
{
private:
vector<player> playersave;//<-Vector of players
public:
void save (player);
};
void database::save(player dname)
{
cout<<"vector"<<endl;
playersave.push_back(dname);//<-Add player to your vector
vector<player>::iterator it;
for( it = playersave.begin(); it != playersave.end(); ++it )
{
it -> print();
}
}
1 2 3 4 5 6 7 8 9
string line;
string firstname;
string lastname;
string email_;
string age_;
string gender_;
string optionn;
player dname;
database savem;//<-Added a database here, called savem
Ensure to remove the construction of database everywhere else in your code; I think line 90.
//
/
#include <iostream>
#include <ostream>
#include <string>
#include <fstream>
#include <iterator>
#include <vector>
#include "database.h"
usingnamespace std;
int main()
{
string a;
int exit;
string letter;
int i = 1;
int n_l=0, n_r=0, n_e=0;
string line;
string firstname;
string lastname;
string email_;
string age_;
string gender_;
string optionn;
player get_datam;
database savem;
//***the main question***//
thestart:
cout<< "1-Add a player" << endl; // program's question
cout<< "2-list players" << endl;
cout<< "3-play game" << endl;
cout<< "Q-Quit" << endl;
cin >> optionn;
option:
if (optionn== "1") // if the variable equation equals 1
{
goto addplayer ; // going to the option 1 add aplayer
}
elseif (optionn== "2") // going to the option 2 list player
{
goto listplayer ;
}
elseif (optionn== "3") // going to the option 3 playgame
{
goto playgame;
}
elseif (optionn== "q") // exit
{
goto end;
}
else
{
cout <<"Error:Invalid option."<<endl;
goto thestart; //going to the beginning and repeating all the previous steps
}
//*** option one***//
{
addplayer: // lable of goto command
get_datam.get_data();
//***** player information ***//
//*** connect to the class that deal with player's information ***//
get_datam.print();
goto thestart;
}
//**************************//
//****option two***//
//*** list all plyers who has been saved in the text file listplayers ***//
{listplayer:
savem.save(get_datam);
goto thestart;
//**************************//
}
//*** option three ***//
//*** program game ***//
{
playgame:
cout<<i<< ".Enter a letter if Left or Right is greater/equal: L E R" << endl; // program's question
cout<< "Q:Quit" << endl;
cin >> letter;
if (letter=="l"||letter=="L"||letter=="r"||letter=="R"||letter=="e"||letter=="E")
{
cout<< "Answer:"<<letter<< endl;
i = i + 1 ; // changing the number of the question to the next number
if (letter=="l"||letter=="L") //counting how many the letter l has been entered
{
n_l = n_l + 1;
}
elseif (letter=="r"||letter=="R") //counting how many the letter r has been entered
{
n_r = n_r+1 ;
}
else //counting how many the letter e has been entered
{
n_e = n_e + 1 ;
}
goto playgame ; // going to the playgame and repeating all the previous steps
}
elseif (letter=="q"||letter=="Q") // print the result and exit from the program
{
i = i-1 ; // uncounting the the question if the answer is q
cout <<i<<"questions: "<<n_l<<"L "<<n_e<<"E "<<n_r<<"R"<<endl;
cout << "Enter any thing to quit" << endl;
cout << "bye" << endl;
cin>> exit;
}
else
{
cout <<"Error:Invalid option. Select L-E-R"<<endl;
goto playgame; //going to the beginning and repeating all the previous steps
}
}
//***********************//
end:
return 0;
}