program to pick a random name from a file with list of names

Hi, I just recently started looking into C++ programming so I don't know much.

I was wondering is it possible to write a program to pick a random name from a list of names, which are in a .txt file? or if I could ask the user to input the list of names and then let the program pick a random name?

Thanks in advance!
I have it so that the maximum number of name entries is 100. The program will keep asking if you want a random name, the same random name can appear twice in a row. Hope it helps. PS. There are many other ways to implement using text files or whatever. Below, I use an string array.

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
#include <iostream>
#include <string>
#include <time.h>

using namespace std;

int main(){
	srand(time(NULL));
	string one;
	string name[100];
	int count = 0;
	string again;
	again = "y";
	int num = 0;
	
	while(one != "0")
	{
		cout<<"Enter name: (0 to stop entering names)"<<endl;
		cin>> one;
		if(one == "0") break;
		name[count] = one;
		count++;

	}
	
	while(again == "y"){
		
		num = rand()%count;
		cout<<num;
		cout<<"Random name chosen was "<<name[num]<<endl;
		cout<<"Chose again? (y/n)";
		cin >> again;
	}


	cin.get();
	cin.ignore();
}
you should use rand() once before you call it in your function because the first call to it is flawed... your function would produce better results... and you should call srand() too before you call rand() and include "time.h"... I think it's
1
2
3
srand( static_cast<unsigned int>(time(NULL)));
// then rand();
rand();

and then your code... enjoy!

[edit] Damn I didn't notice you had already called srand()... :-)
Last edited on
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
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
#include <ctime>
//
// invariant: file contains at least one line
std::string random_line( const char* path )
{
    std::string selected ;
    std::ifstream file(path) ;
    //
    std::string line ;
    std::streamsize nlines = 0 ;
    while( std::getline( file, line ) )
    {
        ++nlines ;
        if( std::rand() % nlines == 0 ) selected = line ;
    }
    //
    return selected ;
}
//
int main()
{
    std::srand( std::time(0) ) ; // seed the C library lcg
    //
    for( int i = 0 ; i < 3 ; ++i ) // repeat thrice
    {
        // print out a random line selected from this file
        std::cout << random_line(__FILE__) << '\n' ;
    }
}

http://coliru.stacked-crooked.com/a/fea6bcffc923a84b
Also, I think you should write

while(one != "0" && count < 100)

so you don't exceed the bounds of the array... :-)
Last edited on
Thank you all for the help. Much appreciated. :)

@JLBorges: Hi, I tried running the program but doesn't compile. Could you please include an example for how I have to enter the path of the file? Thanks.
> I tried running the program but doesn't compile.

http://coliru.stacked-crooked.com/a/fea6bcffc923a84b


> Could you please include an example for how I have to enter the path of the file?

1
2
3
4
5
6
7
8
9
10
11
12
13
int main()
{
    std::srand( std::time(0) ) ; // seed the C library lcg

    const char* const path = "/usr/home/mclaude/learn_cpp/data/names.txt" ;
          // "C:/Documents and Settings/Mc Laude/Desktop/data/names.txt" ;
    std::cout << random_line(path) << '\n' ;

    std::string file_name ;
    std::cout << "file to read from? " ;
    std::getline( std::cin, file_name ) ;
    std::cout << random_line( file_name.c_str() ) << '\n' ;
}


Topic archived. No new replies allowed.