Thread: random question, random choices multiple choice

  1. #1
    Registered User
    Join Date
    Oct 2013
    Posts
    3

    random question, random choices multiple choice

    can someone help me? i need to make a program that prints out 10 random questions from the 15 questions in a .txt file, and then print out the choice IN RANDOM. please do help me. i really need to finish this.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What idea do you have to solve this? Can you write a program that prints out the last 10 questions from the 15 questions in that file?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > please do help me. i really need to finish this.
    Well that suggests that you've at least made a start.

    Perhaps you should post a specific question demonstrating the point you're stuck on.

    There's no point us explaining how to read a file (for example), when all you care about is the random order aspect of the problem.

    > can someone help me?
    How To Ask Questions The Smart Way
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Oct 2013
    Posts
    3
    sorry, i'm just new here. so here's what i got, just trying to print out 10 random numbers out of 15 from a txt file.
    Code:
    #include<iostream>
    #include<conio.h>
    #include<stdlib.h>
    #include<time.h>
    
    
    int main ()
    {
        FILE *fp;
        fp=fopen("D:\\2.txt", "r");
        int stk[10];
        srand ((unsigned)time(NULL));
        for (int c=0; c<9; c++)
        {
           int x = rand ()%10;
    fgets(stk[c],50,fp+x);
    fp=fp-x;
    }
    fclose(fp);
    system("pause>0");
    }
    and, it doesn't work. what am i doing wrong?

  5. #5
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    and, it doesn't work. what am i doing wrong?
    The fp pointer does not point to the number of lines or anything that would help you make a choice among which questions to use. It simply points to the location (memory address) of the stuff needed to actually use your file. fp+x actually changes the address you read from.

    Of course, people would also take issue with the fact that you're using the C library despite not including it.

    You can use seekg() to do what you were attempting, though. Note that this way uses C++ streams.

  6. #6
    Registered User
    Join Date
    Oct 2013
    Posts
    3
    can you please teach me how to make the fp pointer point to the number of lines?

  7. #7
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Quote Originally Posted by ccarlorafael View Post
    can you please teach me how to make the fp pointer point to the number of lines?
    No. That isn't what the fp pointer is for.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    How about starting with the basics.
    In C++ (note, this means using iostream and not stdio), write a program to just print every line of the file.

    No randomness, no "10 from 15", no choices - just print the file.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. random choice
    By quo in forum C Programming
    Replies: 2
    Last Post: 01-25-2012, 12:46 PM
  2. Botched random choice selection function
    By Xom in forum C++ Programming
    Replies: 3
    Last Post: 08-12-2011, 01:03 PM
  3. Multiple choice question.
    By ungalnanban in forum C Programming
    Replies: 2
    Last Post: 02-18-2010, 10:40 AM
  4. Replies: 4
    Last Post: 11-16-2004, 07:29 AM
  5. Multiple random numbers!
    By LynuxPenguin in forum C Programming
    Replies: 1
    Last Post: 06-01-2002, 08:17 PM

Tags for this Thread