Welcome
Welcome to the forums of AntiRTFM's Absolute N00b Spoonfeed C++ Tutorials!

You are currently viewing our boards as a guest, which gives you limited access to view most discussions and access our other features. By joining our free community, you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content, and access many other special features. Registration is fast, simple, and absolutely free, so please, <a href="/profile.php?mode=register">join our community today</a>!

String not changing.

Think you can spy out that little nasty bug? Try debugging some code in here

String not changing.

Postby backlash53 on Fri Sep 11, 2009 10:49 am

Code: Select all
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <cstdlib>
#include <ctime>

using namespace std;

class hangman
{
public:
   hangman(); // prompt names.
   void readWords(); //read words from a txt file and store them into a vector.
   void setWord();// sets a random word from the vector
   void promptUser();
   string getWord() { return theWord; } // accessor
   string getWordC() { return theWordC; } // accessor
   

private:
   string words;
   vector<string> allWords;
   ifstream file;
   string pone; // player one's name
   int random;
   string theWord; // chosen word
   string theWordC; // consealed chosen word
   char letter;
   int tries;
   int time;

   
};

int main()
{
   bool cont = 1;
   srand((unsigned)time(0));
   hangman game;
   game.readWords();
   game.setWord();
   while (cont)
   {
        game.promptUser();
   }
   
   char f;
   cin >> f;
   return 0;
}
hangman::hangman() : tries(0), time(60)
{
   std::cout << "Player one, enter your name: ";
   std::cin >> pone;
   
}
void hangman::readWords()
{
   file.open("words.txt");
   
   if (!file )
   {
      cout << "Unable to open file";
      
   }

   while (file >> words )
   {
      allWords.push_back(words);
   }
   file.close();

}

void hangman::setWord()
{
  random = (rand()%10)+1; // 1 - 10
 
  for ( int i = 0; i != random; i++)
  {
     theWord = allWords[random];
  }
 
}

void hangman::promptUser()
{
   string temp(theWord.size(), '_');
   theWordC = temp;
   cout << "TRIES: " << tries << "\t" << "TIME LEFT: " << time << endl;
   cout << "WORD SO FAR: " << theWordC << endl;
   
   cout << pone << ", guess a letter: ";
   cin >> letter;
   tries++;
   
   

   for ( int i = 0; i != theWord.size(); i++ )
      {
         if ( letter == theWord[i] )
         {
            theWordC[i] = letter;
         }
   }

   if (theWordC == theWord)
   {
      cout << "Congratulations !, you win :)" << endl;
   }
   
}


Firstly please make a file called "words.txt" and place it in the same folder as the program, In the file make sure there are at least 10 words seperated on each different line then finally run the program and you will see that the string that's concealing the word isn't showing the characters that have been gueesed for some reason.
backlash53
 
Posts: 26
Joined: Wed Feb 04, 2009 1:42 pm

Re: String not changing.

Postby C++ on Fri Sep 11, 2009 1:11 pm

Consider what happens if the user enters non numerical characters or multiple characters. Improve your program by getting proper user input. It's mentioned numerous times here on the forum, so you shouldn't have any problems implementing it. I fixed your code and added a time check.

Code: Select all
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <cstdlib>
#include <ctime>

using std::cout;
using std::cin;
using std::string;
using std::vector;
using std::ifstream;

class hangman
{
public:
   hangman(); // prompt names.
   void readWords(); //read words from a txt file and store them into a vector.
   void setWord();// sets a random word from the vector
   void promptUser();
   string getWord() { return theWord; } // accessor
   string getWordC() { return theWordC; } // accessor

private:
   string word;
   vector<string> allWords;
   ifstream file;
   string pone; // player one's name
   string theWord; // chosen word
   string theWordC; // consealed chosen word
   char letter;
   int tries;
   double timeout;
};


int main()
{
   srand((unsigned)time(0));
   hangman game;
   game.readWords();
   while (1)
   {
      game.setWord();
      game.promptUser();
   }
}

hangman::hangman() : tries(0), timeout(60.)
{
   cout << "Player one, enter your name: ";
   cin >> pone;
}

void hangman::readWords()
{
   file.open("words.txt");
   if (!file)
      cout << "Unable to open file";
   while (file >> word)
      allWords.push_back(word);
   file.close();
}

void hangman::setWord()
{
   theWord = allWords.at(rand() % 10); // 1 - 10
   word = string(theWord.size(), '*');
}

void hangman::promptUser()
{
   time_t start = time(&start);
   time_t current;
   double seconds = 0.;

   tries = 0;
   timeout = 60.;
   while (word != theWord && tries < 10 && seconds < timeout)
   {
      cout << "TRIES: " << tries << "\t" << "TIME LEFT: " << (timeout - seconds < 0. ? 0. : timeout - seconds) << "\n";
      cout << "WORD SO FAR: " << word << "\n";

      cout << pone << ", guess a letter: ";
      cin >> letter;
      ++tries;
      seconds = difftime(time(&current), start);

      for (int i = 0; i != theWord.size(); ++i)
         if (letter == theWord.at(i))
            word.at(i) = letter;
   }
   if (seconds >= timeout)
      cout << "Time's up !, you lose :(\n";
   else if (word == theWord)
      cout << "Congratulations !, you win :)\n";
   else
      cout << "No tries left !, you lose :(\n";
}
New to C++ programming? Click here
C++
teh awesome
 
Posts: 217
Joined: Sun May 25, 2008 7:45 am

Re: String not changing.

Postby noobgrammer on Fri Sep 11, 2009 1:51 pm

besides c++ fixing it and making it better

just so you know what the problem was you where having was

Code: Select all
   string temp(theWord.size(), '_');
   theWordC = temp;


You where reinitializing it so it wiped out what you guessed each time
I moved it up one function and it worked except it didn't tell me when I won
I need a compiler with a can of RAID built into it

I added a msn messenger just for programming feel free to email or add yourself to it
User avatar
noobgrammer
 
Posts: 198
Joined: Tue Aug 25, 2009 10:44 am
Location: Orlando

Re: String not changing.

Postby backlash53 on Sun Sep 13, 2009 11:06 am

ah thanks, very much yeah my code was pretty messy but now you guy's solved the problem i was having im gonna go fix it up and post the program.
backlash53
 
Posts: 26
Joined: Wed Feb 04, 2009 1:42 pm


Return to Debug

Who is online

Users browsing this forum: No registered users and 0 guests

cron