STL vector usage

Hello obsessed C++ users.

I wrote a program using the STL. I hope i don't offend anyone if I share this, it may be useful for some.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <iostream>
#include <fstream>

#include <time.h>
#include <stdio.h>
#include <stdlib.h>

#include <algorithm> //including the 2
#include <vector>    //from STL

using namespace 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!
Last edited on
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
.
Last edited on
In C++ the listed headers

1
2
3
#include <time.h>
#include <stdio.h>
#include <stdlib.h> 

should be written as

1
2
3
#include <ctime>
#include <cstdio>
#include <cstdlib> 


And I do not understand why was stdio.h included?!

I do not see any sense to define a variable of type std::string to store file name

string befajlnev = "words.txt"; // creating a file
I think it would be better to define it as

const char *befajlnev = "words.txt";

Also by default ifstream objects have open mode ios::in. So it would be simply to write

ifstream befajl( befajlnev ); // for reading

instead of

ifstream befajl(befajlnev.c_str(), ios::in); // for reading

Also I do not see any sense to define variable random_max.

Instead of the loop while as below

1
2
3
4
5
6
7
8
9
10
11
    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++;
    }


it would be better to use the loop do-while

1
2
3
4
5
6
7
8
9
10
11
    do                             // main loop...
    {
        cout << "Guess it: ";
        cin >> probalkozas;
        if( probalkozas == szo )
        {
            vege = true;
            cout << "You guessed it right in " << guesses << " guesses.\n";
        }
        guesses++;
    } while ( ! vege );


This loop shall be rewritten in whole the following way

1
2
3
4
5
6
7
8
9
10
    do                             // main loop...
    {
        cout << "Guess it: ";
        cin >> probalkozas;
        guesses++;
        vege = probalkozas == szo;
    } while ( ! vege );

     cout << "You guessed it right in " << guesses << " guesses.\n";



So in general your code is bad.

Also the initial value of guesses shall be 0.

And it is a bad programming style when your variables have non-english names. It is difficult to read such programs.
Last edited on
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.
Topic archived. No new replies allowed.