Random number generation does not work although srand is initialized correctly

Hey there,
yesterday I wrote this simple program:

#include <math.h>
#include <string>
#include <conio.h>
#include <cstdlib>
#include <ctime>

using namespace std;

int main()
{
srand(time(NULL));
int counter=1;
int number=0;
int random;
string answer;
string currentline;
bool stopped = false;
string Voc[10000];
ifstream read("Voc.txt");



while(!read.eof())
{
getline(read,Voc[number+1]);
number++;
}
counter = 1;
while(!stopped)
{
random = rand() % 1 + number/2;

cout << Voc[random*2-1] << endl;
cin >> answer;

if(answer == Voc[random*2])
{
cout << "Right!" << endl;
cout << random;
getch();
counter = counter + 1;
}
else
{
cout << "False!" << endl;
cout << random;
getch();
counter = counter + 1;
}
}





}

This is supposed to be a simple vocabulary trainer.
Voc.txt contains a few simple Latin words with the English meaning, structured like this:

servus
slave
dominus
master
corpus
body
...


The trainer itself runs fine, but it seems as if it always picks the same word.
As you see, I initialized srand correctly, so I have no idea what is going wrong there. Does anybody have an idea?
first off: please use the code tag and intend properly, otherwise people will have a hard time reading your code: http://www.cplusplus.com/articles/jEywvCM9/

The next thing i notice is this: you never change number which is used to generate a random number.
And x % 1 = 0;, there is no integer you can divide by 1 that gives rest.
random = rand() % 1 + number/2;

Code:
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
#include <math.h>
#include <string>
#include <conio.h>
#include <cstdlib>
#include <ctime>

using namespace std;

int main()
{
    srand(time(NULL));
    int counter=1;
    int number=0;
    int random;
    string answer;
    string currentline;
    bool stopped = false;
    string Voc[10000];
    ifstream read("Voc.txt");
    
    while(!read.eof())
    {
        getline(read,Voc[number+1]);
        number++;
    }
    counter = 1;
    while(!stopped)
    {
        random = rand() % 1 + number/2;
        
        cout << Voc[random*2-1] << endl;
        cin >> answer;
        
        if(answer == Voc[random*2])
        {
            cout << "Right!" << endl;
            cout << random;
            getch();
            counter = counter + 1;
        }
        else
        {
            cout << "False!" << endl;
            cout << random;
            getch();
            counter = counter + 1;
        }
    }
}


edit:
you might want to have a class that holds both strings (lat and eng)
you might want to use std::vector of that class for dynamic resizing of your dictionary

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Word
{
public: 
  Word() {}
  Word(const Word& word) { *this = word; }

  std::string lat;
  std::string eng;
};
std::vector<Word> dictionary;

// read the file
while(!read.eof())
{
  Word word;

  getline(read, word.lat);
  getline(read, word.eng);

  dictionary.push_back(word);
}


given that as base, you can just call your random like this:
random = rand() % dictionary.size(); // gives index to random word in dictionary

and ask the value like this:
1
2
3
4
5
6
7
8
if(answer == dictionary[random].eng)
  cout << "Right!" << endl;
else
  cout << "Wrong!" << endl;

cout << random;
getch();
counter = counter + 1;
Last edited on
Well number IS actually changed...
1
2
3
4
5
while(!read.eof)
{
getline(read,Voc[number+1]
number++;
}

So it finds out, how many words are actually in the text file. But the idea of putting them into two strings actually helped me ^^
oh yeah, didn't notice that ^^

So, does it work now? :D
Yep. Thanks ^^
Topic archived. No new replies allowed.