Write your question here.
I am stuck to solve this problem im new in cplus plus and right now i am learning about classes and objects i got an assignment from my university and i am stuck .When ever i run the program its behave like infinite loop why it is so i am unable to figure it out please help me
#include<iostream>
#include<string>
usingnamespace std;
class player{
public:
int age;
string name;/* string to store name of the player*/
player(){ /* constructor for the class player*/
cout<<"Please enter your name"<<endl;
cin>>name;
cout<<"Please enter your age"<<endl;
/*cin.ignore();*/
cin>>age;
}
void setplayer_name(player p){
player p1;
p1.setplayer_name(p);
}
void setplayer_age(player p){
cout<<"Please set player's age"<<endl;
player p1;
int x;
p.age=p1.age;
x=p.age;
cout<<"age of player"<<"="<<x<<endl;
}
void display_age(){
player p3;
cout<<"Age of the player"<<p3.age<<endl;
}
void display_name(player x){
player p5;
x=p5;
cout<<"Now the name of the player is"<<"="<<x.name<<endl;
cout<<"now the value of p5 is"<<"="<<p5.name<<endl;
}
int main(){
int choice;
player myplayer1;
player myplayer;
myplayer.display_age();
myplayer.setplayer_age(myplayer1);
cout<<"The age after changing "<<endl;
myplayer.display_age();
Put the code you need help with here.
The first thing you should do is take all that user input out of the class constructor. User input should not be in a class constructor because user input is a big cause of failure, and constructors should not fail.
Next why are you creating "new" instances of your class in every member function? This is why your program appears to be in an endless loop, you keep calling the constructor every time you call one of the other function.
Those functions should be be operating on the class member variables not some temporary instance.
Lastly you need to fix your code so that it actually compiles.
58:37: error: expected '}' at end of input
In member function 'int player::main()':
58:1: error: 'Put' was not declared in this scope
51:6: warning: unused variable 'choice' [-Wunused-variable]
58:37: error: expected '}' at end of input
58:37: warning: no return statement in function returning non-void [-Wreturn-type]
At global scope:
58:37: error: expected unqualified-id at end of input