Hi,
I appreciate the help on this. I was given the following code:
Code:
#include<iostream>
#include<fstream>
#include<string>
#include<ctime>
#include<cstdio>
int main()
{
fstream readfile;
int num=0,max=0;
char a;
string b;
readfile.open("test.txt",fstream::in);
srand((unsigned int)time(0));
while(readfile>>b)
{
max++;
}
readfile.close();
cout<<"press 1 to show name,else to exit"<<endl;
cin>>a;
while(a='1')
{
readfile.open("test.txt",fstream::in);
num=rand()%max;
while(num>=0)
{
readfile>>(b);
num--;
}
cout<<b<<endl;
cout<<"press 1 to show name,else to exit"<<endl;
cin>>a;
cin.ignore();
readfile.close();
}
return 0;
}
When I tried to run it I get errors. It may be where I placed the test.txt file. (C:\test.txt) Can anybody tell me where I've gone wrong? I'm trying to draw a random name from a text file and display it on the screen. In the text file I have as follows:
Joe Blow
Homer Simpson
Sally Lastname
etc..
etc..
Thanks for any help!
MCorn