#include <iostream>
#include <fstream>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <algorithm> //including the 2
#include <vector> //from STL
usingnamespace std;
int main()
{
cout << "Welcome to my word guessing program!\n\n\n";
cout << "In the 'words.txt' file you can see the words that are being used.\n\n";
string befajlnev = "words.txt"; // creating a file
ifstream befajl(befajlnev.c_str(), ios::in); // for reading
if(!(befajl.is_open())) // if not open
{
cout << "NO words.txt\nQUITING...\n\n";
system("PAUSE");
return -1;
}
vector<string> szavak; // creating a vector for the words in the file
string szo; // used for uploading the <vector>
string kevert_szo; // for saving the word
int guesses = 1; // counter for the guesses
string probalkozas; // the guess
bool vege = false;
while(!befajl.eof()) // filling the <vector>
{
befajl >> szo;
szavak.push_back(szo);
}
int random_max = szavak.size(); // selecting a word to be the one to be guessed
srand(time(NULL));
szo = szavak[rand() % (random_max)];
kevert_szo = szo;
random_shuffle(kevert_szo.begin(), kevert_szo.end());
cout << "Here is the word, try to guess it: " << kevert_szo << endl;
while(vege != true) // main loop...
{
cout << "Guess it: ";
cin >> probalkozas;
if( probalkozas == szo )
{
vege = true;
cout << "You guessed it right in " << guesses << " guesses.\n";
}
guesses++;
}
system("PAUSE");
return 0;
}
Note that it was compiled with CodeBlocks. Nothing fancy, just a very simple game, SO MUCH easier with the STL.
Have a nice day!
Hey, I notice you have system("PAUSE");. I was told this should never be used in coding...
that is wrong. read the second post in the beginner forum and that explains it. but system commands are never actually needed and are huge holes in the program
Woow, never thought I would make so many mistakes, I just felt like sharing this code maybe to some it would be helpful. Thanks both of you for your reply though.