I have time to complete this, I have to add 2 void functions, 2 value returning functions, 1 structure and on array. I just need some ideas on where to start, what should I convert to functions, how should I instill stuctures and arrays to this particular program. I'm going to add another character, another menu. Any guidance on how to polish this code up would be greatly appreciated.

#include<iostream>
#include<ctime>
#include<fstream>

using namespace std;

int main()
{
  // Declare and initialize variables
  
  string fileUname = " ";  
  string uname = " ";
  int fileScore = 0;  
  int charaChoice = 0;
  int mainChoice = 0;
  int weapChoice = 0;
  int score = 0;
  int harry = 100;
  int yoda = 200;
  int gandolf = 300;
  int wand = 0;
  int lSaber = 0;
  int magic;
  int weapRand = 0, i=0;
  int magicRand = 0, y=0;

  // Introduction
  cout<< "****************************\n";
  cout<< "*********BE A HERO**********\n";
  cout<< "****************************\n\n\n\n";

  // Prompt user for name
    cout << "Enter your name: ";
    getline(cin,uname);

  // Display main menu around a Do while loop
 
    do{
  cout<<"Please choose from the following menu\n";
  cout<<"\t1) See rules\n";
  cout<<"\t2) Play game\n";
  cout<<"\t3) Exit\n";
  cout<<"Enter choice here "<<uname<<": ";
  cin>>mainChoice;
  
  switch(mainChoice)
    {
    case 1:
      cout<<"\tChoose 2 to start playing the game then pick a character and weapon\n\n\n";
      break;
    case 2:
      cout<<"\tGet ready for your adventure!!!\n";
       break;
    case 3:
      cout<<"\tThanks for playing!!!Bye\n";
return 0;
     break;
    default:
    cout<<"Invalid choice please choose from the menu.\n";
    }    
  }
  while(mainChoice!=2);
 

  // Display character menu

  cout<<"Please choose from one of the following heroes\n";
  cout<<"\t1) Harry Potter\n";
  cout<<"\t2) Yoda\n";
  cout<<"\t3) Gandolf\n";
  cout<<"Enter choice here "<<uname<<": ";
  cin>>charaChoice;

  switch(charaChoice){
    case 1:
    charaChoice = harry;
    cout<<"Expelliarmus!!!\n";
   break;
 case 2:
   charaChoice = yoda;
   cout<<"Do or do not! There is no try!\n"; 
   break;
 case 3:
   charaChoice = gandolf;
   cout<<"YOU SHALL NOT PASS!!!!!\n";
   break;
 default:
   cout<< "Invalid choice choose again: ";
  }
  //  cout << charaChoice<<endl;

  // Display weapon menu
  do{
cout<<"Please choose one from the following weapons\n";
  cout<<"\t1) Wand\n";
  cout<<"\t2) Light Saber\n";
  cout<<"\t3) Magic\n";
  cout<<"\t4) Finish\n";
  cout<<"Enter choice here "<<uname<<": ";
  cin>>weapChoice;

  // if statement : if the user picks Flamethrower or Bow and arrow
  // randomly generate number from 1-4 for practice session
  if (weapChoice == 1){
    weapChoice = wand;
 srand(time(NULL)); //seed random number generator

  //generate 4 numbers
 // for(i=1; i<=4; i++){
    
      weapRand = rand()%4 + 1; //generate a random number from 1 to 4
      // }
      if (weapRand == 1){
	cout<< "You hit Target A, 100 points"<<endl;
      	charaChoice = charaChoice + 100;
      }
      else if (weapRand == 2){
	cout<< "You hit Target B, 200 points"<<endl;
      	charaChoice = charaChoice + 200;
      }
      else if (weapRand == 3){
	cout<< "You hit Target C, 300 points"<<endl;
      	charaChoice = charaChoice + 300;
      }
      else
      	cout<<"You missed the targets"<<endl;
      //    cout<<weapRand<<endl;
    
    

  }
  if (weapChoice == 2){
    weapChoice = lSaber;
srand(time(NULL)); //seed random number generator

  //generate 4 numbers
  
    {
      weapRand = rand()%4 + 1; //generate a random number from 1 to 4
if (weapRand == 1){
	cout<< "You hit Target A, 100 points"<<endl;
      	charaChoice = charaChoice + 100;
      }
      else if (weapRand == 2){
	cout<< "You hit Target B, 200 points"<<endl;
      	charaChoice = charaChoice + 200;
      }
      else if (weapRand == 3){
	cout<< "You hit Target C, 300 points"<<endl;
      	charaChoice = charaChoice + 300;
      }
      else
      	cout<<"You missed the targets"<<endl;
//    cout<<weapRand<<endl;

    }



  }
  // if the user chooses magic user will randomly generate #  
  // between 1-2 : if 1 increment 200 points if 2 decrement 100 points
  if (weapChoice == 3){
srand(time(NULL)); //seed random number generator
magicRand = rand()%2 + 1;
  //generate 2 numbers
  
// }
  //  magicRand = rand()%2 + 1; //generate a random number from 1 to 2
if (magicRand == 1){
	cout<< "Your magic worked, 200 points!!"<<endl;
      	charaChoice = charaChoice + 200;
      }
      else if (magicRand == 2){
	cout<< "Your magic didn't work, minus 100 points"<<endl;
      	charaChoice = charaChoice + 200;
      }
           
      
// cout<<magicRand<<endl;
  }
      //weapChoice = magic;
  }
  while(weapChoice != 4);

  cout << "Your score is "<<charaChoice<<endl;
   

  // if score reaches below 1 or after 10 tries end game

  // (outside of program) create text file "highscore.txt" containing first name and value 0
  // read in highscore.txt 
  // if score is greater than value in file replace with users name and score else do not change the file
  ifstream infile;
  infile.open("highscore.txt", ios::in);
  infile>>fileUname>>fileScore;
  infile.close();
  ofstream outfile;
  if(charaChoice > fileScore)

  outfile.open("highscore.txt", ios::out);
  outfile<<uname<<" "<<charaChoice<<endl;
  outfile.close();


  
 
  return 0;
}

Recommended Answers

All 17 Replies

Whoever gave you those requirements was probably trying to help you out so your code wouldn't be solely lumped in main.

You can easily dismantle your code by--

adding the values declared/defined at the start of main to a Struct.

Making the multi-lines of code that are constantly c-outing values available in void functions (since they really shouldn't be returning anything - they're just there to display information).

The case-switch statements and other conditions can return a value needed for another function. If not that, then you can at least return a reference to a ifstream object near the end to save the data.

What he ^^ said.
But also this line: srand(time(NULL)); //seed random number generator should be called once and only once. I normally put it somewhere at the beginning of main()

Any guidance on how to polish this code up would be greatly appreciated.

The code needs proper, consistent indentation.

commented: It certainly does! +19

I'd write a lil' function for each 'weapon type' as you've currently got it sectioned...

then in main you'd have only function calls like:

int main( ){
String character = getCharacterSelection( );
String weapon = getWeaponSelection( );

/*target practice takes char and weapon and does the random dance to figure out points...*/
int score = targetPractice(character, weapon);

//good chance to use a void function. :)
saveGame( );

}

something like that... keep your main function as clutter free as possible. In the ideal case your code should be clear enough to be read like a story.

does indentation really matter in C++, I know it matters in other languages?

does indentation really matter in C++, I know it matters in other languages?

For functionality: no, not really
For readability: Yes! Most definitely! I hear a lot of people say: "I don't think it's worth the trouble/time" but when people like that post a problem with code, it's usually a mismatched bracket or something... This is one of the problems you'll never have again when your code is properly indented.

A good IDE will help you with the indention of your code. I personally use and like VS2008. If I want to indent the code automatically, I just press: ctrl-a ctrl-k ctrl-f :)

I'm lost / stuck !!

What am I doing wrong in the function calls? The program compiles but does not run, it's like main just gets run through and ends.

Where / how should I use structs, maybe for characterChoices and weapon choice, if so, do I substitue the switch statements for the structs, !@#$#@%$# I'm so confused. :'(

#include<iostream>
#include<ctime>
#include<fstream>

using namespace std;

string Introduction(string);
string userName(string& uname);
string getMainChoice (int& ,string);
string getCharacterSelection(int&);
string getWeaponSelection(int&,int);
int fileHighScore(int,string);


int main()
{
  // Declare and initialize variables
  
  string fileUname = " ";  
  string uname = " ";
  int fileScore = 0;  
  int charaChoice = 0;
  int mainChoice = 0;
  int weapChoice = 0;
  int score = 0;
  int harry = 100;
  int yoda = 200;
  int gandolf = 300;
  int wand = 0;
  int lSaber = 0;
  int magic;
  int weapRand = 0, i=0;
  int magicRand = 0, y=0;
  
  
void Introduction();  
string userName(string& uname);
string getMainChoice (int& mainChoice,string uname);
string getCharacterSelection(int& charaChoice,int harry, int yoda, int gandolf);
int fileHighScore(int charaChoice,string uname);




return 0;
}
void Introduction()
  {
  // Introduction
  cout<< "****************************\n";
  cout<< "*********BE A HERO**********\n";
  cout<< "****************************\n\n\n\n";
}
string userName(string& uname)
{
  // Prompt user for name
    cout << "Enter your name: ";
    getline(cin,uname);
    
    return uname;

}
string getMainChoice (int& mainChoice,string uname)
{
  // Display main menu around a Do while loop
 
    do{
  cout<<"Please choose from the following menu\n";
  cout<<"\t1) See rules\n";
  cout<<"\t2) Play game\n";
  cout<<"\t3) Exit\n";
  cout<<"Enter choice here "<<uname<<": ";
  cin>>mainChoice;
  
  switch(mainChoice)
    {
    case 1:
      cout<<"\tChoose 2 to start playing the game then pick a character and weapon\n\n\n";
      break;
    case 2:
      cout<<"\tGet ready for your adventure!!!\n";
       break;
    case 3:
      cout<<"\tThanks for playing!!!Bye\n";
return 0;
     break;
    default:
    cout<<"Invalid choice please choose from the menu.\n";
    }    
  }
  while(mainChoice!=2);
  
  return mainChoice,uname;
} 
string getCharacterSelection(int& charaChoice, string uname,int harry, int yoda, int gandolf)
{
  // Display character menu

  cout<<"Please choose from one of the following heroes\n";
  cout<<"\t1) Harry Potter\n";
  cout<<"\t2) Yoda\n";
  cout<<"\t3) Gandolf\n";
  cout<<"Enter choice here "<<uname<<": ";
  cin>>charaChoice;

  switch(charaChoice){
  case 1:
    charaChoice = harry;
    cout<<"Expelliarmus!!!\n";
   break;
 case 2:
   charaChoice = yoda;
   cout<<"Do or do not! There is no try!\n"; 
   break;
 case 3:
   charaChoice = gandolf;
   cout<<"YOU SHALL NOT PASS!!!!!\n";
   break;
 default:
   cout<< "Invalid choice choose again: ";
  }
  //  cout << charaChoice<<endl;
  return charaChoice,uname;
}
string getWeaponSelection(int& weapChoice, int charaChoice,int weapRand,string uname, int wand, int lSaber, int magic, int magicRand)
{
  // Display weapon menu
  do{
cout<<"Please choose one from the following weapons\n";
  cout<<"\t1) Wand\n";
  cout<<"\t2) Light Saber\n";
  cout<<"\t3) Magic\n";
  cout<<"\t4) Finish\n";
  cout<<"Enter choice here "<<uname<<": ";
  cin>>weapChoice;

  // if statement : if the user picks Flamethrower or Bow and arrow
  // randomly generate number from 1-4 for practice session
  if (weapChoice == 1){
    weapChoice = wand;
 srand(time(NULL)); //seed random number generator

  //generate 4 numbers
 // for(i=1; i<=4; i++){
    
      weapRand = rand()%4 + 1; //generate a random number from 1 to 4
      // }
      if (weapRand == 1){
	cout<< "You hit Target A, 100 points"<<endl;
      	charaChoice = charaChoice + 100;
      }
      else if (weapRand == 2){
	cout<< "You hit Target B, 200 points"<<endl;
      	charaChoice = charaChoice + 200;
      }
      else if (weapRand == 3){
	cout<< "You hit Target C, 300 points"<<endl;
      	charaChoice = charaChoice + 300;
      }
      else
      	cout<<"You missed the targets"<<endl;
      //    cout<<weapRand<<endl;
    
    

  }
  if (weapChoice == 2){
    weapChoice = lSaber;


  //generate 4 numbers
  
    {
      weapRand = rand()%4 + 1; //generate a random number from 1 to 4
if (weapRand == 1){
	cout<< "You hit Target A, 100 points"<<endl;
      	charaChoice = charaChoice + 100;
      }
      else if (weapRand == 2){
	cout<< "You hit Target B, 200 points"<<endl;
      	charaChoice = charaChoice + 200;
      }
      else if (weapRand == 3){
	cout<< "You hit Target C, 300 points"<<endl;
      	charaChoice = charaChoice + 300;
      }
      else
      	cout<<"You missed the targets"<<endl;
//    cout<<weapRand<<endl;

    }



  }
  // if the user chooses magic user will randomly generate #  
  // between 1-2 : if 1 increment 200 points if 2 decrement 100 points
  if (weapChoice == 3){

magicRand = rand()%2 + 1;
  //generate 2 numbers
  
// }
  //  magicRand = rand()%2 + 1; //generate a random number from 1 to 2
if (magicRand == 1){
	cout<< "Your magic worked, 200 points!!"<<endl;
      	charaChoice = charaChoice + 200;
      }
      else if (magicRand == 2){
	cout<< "Your magic didn't work, minus 100 points"<<endl;
      	charaChoice = charaChoice + 200;
      }
           
      
// cout<<magicRand<<endl;
  }
      //weapChoice = magic;
  }
  while(weapChoice != 4);

  cout << "Your score is "<<charaChoice<<endl;
}   
int fileHighScore(int charaChoice,string uname)
{
string fileUname = " ";
int fileScore = 0;
  // if score reaches below 1 or after 10 tries end game

  // (outside of program) create text file "highscore.txt" containing first name and value 0
  // read in highscore.txt 
  // if score is greater than value in file replace with users name and score else do not change the file
  ifstream infile;
  infile.open("highscore.txt", ios::in);
  infile>>fileUname>>fileScore;
  infile.close();
  ofstream outfile;
  if(charaChoice > fileScore)

  outfile.open("highscore.txt", ios::out);
  outfile<<uname<<" "<<charaChoice<<endl;
  outfile.close();
}

I used VC++ 2008 Express and had a couple errors. There are two functions that are supposed to return values but don't. You have to correct that problem before attempting to run the program.

error C4716: 'getWeaponSelection' : must return a value
error C4716: 'fileHighScore' : must return a value

And you need to include <string> at the top of the program.

Lines 36 - 40 aren't function calls. Looks like they are function declarations, so they should be above main where your other function declarations are (lines 7 - 12). Move them up there and if you get any duplicates for a function, delete one of them. A function CALL would be something like this:

Introduction ();
userName (uname);

Put these lines above return 0; Leave the words void , int , string , etc. off of the function calls. You don't want them. Those are already handled elsewhere. Just put the function name, then the variable names to pass to the functions inside the parentheses.

Elaborating more on VernonDozier's post, your function call should only contain the paramaters and not them declared. Change your statements that look like this

string getWeaponSelection(int& weapChoice, int charaChoice,int weapRand,string uname, int wand, int lSaber, int magic, int magicRand

To this

getWeaponSelection (weapChoice, charaChoice, weapRand,uname, wand, lSaber, imagic, magicRand)

Dont forget to declare them though.

I fixed the functions calls


ok what needs to be returned in filehighscore function.

#include<iostream>
#include<ctime>
#include<fstream>
#include<string>

using namespace std;

void Introduction();
string userName(string& );
string getMainChoice (int& ,string);
string getCharacterSelection(int&,string,int,int,int);
string getWeaponSelection(int&,int,int,string,int,int,int,int);
int fileHighScore(int,string);


int main()
{
  // Declare and initialize variables
  
  string fileUname = " ";  
  string uname = " ";
  int fileScore = 0;  
  int charaChoice = 0;
  int mainChoice = 0;
  int weapChoice = 0;
  int score = 0;
  int harry = 100;
  int yoda = 200;
  int gandolf = 300;
  int wand = 0;
  int lSaber = 0;
  int magic;
  int weapRand = 0, i=0;
  int magicRand = 0, y=0;
  
  
Introduction();  
userName(uname);
getMainChoice (mainChoice,uname);
//string getMainChoice ();
getCharacterSelection(charaChoice,uname,harry,yoda,gandolf);
//string getCharacterSelection();
fileHighScore(charaChoice,uname);




return 0;
}
void Introduction()
  {
  // Introduction
  cout<< "****************************\n";
  cout<< "*********BE A HERO**********\n";
  cout<< "****************************\n\n\n\n";
}
string userName(string& uname)
{
  // Prompt user for name
    cout << "Enter your name: ";
    getline(cin,uname);
    
    return uname;

}
string getMainChoice (int& mainChoice,string uname)
{
  // Display main menu around a Do while loop
 
    do{
  cout<<"Please choose from the following menu\n";
  cout<<"\t1) See rules\n";
  cout<<"\t2) Play game\n";
  cout<<"\t3) Exit\n";
  cout<<"Enter choice here "<<uname<<": ";
  cin>>mainChoice;
  
  switch(mainChoice)
    {
    case 1:
      cout<<"\tChoose 2 to start playing the game then pick a character and weapon\n\n\n";
      break;
    case 2:
      cout<<"\tGet ready for your adventure!!!\n";
       break;
    case 3:
      cout<<"\tThanks for playing!!!Bye\n";
return 0;
     break;
    default:
    cout<<"Invalid choice please choose from the menu.\n";
    }    
  }
  while(mainChoice!=2);
  
  return mainChoice,uname;
} 
string getCharacterSelection(int& charaChoice, string uname,int harry, int yoda, int gandolf)
{
  // Display character menu

  cout<<"Please choose from one of the following heroes\n";
  cout<<"\t1) Harry Potter\n";
  cout<<"\t2) Yoda\n";
  cout<<"\t3) Gandolf\n";
  cout<<"Enter choice here "<<uname<<": ";
  cin>>charaChoice;

  switch(charaChoice){
  case 1:
    charaChoice = harry;
    cout<<"Expelliarmus!!!\n";
   break;
 case 2:
   charaChoice = yoda;
   cout<<"Do or do not! There is no try!\n"; 
   break;
 case 3:
   charaChoice = gandolf;
   cout<<"YOU SHALL NOT PASS!!!!!\n";
   break;
 default:
   cout<< "Invalid choice choose again: ";
  }
  //  cout << charaChoice<<endl;
  return charaChoice,uname;
}
string getWeaponSelection(int& weapChoice, int charaChoice,int weapRand,string uname, int wand, int lSaber, int magic, int magicRand)
{
  // Display weapon menu
  do{
cout<<"Please choose one from the following weapons\n";
  cout<<"\t1) Wand\n";
  cout<<"\t2) Light Saber\n";
  cout<<"\t3) Magic\n";
  cout<<"\t4) Finish\n";
  cout<<"Enter choice here "<<uname<<": ";
  cin>>weapChoice;

  // if statement : if the user picks Flamethrower or Bow and arrow
  // randomly generate number from 1-4 for practice session
  if (weapChoice == 1){
    weapChoice = wand;
 srand(time(NULL)); //seed random number generator

  //generate 4 numbers
 // for(i=1; i<=4; i++){
    
      weapRand = rand()%4 + 1; //generate a random number from 1 to 4
      // }
      if (weapRand == 1){
	cout<< "You hit Target A, 100 points"<<endl;
      	charaChoice = charaChoice + 100;
      }
      else if (weapRand == 2){
	cout<< "You hit Target B, 200 points"<<endl;
      	charaChoice = charaChoice + 200;
      }
      else if (weapRand == 3){
	cout<< "You hit Target C, 300 points"<<endl;
      	charaChoice = charaChoice + 300;
      }
      else
      	cout<<"You missed the targets"<<endl;
      //    cout<<weapRand<<endl;
    
    

  }
  if (weapChoice == 2){
    weapChoice = lSaber;


  //generate 4 numbers
  
    {
      weapRand = rand()%4 + 1; //generate a random number from 1 to 4
if (weapRand == 1){
	cout<< "You hit Target A, 100 points"<<endl;
      	charaChoice = charaChoice + 100;
      }
      else if (weapRand == 2){
	cout<< "You hit Target B, 200 points"<<endl;
      	charaChoice = charaChoice + 200;
      }
      else if (weapRand == 3){
	cout<< "You hit Target C, 300 points"<<endl;
      	charaChoice = charaChoice + 300;
      }
      else
      	cout<<"You missed the targets"<<endl;
//    cout<<weapRand<<endl;

    }



  }
  // if the user chooses magic user will randomly generate #  
  // between 1-2 : if 1 increment 200 points if 2 decrement 100 points
  if (weapChoice == 3){

magicRand = rand()%2 + 1;
  //generate 2 numbers
  
// }
  //  magicRand = rand()%2 + 1; //generate a random number from 1 to 2
if (magicRand == 1){
	cout<< "Your magic worked, 200 points!!"<<endl;
      	charaChoice = charaChoice + 200;
      }
      else if (magicRand == 2){
	cout<< "Your magic didn't work, minus 100 points"<<endl;
      	charaChoice = charaChoice + 200;
      }
           
      
// cout<<magicRand<<endl;
  }
      //weapChoice = magic;
  }
  while(weapChoice != 4);

  cout << "Your score is "<<charaChoice<<endl;
  return weapChoice,charaChoice,uname;
}   
int fileHighScore(int charaChoice,string uname)
{
string fileUname = " ";
int fileScore = 0;
  // if score reaches below 1 or after 10 tries end game

  // (outside of program) create text file "highscore.txt" containing first name and value 0
  // read in highscore.txt 
  // if score is greater than value in file replace with users name and score else do not change the file
  ifstream infile;
  infile.open("highscore.txt", ios::in);
  infile>>fileUname>>fileScore;
  infile.close();
  ofstream outfile;
  if(charaChoice > fileScore)

  outfile.open("highscore.txt", ios::out);
  outfile<<uname<<" "<<charaChoice<<endl;
  outfile.close();
  return charaChoice,uname;
}

so what compile errors are you getting now? And why don't you just fix them ?

line 246: you can't return two things at the same time. The comma operator will cause only the last one to be returned, ignoring the first one.

I'm getting this error, tried a few different things and keep getting getting compile errors.

232 H:\project2_1.cpp new declaration `int fileHighScore(int&, std::string)'

17 H:\project2_1.cpp ambiguates old declaration `std::string fileHighScore(int&, std::string)'

int fileHighScore(int& charaChoice,string uname)
{
string fileUname = " ";
int fileScore = 0;
  // if score reaches below 1 or after 10 tries end game

  // (outside of program) create text file "highscore.txt" containing first name and value 0
  // read in highscore.txt 
  // if score is greater than value in file replace with users name and score else do not change the file
  ifstream infile;
  infile.open("highscore.txt", ios::in);
  infile>>fileUname>>fileScore;
  infile.close();
  ofstream outfile;
  if(charaChoice > fileScore)

  outfile.open("highscore.txt", ios::out);
  outfile<<uname<<" "<<charaChoice<<endl;
  outfile.close();
  return charaChoice;
}

Be careful with the function declarations and function definitions. The return type, function name and function parameters must match. In post #12 line 13 declares
fileHighScore like this:

int fileHighScore(int,string);

and line 227 starts the definition like this;

int fileHighScore(int charaChoice,string uname)

Those two lines match. However, in post #14 you have an alternate version of the definition of fileHighScore() that starts like this:

int fileHighScore(int& charaChoice,string uname)

Everything is the same as line 227 in post #12 except you are passing a reference to an int instead of a copy of an int. If you change line 227 post #12 to line 1 post #14 then be sure you change line 13 post #12 as well otherwise you could generate an error similar to what you posted.

If you pass the int variable charaChoice by reference, as in post #14, then there is no reason to return the value to the calling function as passing the variable by reference retains any changes to the value of the variable in the calling function made in the called function.

EDIT: Another consideration is that the return value of the two versions of these lines

232 H:\project2_1.cpp new declaration `int fileHighScore(int&, std::string)'

17 H:\project2_1.cpp ambiguates old declaration `std::string fileHighScore(int&, std::string)'

differ, which may also cause a problem.

The point being, something somewhere isn't matching up between function declaration and function definition.

I think I fixed everything now the program end abruptly after the second menu. I've tried a number of things, couldn't get it , I'm not giving up just looking for a slap in the face and a pointer in the right direction. How do you guys stay sane with the frustration of programming?

#include<iostream>
#include<ctime>
#include<fstream>
#include<string>

using namespace std;

void Introduction();
string userName(string& );
string getMainChoice (int& ,string);
int getCharacterSelection(int,string,int,int,int);
string getWeaponSelection(int&,int,int,string,int,int,int,int);
string fileHighScore(int,string);


int main()
{
  // Declare and initialize variables
  
  string fileUname = " ";  
  string uname = " ";
  int fileScore = 0;  
  int charaChoice = 0;
  int mainChoice = 0;
  int weapChoice = 0;
  int score = 0;
  int harry = 100;
  int yoda = 200;
  int gandolf = 300;
  int wand = 0;
  int lSaber = 0;
  int magic;
  int weapRand = 0, i=0;
  int magicRand = 0, y=0;
  
  
Introduction();  
userName(uname);
getMainChoice (mainChoice,uname);
//string getMainChoice ();
getCharacterSelection(charaChoice,uname,harry,yoda,gandolf);
//string getCharacterSelection();
fileHighScore(charaChoice,uname);




return 0;
}
void Introduction()
  {
  // Introduction
  cout<< "****************************\n";
  cout<< "*********BE A HERO**********\n";
  cout<< "****************************\n\n\n\n";
}
string userName(string& uname)
{
  // Prompt user for name
    cout << "Enter your name: ";
    getline(cin,uname);
    
    return uname;

}
string getMainChoice (int& mainChoice,string uname)
{
  // Display main menu around a Do while loop
 
    do{
  cout<<"Please choose from the following menu\n";
  cout<<"\t1) See rules\n";
  cout<<"\t2) Play game\n";
  cout<<"\t3) Exit\n";
  cout<<"Enter choice here "<<uname<<": ";
  cin>>mainChoice;
  
  switch(mainChoice)
    {
    case 1:
      cout<<"\tChoose 2 to start playing the game then pick a character and weapon\n\n\n";
      break;
    case 2:
      cout<<"\tGet ready for your adventure!!!\n";
       break;
    case 3:
      cout<<"\tThanks for playing!!!Bye\n";
return 0;
     break;
    default:
    cout<<"Invalid choice please choose from the menu.\n";
    }    
  }
  while(mainChoice!=2);
  
  return uname;
} 
int getCharacterSelection(int charaChoice, string uname,int harry, int yoda, int gandolf)
{
  // Display character menu

  cout<<"Please choose from one of the following heroes\n";
  cout<<"\t1) Harry Potter\n";
  cout<<"\t2) Yoda\n";
  cout<<"\t3) Gandolf\n";
  cout<<"Enter choice here "<<uname<<": ";
  cin>>charaChoice;

  switch(charaChoice){
  case 1:
    charaChoice = harry;
    cout<<"Expelliarmus!!!\n";
   break;
 case 2:
   charaChoice = yoda;
   cout<<"Do or do not! There is no try!\n"; 
   break;
 case 3:
   charaChoice = gandolf;
   cout<<"YOU SHALL NOT PASS!!!!!\n";
   break;
 default:
   cout<< "Invalid choice choose again: ";
  }
  //  cout << charaChoice<<endl;
  return charaChoice;
}
string getWeaponSelection(int& weapChoice, int charaChoice,int weapRand,string uname, int wand, int lSaber, int magic, int magicRand)
{
  // Display weapon menu
  do{
cout<<"Please choose one from the following weapons\n";
  cout<<"\t1) Wand\n";
  cout<<"\t2) Light Saber\n";
  cout<<"\t3) Magic\n";
  cout<<"\t4) Finish\n";
  cout<<"Enter choice here "<<uname<<": ";
  cin>>weapChoice;

  // if statement : if the user picks Flamethrower or Bow and arrow
  // randomly generate number from 1-4 for practice session
  if (weapChoice == 1){
    weapChoice = wand;
 srand(time(NULL)); //seed random number generator

  //generate 4 numbers
 // for(i=1; i<=4; i++){
    
      weapRand = rand()%4 + 1; //generate a random number from 1 to 4
      // }
      if (weapRand == 1){
	cout<< "You hit Target A, 100 points"<<endl;
      	charaChoice = charaChoice + 100;
      }
      else if (weapRand == 2){
	cout<< "You hit Target B, 200 points"<<endl;
      	charaChoice = charaChoice + 200;
      }
      else if (weapRand == 3){
	cout<< "You hit Target C, 300 points"<<endl;
      	charaChoice = charaChoice + 300;
      }
      else
      	cout<<"You missed the targets"<<endl;
      //    cout<<weapRand<<endl;
    
    

  }
  if (weapChoice == 2){
    weapChoice = lSaber;


  //generate 4 numbers
  
    {
      weapRand = rand()%4 + 1; //generate a random number from 1 to 4
if (weapRand == 1){
	cout<< "You hit Target A, 100 points"<<endl;
      	charaChoice = charaChoice + 100;
      }
      else if (weapRand == 2){
	cout<< "You hit Target B, 200 points"<<endl;
      	charaChoice = charaChoice + 200;
      }
      else if (weapRand == 3){
	cout<< "You hit Target C, 300 points"<<endl;
      	charaChoice = charaChoice + 300;
      }
      else
      	cout<<"You missed the targets"<<endl;
//    cout<<weapRand<<endl;

    }



  }
  // if the user chooses magic user will randomly generate #  
  // between 1-2 : if 1 increment 200 points if 2 decrement 100 points
  if (weapChoice == 3){

magicRand = rand()%2 + 1;
  //generate 2 numbers
  
// }
  //  magicRand = rand()%2 + 1; //generate a random number from 1 to 2
if (magicRand == 1){
	cout<< "Your magic worked, 200 points!!"<<endl;
      	charaChoice = charaChoice + 200;
      }
      else if (magicRand == 2){
	cout<< "Your magic didn't work, minus 100 points"<<endl;
      	charaChoice = charaChoice + 200;
      }
           
      
// cout<<magicRand<<endl;
  }
      //weapChoice = magic;
  }
  while(weapChoice != 4);

  cout << "Your score is "<<charaChoice<<endl;
  return weapChoice,charaChoice,uname;
}   
string fileHighScore(int charaChoice,string uname)
{
string fileUname = " ";
int fileScore = 0;
  // if score reaches below 1 or after 10 tries end game

  // (outside of program) create text file "highscore.txt" containing first name and value 0
  // read in highscore.txt 
  // if score is greater than value in file replace with users name and score else do not change the file
  ifstream infile;
  infile.open("highscore.txt", ios::in);
  infile>>fileUname>>fileScore;
  infile.close();
  ofstream outfile;
  if(charaChoice > fileScore)

  outfile.open("highscore.txt", ios::out);
  outfile<<uname<<" "<<charaChoice<<endl;
  outfile.close();
  return uname;
}

I think I fixed everything now the program end abruptly after the second menu. I've tried a number of things, couldn't get it , I'm not giving up just looking for a slap in the face and a pointer in the right direction. How do you guys stay sane with the frustration of programming?

Well, first thing I do is format the code so it can be followed. Many times that alone will point out the problems.

Next is write small sections at a time, compile, test. Then do another section.

Then add output statements to show you when the program hits key spots and display values that are important.

And the most important sanity measure -- take a break, watch a few minutes of TV, and drink a cola.

I think I fixed everything now the program end abruptly after the second menu.

There is more to fix, consider e.g. return weapChoice,charaChoice,uname; And really, you should format the code as already suggested, for example, the getWeaponSelection(...) function is pretty much unreadable as of now.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.