-1

I've been following lessons in Codecademy to learn C++. I've come to the last project where I learned to make a program that works like hangman. I wanted to add a list of words from which the program chooses a new word each time the program is run. (line 5)

#include "ufo.hpp"

int main() {

  srand (time(NULL));

  std::cout << "=============\n";
  std::cout << "UFO: The Game\n";
  std::cout << "=============\n";
  std::cout << "Instructions: save your friend from alien\nabduction by guessing the letters in the\ncodeword.\n";

  for (int i = 0; i < codeword.size(); i++) {
      answer.append("_");
  }

  while (answer != codeword && misses <= 6) {
      take_turn();
  }

  if (answer == codeword) {
      std::cout << "Hooray! You saved the person and earned a medal of honor!\n";
  } else {
      std::cout << "Oh no! The UFO just flew away with another person!\n";
      std::cout << "The codeword was: " << codeword << "\n";
  }

  return 0;
}

In the code above, I used srand(time(NULL)) in the main function. The rand_number variable can be found in the following header: (line 11)

#include <iostream>
#include <vector>
#include <stdlib.h>
#include <time.h>

void display(int misses);

// Declare variables

std::vector<std::string> codewords = {"camera", "computer", "table", "desk", "bed", "chair", "drawer", "button", "elephant", "mouse", "printer", "soap", "toothbrush", "pillow", "curtain", "wall", "roof", "poster", "grasland", "woodpecker", "jacket", "trousers", "waterfall", "softdrink", "laundry"};
int rand_number = rand() % codewords.size();

std::string codeword = codewords[rand_number];
std::string answer;
int misses = 0;
std::vector<char> incorrect;
bool guess = false;
char letter;

// Define take_turn()

void take_turn() {

  display(misses);

  std::cout << "Please enter your guess: ";
  std::cin >> letter;

  for (int i = 0; i < codeword.size(); i++) {
    if (letter == codeword[i]) {
      answer[i] = letter;
      guess = true;
    }
  }

  if (guess) {
    std::cout << "Correct!\n";
  } else {
    std::cout << "Incorrect! The tractor beam pulls the person in further.\n";
    incorrect.push_back(letter);
    misses++;
  }

  std::cout << "==========================\n";

  std::cout << "Incorrect Guesses:\n";
  for (int i = 0; i < incorrect.size(); i++) {
    std::cout << incorrect[i] << " ";
  }

  std::cout << "\nCodeword:\n";

  for (int i = 0; i < answer.size(); i++) {
    std::cout << answer[i] << " ";
  }

  std::cout << "\n";

  guess = false;
}



void display(int misses) {

if (misses == 0 || misses == 1) {

std::cout << "                 .                            \n";
std::cout << "                 |                            \n";
std::cout << "              .-\"^\"-.                       \n";
std::cout << "             /_....._\\                       \n";
std::cout << "         .-\"`         `\"-.                  \n";
std::cout << "        (  ooo  ooo  ooo  )                   \n";
std::cout << "         '-.,_________,.-'    ,-----------.   \n";
std::cout << "              /     \\        (  Send help! ) \n";
std::cout << "             /   0   \\      / `-----------'  \n";
std::cout << "            /  --|--  \\    /                 \n";
std::cout << "           /     |     \\                     \n";
std::cout << "          /     / \\     \\                   \n";
std::cout << "         /               \\                   \n";

} else if (misses == 2) {

std::cout << "                 .                            \n";
std::cout << "                 |                            \n";
std::cout << "              .-\"^\"-.                       \n";
std::cout << "             /_....._\\                       \n";
std::cout << "         .-\"`         `\"-.                  \n";
std::cout << "        (  ooo  ooo  ooo  )                   \n";
std::cout << "         '-.,_________,.-'    ,-----------.   \n";
std::cout << "              /  0  \\        (  Send help! ) \n";
std::cout << "             / --|-- \\      / `-----------'  \n";
std::cout << "            /    |    \\    /                 \n";
std::cout << "           /    / \\    \\                    \n";
std::cout << "          /             \\                    \n";
std::cout << "         /               \\                   \n";

} else if (misses == 3) {

std::cout << "                 .                            \n";
std::cout << "                 |                            \n";
std::cout << "              .-\"^\"-.                       \n";
std::cout << "             /_....._\\                       \n";
std::cout << "         .-\"`         `\"-.                  \n";
std::cout << "        (  ooo  ooo  ooo  )                   \n";
std::cout << "         '-.,_________,.-'    ,-----------.   \n";
std::cout << "              /--|--\\        (  Send help! ) \n";
std::cout << "             /   |   \\      / `-----------'  \n";
std::cout << "            /   / \\   \\    /                \n";
std::cout << "           /           \\                     \n";
std::cout << "          /             \\                    \n";
std::cout << "         /               \\                   \n";

} else if (misses == 4) {

std::cout << "                 .                            \n";
std::cout << "                 |                            \n";
std::cout << "              .-\"^\"-.                       \n";
std::cout << "             /_....._\\                       \n";
std::cout << "         .-\"`         `\"-.                  \n";
std::cout << "        (  ooo  ooo  ooo  )                   \n";
std::cout << "         '-.,_________,.-'    ,-----------.   \n";
std::cout << "              /  |  \\        (  Send help! ) \n";
std::cout << "             /  / \\  \\      / `-----------' \n";
std::cout << "            /         \\    /                 \n";
std::cout << "           /           \\                     \n";
std::cout << "          /             \\                    \n";
std::cout << "         /               \\                   \n";

} else if (misses == 5) {

std::cout << "                 .                            \n";
std::cout << "                 |                            \n";
std::cout << "              .-\"^\"-.                       \n";
std::cout << "             /_....._\\                       \n";
std::cout << "         .-\"`         `\"-.                  \n";
std::cout << "        (  ooo  ooo  ooo  )                   \n";
std::cout << "         '-.,_________,.-'    ,-----------.   \n";
std::cout << "              / / \\ \\        (  Send help! )\n";
std::cout << "             /       \\      / `-----------'  \n";
std::cout << "            /         \\    /                 \n";
std::cout << "           /           \\                     \n";
std::cout << "          /             \\                    \n";
std::cout << "         /               \\                   \n";

} else if (misses == 6) {

std::cout << "                 .                            \n";
std::cout << "                 |                            \n";
std::cout << "              .-\"^\"-.                       \n";
std::cout << "             /_....._\\                       \n";
std::cout << "         .-\"`         `\"-.                  \n";
std::cout << "        (  ooo  ooo  ooo  )                   \n";
std::cout << "         '-.,_________,.-'    ,-----------.   \n";
std::cout << "              /     \\        (  Send help! ) \n";
std::cout << "             /       \\      / `-----------'  \n";
std::cout << "            /         \\    /                 \n";
std::cout << "           /           \\                     \n";
std::cout << "          /             \\                    \n";
std::cout << "         /               \\                   \n";

}
}

I haven't been coding for too long so tips are always welcome

  • 1
    Possible duplicate of [rand() generating the same number – even with srand(time(NULL)) in my main!](https://stackoverflow.com/questions/3032726/rand-generating-the-same-number-even-with-srandtimenull-in-my-main) – Tom Mar 20 '19 at 14:05
  • 2
    Global variables are initialized before `main` is run, i.e. before you call `srand`. – 0x5453 Mar 20 '19 at 14:05

1 Answers1

2

It's a problem of timing. The random number expression in your header is executed before the random number generator seeding code in your main function. Change your code like this

// header
int rand_number;
std::string codeword;

// source
int main() {
    srand (time(NULL));
    rand_number = rand() % codewords.size();
    codeword = codewords[rand_number];

Now the random number is generated after the random number generator has been seeded.

PS why are the take_turn and display functions in your header file? Functions (unless they are inline) should be placed in source files.

john
  • 85,011
  • 4
  • 57
  • 81
  • I applied the suggested code but it still returns 'camera' as codeword (index 0). It is an exercise on header files in Codecademy so I suppose that's why I had to include those functions in the header file – MartienskieNum1 Mar 20 '19 at 14:12
  • @MartienskieNum1 If codeacademy is suggesting that you should put non-inline functions in header files then you really need to find a better tutorial. – john Mar 20 '19 at 14:24
  • @MartienskieNum1 Sorry I wasn't paying attention. It's basically the same problem again. You are using the `rand_number` first (in the header file) and the setting it afterwards. Obviously it should be the other way around. See updated answer. – john Mar 20 '19 at 14:28
  • Your modification fixed it, thanks a lot :) and what are non-inline functions then? – MartienskieNum1 Mar 20 '19 at 14:35