Click here to Skip to main content
15,889,992 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi, is it possible that we put all functions in one function then call it in main function.
below code is mainly in main function but I want that it should be in a single function(not main) then we call it in main function.
example
function()
{
code that is written in main function and other functions
}


int main()
{
function();
}

What I have tried:

#include <iostream>
 #include <cstdlib>
 #include <ctime>
 #include <string>
 using namespace std;
 int tries=3;
 int letterFill (char, string, string&);
 int main ()
 {
 char letter;
 int wrong_guesses=0;
 string word;
 string words[] =
 {"punjab", "balochistan", "sindh", "KPK", "NWFP"};
//select random word from array and replace all of it's characters with *
 srand(time(NULL));
 int n=rand()% 5;
 word=words[n];
 string unknown(word.length(),'*');
 //game body that will show in output
 cout <<endl;
 cout << "Hangman Game";
 cout << "\n\nYou have " << tries << " tries to try and guess the word."<<endl;
 // Loop until the guesses are used up
 while (wrong_guesses < tries)
 {
 cout << unknown;
 cout << "\n\nGuess a letter: ";
 cin >> letter;
 // Fill secret word with letter if the guess is correct, otherwise increment the number of wrong guesses.
 if (letterFill(letter, word, unknown)==0)
 {
 cout << endl << "wrong guess" << endl;
 wrong_guesses++;
 }
 else
 {
 cout << endl << "letter found. Find other letters." << endl;
 }
 // Tell user how many guesses has left.
 cout << "You have " << tries - wrong_guesses;
 cout << " guesses left." << endl;
 // Check if user guessed the word.
 if (word==unknown)
 {
 cout << word << endl;
 cout << "Congrats! You won the Game";
 break;
 }
 }
 if(wrong_guesses == tries)
 {
 cout << "\nSorry, you lose...you've been hanged." << endl;
 cout << "The word was : " << word << endl;
 }
 cin.ignore();
 cin.get();
 return 0;
 }
 /* Take a one character guess and the secret word, and fill in the
 unfinished guessword. Returns number of characters matched.
 Also, returns zero if the character is already guessed. */
 int letterFill (char guess, string secretword, string &guessword)
 {
 int i;
 int matches=0;
 int len=secretword.length();
 for (i = 0; i< len; i++)
 {
 // Did we already match this letter in a previous guess?
 if (guess == guessword[i])
 return 0;
 // Is the guess in the secret word?
 if (guess == secretword[i])
 {
 guessword[i] = guess;
 matches++;
 }
 }
 return matches;
 }
Posted
Updated 4-Jan-22 5:40am
Comments
0x01AA 4-Jan-22 11:22am    
You do already similar thing with int letterFill (char guess, string secretword, string &guessword) which you are using from main

Same thing you can do with your code in main.
Greg Utas 4-Jan-22 12:29pm    
Doing this is a good idea. main should be kept fairly small. By moving pieces of it out into other functions, you break up the software into smaller, more understandable pieces that you might even be able to reuse. It also makes it easier to understand and evolve main itself.

1 solution

As mentioned in the comment:
You do already similar thing with int letterFill (char guess, string secretword, string &guessword) which you are using from main

Same thing you can do with your code in main and put it into a separate Function. I named it 'MyFunction' of course a better name makes sense.

C++
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
using namespace std;

// Global stuff
const int tries = 3;

// Forward declarations
int letterFill(char, string, string & );
void MyFunction();

//
// Here the new slim main function 
//
int main() {
  // Call here MyFunction
  MyFunction();
  return (0);
};

//
// The 'ousourced' code
//
void MyFunction() {
  char letter;
  int wrong_guesses = 0;
  string word;
  string words[] = {
    "punjab",
    "balochistan",
    "sindh",
    "KPK",
    "NWFP"
  };
  //select random word from array and replace all of it's characters with *
  srand(time(NULL));
  int n = rand() % 5;
  word = words[n];
  string unknown(word.length(), '*');
  //game body that will show in output
  cout << endl;
  cout << "Hangman Game";
  cout << "\n\nYou have " << tries << " tries to try and guess the word." << endl;
  // Loop until the guesses are used up
  while (wrong_guesses < tries) {
    cout << unknown;
    cout << "\n\nGuess a letter: ";
    cin >> letter;
    // Fill secret word with letter if the guess is correct, otherwise increment the number of wrong guesses.
    if (letterFill(letter, word, unknown) == 0) {
      cout << endl << "wrong guess" << endl;
      wrong_guesses++;
    } else {
      cout << endl << "letter found. Find other letters." << endl;
    }
    // Tell user how many guesses has left.
    cout << "You have " << tries - wrong_guesses;
    cout << " guesses left." << endl;
    // Check if user guessed the word.
    if (word == unknown) {
      cout << word << endl;
      cout << "Congrats! You won the Game";
      break;
    }
  }
  if (wrong_guesses == tries) {
    cout << "\nSorry, you lose...you've been hanged." << endl;
    cout << "The word was : " << word << endl;
  }
  cin.ignore();
  cin.get();
}

/* Take a one character guess and the secret word, and fill in the
unfinished guessword. Returns number of characters matched.
Also, returns zero if the character is already guessed. */
int letterFill(char guess, string secretword, string & guessword) {
  int i;
  int matches = 0;
  int len = secretword.length();
  for (i = 0; i < len; i++) {
    // Did we already match this letter in a previous guess?
    if (guess == guessword[i])
      return 0;
    // Is the guess in the secret word?
    if (guess == secretword[i]) {
      guessword[i] = guess;
      matches++;
    }
  }
  return matches;
}
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900