Public Group

# MASH game

This topic is 2715 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

sooooooo i remember this game being played alot back in middle school/elementary school(dont ask y i remembered it) but i decided to code it using c++ and all the things ive learned so far as a learning expierence. probly just a few questions, is it coded well, and is there a way i would be able to post this as well as other things ive coded in such a way that you could just click and play it? thanks
 #include<string> using std::string; class MashGame{ public: MashGame(); bool quit(); string getName(); string getCar(); string getHoneyMoon(); string getPet(); string getJob(); int getNumOfKids(); private: static const int maxNum = 4; string name[maxNum]; string car[maxNum]; string honeyM[maxNum]; string pet[maxNum]; string job[maxNum]; int kid[maxNum]; void setCategories(); void calculate(); }; 

 #include<iostream> using std::cout; using std::cin; using std::endl; #include<iomanip> using std::setw; #include<ctime> using std::time; #include<cstdlib> using std::rand; using std::srand; #include "MashGame.h" MashGame::MashGame(){ while(quit()){ setCategories(); calculate(); } } bool MashGame::quit(){ char choice; cout << "------------------------------" << endl << "WELCOME TO THE GAME OF M.A.S.H" << endl << "------------------------------" << endl << endl << "would you like to play?( type y for yes, n for no)" << endl; cin >> choice; if(choice == 'y'){ cout << "Lets Begin!!!!!" << endl << endl; return true; } else return false; } void MashGame::setCategories(){ for(int i = 0; i < 6; i++){ for(int j = 0; j < 4; j++){ switch(i){ case 0: if(j == 0) cout << "Enter 4 of your Crushes names:" << endl; cout << "Enter name " << j + 1 << ": "; cin >> name[j]; cout << endl; break; case 1: if(j == 0) cout << "Enter 4 of your dream cars:" << endl; cout << "Enter Dream Car " << j + 1 << ": "; cin >> car[j]; cout << endl; break; case 2: if(j == 0) cout << "Enter 4 of the places you would like to have your" " HoneyMoon at:" << endl; cout << "Enter HoneyMoon Location 1: "; cin >> honeyM[j]; cout << endl << endl; break; case 3: if(j == 0) cout << "Enter 4 Pets you would like to have:" << endl; cout << "Enter Pet " << j + 1 << ": "; cin >> pet[j]; cout << endl; break; case 4: if(j == 0) cout << "Enter 4 of your Dream Jobs:" << endl; cout << "Enter Dream Job " << j + 1 << ": "; cin >> job[j]; cout << endl; break; case 5: if(j == 0) cout << "Enter 4 Different Numbers: " << endl; cout << "Enter #" << j + 1<< ": "; cin >> kid[j]; cout << endl; break; } } } } void MashGame::calculate(){ int fate; cout << "Pick a number, any number, but this number is very important" " because it decides your fate!" << endl; cin >> fate; srand(time(0)); cout << endl << endl << "************YOUR FATE*************************" << endl; cout << "You will get married to " << getName() << endl << "Your honey moon will be at " << getHoneyMoon() << endl << "You will drive around in a " << getCar() << endl << "You will have a " << getPet() << " as a pet." << endl << "You have a job as " << getJob() << endl << "Oh, and I forgot " "about your kids, you have " << getNumOfKids() << " of them" << endl << endl; cout << "********HAS BEEN DECIDED**********************" << endl << endl; } string MashGame::getName(){ int choice = rand() % 3; return name[choice]; } string MashGame::getCar(){ int choice = rand() % 3; return car[choice]; } string MashGame::getHoneyMoon(){ int choice = rand() % 3; return honeyM[choice]; } string MashGame::getPet(){ int choice = rand() % 3; return pet[choice]; } string MashGame::getJob(){ int choice = rand() % 3; return job[choice]; } int MashGame::getNumOfKids(){ int choice = rand() % 3; return kid[choice]; } 

 #include "MashGame.h" int main(){ MashGame play; return 0; } 

##### Share on other sites
Here are the problems I noticed

* Using std::string in the header. You shouldn't use using declarations in headers because they'll pollute the namespace and cause difficult to find conflicts later on.
* Use of a conditional "if(j == 0)" in setCategories with no enclosing braces and no indentation. This makes the code hard to read and will probably lead to bugs later.
* "int choice = rand() % 3;" This will only pick from the first three entries. I'm not sure if this is desirable behavior. You probably want to change the 3 to maxNum.
* You should probably change the 4 to maxNum in the j loop so that you don't forget to change it if maxNum changes. Otherwise, you'll get array overflows which will best case scenario crash the game.

• 10
• 9
• 48
• 12
• 10
• ### Forum Statistics

• Total Topics
631383
• Total Posts
2999688
×