Text based "dating" game using OOB advice, Passing objects to contructors
Oct 24, 2016 at 2:06am UTC
So I'm making a text based game using Classes, inheritance , derived classes and the such for a class project. Was hoping someone could take a peek at my code so far and give me some pointers on how to streamline this and make more efficient. For example I plan on adding another class for the girl the player talks to and I want to take in all current players stats as parameters for one of the girl functions.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#include <iostream>
#include <string>
#include "Cbros.h"
#include "Engineer.h"
#include "Club.h"
using namespace std;
int main(){
Club Club1;
Club1.displayMenu();
Club1.processInput();
return 0;
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
#ifndef CBROS_H
#define CBROS_H
#include <iostream>
#include <string>
class Cbros
{
public :
Cbros();
//default constructor
Cbros(std::string , int , double );
std::string getName()const ;
int getAge ()const ;
int getStr()const ;
int getCharm()const ;
int getIntel()const ;
int getHeight ()const ;
void setName(std::string);
void setAge(int );
void setHeight(int );
void setIntel(int );
void setStr(int );
void setCharm(int );
class Wallet{
public :
double addMoney(double amount);//Adds Money to wallet
double removeMoney(double amount);//removes money from wallet
double countMoney();//displays money in wallet
private :
double money;
};
Wallet Mywallet;
protected :
std::string newName;
std::string newRace;
int newAge;
double newHeight;
int newStr;
int newInt;
int newCharm;
private :
};
#endif // CBROS_H
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
#include "Club.h"
#include "Cbros.h"
#include "Engineer.h"
#include "Artsy.h"
#include "Jock.h"
#include <iostream>
#include <string>
Cbros::Cbros()
{
}
Cbros::Cbros(std::string name ,int age , double height )
:newName(name),newAge(age),newHeight(height)
{
}
std::string Cbros::getName() const {
return newName;
}
int Cbros::getAge() const {
return newAge;
}
int Cbros::getHeight() const {
return newHeight;
}
int Cbros::getIntel() const {
return newInt;
}
int Cbros::getCharm() const {
return newCharm;
}
int Cbros::getStr() const {
return newStr;
}
void Cbros::setName(std::string name){
newName=name;
}
void Cbros::setAge(int age){
newAge=age;
}
void Cbros::setHeight(int height){
newHeight=height;
}
void Cbros::setCharm(int charm){
newCharm=charm;
}
void Cbros::setIntel(int intel) {
newInt=intel;
}
void Cbros::setStr(int str){
newStr=str;
}
double Cbros::Wallet::addMoney(double amount){if ( amount < 0 )
return 0;
money += amount;
return amount;
}
double Cbros::Wallet::removeMoney(double amount) {
if ( ( amount < 0 ) || ( money < amount ) )
return 0;
money -= amount;
return amount;
}
double Cbros::Wallet::countMoney() {
return money;
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
#ifndef CLUB_H
#define CLUB_H
#include "Cbros.h"
#include "Engineer.h"
#include <iostream>
#include <string>
class Club: public Cbros
{
public :
Club();
int displayMenu();
void processInput();
protected :
private :
int menuSelection;
};
#endif // CLUB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
#include "Club.h"
#include "Engineer.h"
#include "Cbros.h"
#include "Artsy.h"
#include "Jock.h"
#include <iostream>
#include <string>
Club::Club()
{}
int Club::displayMenu(){
std::cout<<"Welcome Meet a Girl Game!" <<std::endl;
std::cout<<"The goal is to use your inherent skills to earn a girl's phone number\n\n" ;
std::cout<<"Please enter the number of your choice:\n\n" ;
std::cout<<"1. Go the Club\n" ;
std::cout<<"2. Stay at Home\n" ;
std::cin>>menuSelection;
return menuSelection;
}
void Club::processInput(){
while (menuSelection!=1&&menuSelection!=2){
std::cout<<"invalid choice\n" <<displayMenu();
}
if (menuSelection==1){
std::cout<<"Are you a\n" ;
std::cout<<"1. Engineer Bro?\n" ;
std::cout<<"2. Artsy Bro?\n" ;
std::cout<<"3. Jock Bro?\n" ;
std::cin>>menuSelection;
switch (menuSelection){
case 1:{
std::cout<<"Welcome Engineer Bro\n" ;
std::cout<<"Please enter your name\n" ;
std::cin.ignore();
std::getline(std::cin, newName);
std::cout<<"Please enter your Height(in inches)\n" ;
std::cin>>newHeight;
std::cout<<"Please enter your Age\n" ;
std::cin>>newAge;
Engineer::Cbros Engineer(newName, newAge, newHeight);
Engineer.setCharm(3);
Engineer.setStr(3);
Engineer.setIntel(6);
Engineer.Mywallet.addMoney(100);
std::cout<<"Class: Engineer\n" ;
std::cout<<"Name: " <<Engineer.getName()<<std::endl;
std::cout<<"Height: " <<Engineer.getHeight()<<std::endl;
std::cout<<"Age " <<Engineer.getAge()<<std::endl;
std::cout<<"Initial Stats" <<std::endl;
std::cout<<"Charm: " <<Engineer.getCharm()<<std::endl;
std::cout<<"Strength: " <<Engineer.getStr()<<std::endl;
std::cout<<"Intelligence: " <<Engineer.getIntel()<<std::endl;
std::cout<<"Wallet Balance: " <<Engineer.Mywallet.countMoney<<"$" <<std::endl;
break ;
}
case 2:{
std::cout<<"Welcome Artsy Bro!\n" ;
std::cout<<"Please enter your name\n" ;
std::cin.ignore();
std::getline(std::cin, newName);
std::cout<<"Please enter your Height(in inches)\n" ;
std::cin>>newHeight;
std::cout<<"Please enter your Age\n" ;
std::cin>>newAge;
Artsy::Cbros Artsy(newName, newAge, newHeight);
std::cout<<"Artsy Bro's Name: " <<Artsy.getName()<<std::endl;
std::cout<<"Height: " <<Artsy.getHeight()<<std::endl;
std::cout<<"Age " <<Artsy.getAge()<<std::endl;
break ;
}
case 3: {
std::cout<<"Welcome Jock Bro!\n" ;
std::cout<<"Please enter your name\n" ;
std::cin.ignore();
std::getline(std::cin, newName);
std::cout<<"Please enter your Height(in inches)\n" ;
std::cin>>newHeight;
std::cout<<"Please enter your Age\n" ;
std::cin>>newAge;
Jock::Cbros Jock(newName, newAge, newHeight);
std::cout<<"Jock Bro's Name: " <<Jock.getName()<<std::endl;
std::cout<<"Height: " <<Jock.getHeight()<<std::endl;
std::cout<<"Age " <<Jock.getAge()<<std::endl;
break ;
}
default :
break ;
}
}
else if (menuSelection==2){
std::cout<<"Enjoy Netflix Bro" ;}
}
Last edited on Oct 24, 2016 at 4:56am UTC
Oct 24, 2016 at 2:36am UTC
Firstly :
std::cout<<"Please enter your name\n" ;
When you are about to tell the user to input something, remove the unnecessary newline character, and add a colon character (:) in its place. That will make your program look better.
Should be :
std::cout<<"Please enter your name : " ;
Secondly :
1 2
std::cin.ignore();
std::getline(std::cin, newName);
This std::cin.ignore() can accidentally stop your program in an unexpected way, if there is no newline character remaining in the stream. I recommend not always using std::cin.ignore() in this situation. std::cin.ignore(1000, '\n') may even make the problem worse.
The best way to use std::cin.ignore is this :
std::cin >> something; std::cin.ignore();
Last edited on Oct 24, 2016 at 2:37am UTC
Oct 24, 2016 at 4:55am UTC
Thank you much! More than anything I was hoping to get guidance on how I might pass an object thats already been created to the constructor of another class so that it can use those variables in its functions.
Topic archived. No new replies allowed.