[Error Log|Solved] strcpy clear source

eclipse CDE
MinGW

From Lab sample code.


answer_word is clear although it is const char*.




Reason: 
const char answer_word[] = 's', 'e', 'm', 'e', 's', 't', 'e', 'r' }; // the secret word
without '\0' at the end of string.

Solution :
add '\0' at the end when initializing string
const char answer_word[] = 's', 'e', 'm', 'e', 's', 't', 'e', 'r', '\0' };




#include "stdio.h"

#include <iostream>

#include <cstdlib>

#include <ctime>

#include "string.h"

using namespace std;
/*
* The word guessing game
*/

/*
* display(const char[], int): Print out the character array in a single line.
*/
void display_word(const char word[], int size)
{
 // TODO 1: print the word
 for (int i=0; i<size; i++)
  printf("%c ", word[i]);
 printf("\n");
}

/*
* success(char[], int): Check whether the secret word has been guessed.
*/
bool success(const char guess_word[], int size)
{
 // TODO 2: return true if the guess_word is completed, otherwise return false
 for (int i=0; i<size; i++) {
  if (guess_word[i]=='_')
   return false;
 }

 return true;
}

/*
* 1st hint function
* generate_hint_random_words(const char[], int, char[]): Generate a new character array by reordering the given array.
*/
void generate_hint_random_words(const char answer_word[], int size, char hint[])
{
 // TODO 3: randomize the characters in answer_word array and stored in hint array
 char ch=0;
 int temp=0;

 strncpy(hint, answer_word, size);
 srand(time(NULL));

 for (int i=0; i<size; i++) {
  temp = (rand()%size);

  ch = hint[temp];
  hint[temp] = hint[i];
  hint[i] = ch;
 }
}

/*
* 2nd hint function
* generate_hint_unique_words(const char[], int, char[], int): Generate a new character array which contains distinct elements of the given character array.
*/
void generate_hint_unique_words(const char answer_word[], int size, char hint[], int &num_unique)
{
 // TODO 4: Generate the hint array by finding the unique characters and the number of unique characters in the answer_word
 char *ch;

 num_unique = 0;
 ch = (char *)malloc( sizeof(char)*(size+1) );
 strncpy(ch, answer_word, size);
 ch[size] = '\0';

 for (int i=0; i<size;i++) {
  for (int j=i+1; j<size; j++) {
   if (ch[i]==ch[j])
    ch[j]=NULL;
  }
  if (ch[i]!=NULL)
   hint[num_unique++] = ch[i];
 }
}

/*
* 3rd hint funtion
* generate_hint_partial_words(const char[], int, char, int): Generate a new character array by replacing n partial characters in the given array with '*'.
*/
void generate_hint_partial_words(const char answer_word[], int size, int n, char hint[])
{
 // TODO 5: Generate the hint array by randomly replacing n characters in the answer_word
 int temp=0;

 strncpy(hint, answer_word, size);
 srand(time(NULL));

 for (int i=0; i<n;) {
  temp = (rand()%size);
  if (hint[temp]!='*') {
   hint[temp] = '*';
   i++;
  }
 }
}

/*
* play(const char[], int, char[]): guess the secret word
*/
void play(const char answer_word[], int size, char guess_word[])
{
 int position = 0;
 char character;
 while (true)
 {
  cout << "Please choose the position [1-" << size << "] you want to fill in." << endl;
  cout << "Or you can enter 0 to quit the game: ";
  cin >> position;
  if (position == 0)
   break;

  // check the position
  while (position < 0 || position > size || guess_word[position-1]!='_')
  {
   cout << "Invalid position, please enter again: ";
   cin >> position;
  }
  if (position == 0)
   break;

  cout << "Please enter the character [a-z]: ";
  cin >> character;

  // check the input charater
  while (character < 'a' || character > 'z')
  {
   cout << "Invalid character, please enter again: ";
   cin >> character;
  }


  // TODO 6: complete the following
  // 1. check if the user's input is correct or not against the secret word (i.e. answer_word[])
  // 2. update the word puzzle (i.e. guess_word[]) with the user's input
  // 3. check if the word puzzle is completed, i.e. all letters in the guess_word[] are filled in correctly
  // 4. continue the game and display the word_puzzle (i.e. guess word)

  if (character == answer_word[position-1]) {
   printf("Great choice :> Please continue!\n");
   guess_word[position-1] = character;
  } else
   printf("Seems to be a bad choice :< Try again\n");

  if (success(guess_word, size)) {
   printf("Congratulations! The secret word is: \n");
   display_word(guess_word, size);    // display the word puzzle
   break;
  }else {
   printf("\n");
   printf("Guess the word:\n");
   display_word(guess_word, size);    // display the word puzzle
  }

  cout << endl;
 }
}


int main()
{
const char answer_word[] = { 's', 'e', 'm', 'e', 's', 't', 'e', 'r' }; // the secret word
 const int SIZE = 8;
 char hint[SIZE+1] = {};
 int num_unique = 0, hint_mode = 0, partial_size = 0;
 char guess_word[SIZE] = {};   // the word puzzle
 char replay = 'n';

 do
 {  // print the main menu
  cout << "******************************WELCOME TO THE WORD GUESSING GAME*****************************" << endl;
  cout << "Try to fill in the blanks with letters to figure out the secret word given the hint!" << endl << endl;
  cout << "Please choose one of the following hint mode:" <<endl;
  cout << "1. Randomized word"<< endl;
  cout << "   e.g: If the secret word is 'lucky', the hint might be 'clyuk'. " << endl;
  cout << "2. Unique letters of the secret word" <<endl;
  cout << "   e.g: If the secret word is 'address', the hint is 'adres'. " << endl;
  cout << "3. Partial word" <<endl;
  cout << "   e.g: If the secret word is 'welcome', the hint might be 'w*lc*m*'. "<<endl;
  cout << "Please enter one of the hint mode: ";
  cin >> hint_mode;

  // check the input
  while (hint_mode < 1 || hint_mode > 3)
  {
   cout << "Invalid input! Please choose one of the hint mode: ";
   cin >> hint_mode;
  }

  // initialize the word puzzle
  for (int i = 0; i < SIZE; ++i)
  {
   guess_word[i] = '_';
  }

  // generate a hint array according to the hint mode the player chosed
  switch (hint_mode)
  {
  case 1 :
   generate_hint_random_words(answer_word, SIZE, hint);
   cout << "Hint: ";
   display_word(hint, SIZE);
   cout << endl;
   break;

  case 2:
   generate_hint_unique_words(answer_word, SIZE, hint, num_unique);
   cout << "Hint: ";
   display_word(hint, num_unique);
   cout << endl;
   break;

  case 3:
   cout << "Please enter the number of characters you want to eliminate [1-4]: ";
   cin >> partial_size;
   while (partial_size < 1 || partial_size > 4)
   {
    cout << "Invalid input! Please enter the number of characters you want to eliminate [1-4]: ";
    cin >> partial_size;
   }
   generate_hint_partial_words(answer_word, SIZE, partial_size, hint);
   cout << "Hint: ";
   display_word(hint, SIZE);
   cout << endl;
   for (int i = 0; i < SIZE; ++i)
   {
    if (hint[i] != '*')
    {
     guess_word[i] = hint[i];
    }
   }
   break;

  default:
   cerr << "Error: Invalid hint mode " << hint_mode << endl;
   break;
  }

  cout << "Guess the word:" << endl;
  display_word(guess_word, SIZE);    // display the word puzzle
  cout << endl;
  play(answer_word, SIZE, guess_word); // guess the secret word

  cout << endl;
  cout << "Play again? (y/n) ";
  cin >> replay;
  cout << endl;

 } while (replay == 'y');

 return 0;
}


留言

此網誌的熱門文章

[OpenCV] RAW rgb data to IplImage | 以rgb原始資料建立IplImage指標

Monitoring System Spec.& IO Mapping

突如奇來的高科技電子零件