I begin with c++ and i'm just doing a mini Game, and i would like to open my file.txt which contains:
Hello
Test
Random
Mysterious
Nice
Good
Uber
Facebook
etc...
and inside my code i put myself a word inside my variable :
RandName
So how i could open a file.txt, take a random word inside my file and insert into my game. I think i have to use ofstream but i don't really know how to use it.
Code:
#include <iostream>
#include <string>
#include <ctime>
#include <cstdlib>
using namespace std;
string Shake(string str){
string melange;
int i(0);
while (str.size() != 0){
i = rand() % str.size();
melange += str[i];
str.erase(i, 1);
}
return melange;
}
int main(){
std::cout << "Welcome to secret word : ";
string RandName("Random");
string Reponse("");
string RandNameShake("");
int cpt(0);
int lose(10);
int Replay(0);
srand(time(0));
std::cin >> Reponse;
while (RandName != Reponse && lose != 0) {
RandNameShake = Shake(RandName);
std::cout << "Wrong word ! " << '\n';
std::cout << endl << "The secret word is : " << RandNameShake << endl;
std::cout << "HIT : " << lose << '\n';
std::cout << endl << "Try again : ";
std::cin >> Reponse;
cpt++;
lose--;
}
if (lose == 0 ) {
std::cout << endl << "Sorry you don't find the word ... " << '\n';
std::cout << endl << "The word was : " << RandName <<'\n';
std::cout << endl << "An other game ? 1/Yes 2/No" << '\n';
std::cin >> Replay;
}
else
std::cout << endl << "Good Game yu find the word in " << cpt << " hits" << endl;
std::cout << endl << "An other game ? 1/Yes 2/No" << '\n';
std::cin >> Replay;
if (Replay == 1) {
main();
}
else if (Replay == 2) {
std::cout << "All right see you soon :) !" << '\n';
return 0;
}
else
std::cout << "Don't understand i close the program" << '\n';
return 0;
return 0;
}