-2

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;
}
melpomene
  • 84,125
  • 8
  • 85
  • 148
Yns_D
  • 19
  • 7
  • 2
    `std::ofstream` is for output (that what first 'o' at the type name means). You need `std::ifstream` - how to use it you can find online or almost any C++ textbook. – Slava Mar 16 '18 at 15:25
  • This is what I used to learn basic file IO: http://www.learncpp.com/cpp-tutorial/186-basic-file-io/. The class you need is `std::ifstream` – Arnav Borborah Mar 16 '18 at 15:27
  • You may find [this](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) of use – UKMonkey Mar 16 '18 at 15:32
  • Thanks i will try – Yns_D Mar 16 '18 at 15:39
  • `[teach-me]` Since this looks very much like a homework, please work with your professor/teacher. This question demonstrates that you need a lot more explaining before you are ready to write code. –  Mar 16 '18 at 15:41

1 Answers1

0

This sounds awfully lot like homework assignment, so I will leave the code for you and limit the answer to general directions.

I think i have to use ofstream

Try again. ofstream as the name suggests is for output, and what you need in this case is input.

Onto getting a random word - an inefficient native solution would involve counting the number of lines/words, picking one at random and skipping ahead till you reach it. That however involves a lot of unnecessary operations that can be avoided.

Another solution, inefficient in a different way would involve reading all of the words into a container and picking at random an index from the word container. That solution might however turn out to be more efficient if you are gonna do a lot of random word picking.

For a single word it would be better to pick a position in the file at random and seek ahead until you find the next new line, and read a line from that position. If you reach the end without finding one, that means the position happened to be in the last line, so you need to go back until you find the first new line, that will represent the last line in the file and read that.

In either case, you will end up with a word picked at random with minimum amount of overhead.

dtech
  • 47,916
  • 17
  • 112
  • 190