Thread: Random Pick from a file

  1. #1
    Registered User
    Join Date
    Jul 2002
    Posts
    34

    Random Pick from a file

    Hi all,
    Can anybody show me how to code a program that will pick a random string of text from a file stored on the local drive of a pc and display it on the screen? I have a copy of Visual C++ 6.0...if that makes any difference. Any help would be appreciated!
    MCorn

  2. #2
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

  3. #3
    Registered User
    Join Date
    Jul 2002
    Posts
    34

    Errors?

    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

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code tags are nice. However, there's a lot to be said for good indentation also. Or any indentation for that matter...

    I was given the following code:
    You know, a better way to learn would be to try it yourself, instead of relying on someone doing the work for you. But what do I know...

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    The file test.txt should be in the same directory as the executable (unless you are running this from your IDE, in which case it depends on the IDE).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File Writing Problem
    By polskash in forum C Programming
    Replies: 3
    Last Post: 02-13-2009, 10:47 AM
  2. Need Help Fixing My C Program. Deals with File I/O
    By Matus in forum C Programming
    Replies: 7
    Last Post: 04-29-2008, 07:51 PM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM