Help me with my project

This is a discussion on Help me with my project within the C Programming forums, part of the General Programming Boards category; i cant random the question i have 10 question and only 5 question will appear in one category since that ...

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    10

    Help me with my project

    i cant random the question
    i have 10 question and only 5 question will appear in one category since that is not complete
    i hope anyone can help me to random the question

    im only beginner..
    here my sample code

    Code:
    # include <stdio.h>
    # include <stdlib.h>
    int score;
    main()
    {
    char cat;
    clrscr();
    printf ("Welcome to Trivia");
    getch();
    back1:
    clrscr();
    printf ("\n");
    printf ("                              Select Category\n");
    printf ("\n");
    printf ("\n");
    printf ("\n");
    printf ("\n");
    printf ("    [A] HISTORY\n");
    printf ("\n");
    printf ("    [B] GENERAL KNOWLEDGE\n");
    printf ("\n");
    printf ("    [C] ENGLISH\n");
    printf ("\n");
    printf ("    [D] SCIENCE\n");
    printf ("\n");
    printf ("    [E] QUIT\n");
    printf ("    Enter a category: ");
    scanf ("%c",&cat);
    switch (cat)
    {
    case 'A':history();break;
    case 'a':history();break;
    case 'B':;break;
    case 'b':;break;
    case 'C':;break;
    case 'c':;break;
    case 'D':;break;
    case 'd':;break;
    case 'E':;break;
    case 'e':;break;
    }
    goto back1;
    }
    
    
    
    history ()
    {
    
    char ans;
    int  ok=0,clr=0,q;
    clrscr();
    printf ("\n\n\n                       HISTORY\n");
    getch();
    back:
    clrscr();
    clr++;
    q=random(1);
    printf ("%d",q);
    getch();
    switch (q)
    {
    case 0:
    if (ok != 1)
    {
    printf ("\nWho is the first to discover PAPER?\n\n\n");
    printf ("[A] India              [C] Egypt\n\n");
    printf ("[B] China              [D] none of the above\n");
    printf (" Answer: ");
    scanf  ("%c",&ans);
    switch (ans)
    {
    case 'B':break;
    case 'b':break;
    default :break;
    }
    printf ("%d",clr);
    if (clr >= 10)
    {
    getch();
    main();
    ++ok;
    }
    else
    {
    goto back;
    ++ok;
    }
    break;
    }
    
    case 1:
    if (ok != 1)
    {
    printf ("\nWhat is the first to discover PAPER?\n\n\n");
    printf ("[A] India              [C] Egypt\n\n");
    printf ("[B] China              [D] none of the above\n");
    printf (" Answer: ");
    scanf  ("%c",&ans);
    switch (ans)
    {
    case 'B':break;
    case 'b':break;
    default :break;
    }
    printf ("%d",clr);
    if (clr >= 10)
    {
    getch();
    main();
    ++ok;
    }
    else
    {
    goto back;
    ++ok;
    }
    }
    break;
    }
    
    
    }
    
    
    
    thanks for help..

  2. #2
    Registered User
    Join Date
    Feb 2010
    Posts
    57
    * you can have the 10 questions in an array of strings.
    * Then using rand() function or your own random algorithm you can generate any random numbers.
    * Then you can use that random number as an index to get the questions from that array.

  3. #3
    Registered User
    Join Date
    Mar 2010
    Posts
    10
    can you give some example anyway thanks for answering my help

  4. #4
    Registered User
    Join Date
    Feb 2010
    Posts
    57
    This is just a sample code. I hope this will help you.
    Code:
    #include<stdio.h>
    int main()
    {
            char *q[]={ "c", "c++", "java" };
            srand(time()); // It will set the seed as epoch second
            printf("%s\n",q[rand()%3]); // get the index value within 3 and print that string
            return 0;
    }

  5. #5
    Registered User
    Join Date
    Mar 2010
    Posts
    10
    ^ i cant do it can u help me in coding in case random
    which if i press the answer then another question will follow
    or check my code
    i got some error on that thanks.

  6. #6
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,409

    rand

    first, learn some indentation so you can post your queries in a nice way for people to read.

    you need to put a loop in to have the program ask you the questions more than once, you can use a for loop, or a while loop for example.

    this is a way of getting a random number that you can then test in your switch or whatever >

    Code:
    //remember to include a header like time.h with a random number function in it
    
    const int QMAX = 3; //this so that you can set the max number of questions to choose from
    
    int main()
    {
        int qnum = 0;
        int count = 0;
        
        srand(time(NULL)); //always leave this outside of any loop and 
                           //only use once in the program, usually at the beginning like here for example.
        
        for(count = 0; count < 10; count++) //loop ten times
        {
            qnum = rand() % QMAX;          //choose a random number 0, 1 or 2
            printf("question number = %d\n", qnum);
        }
        printf("\n");
        
    return 0;
    
    }
    now if you think about it you can put more stuff inside the loop, so it asks questions each time etc etc.
    before the loop starts you can do your first display, the title and categories etc,...

    get rid of the goto part that you had before.
    Last edited by rogster001; 03-15-2010 at 07:28 AM.

  7. #7
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,794
    Code:
    history ()
    {
    
        ...
    
        switch (q)
        {
        case 0:
    
                ...
    
                if (clr >= 10)
                {
                    getch();
                    main();
                    ++ok;
                }
    
                ...
    
        case 1:
    
                ...
    
                if (clr >= 10)
                {
                    getch();
                    main();
                    ++ok;
                }
    
                ...
        }
    
    
    }
    Ugh... no, never call main.
    Last edited by hk_mp5kpdw; 03-15-2010 at 11:15 AM. Reason: Removed "recursively". Recursively or not you should not call main directly.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  8. #8
    Registered User
    Join Date
    Mar 2010
    Posts
    10
    Quote Originally Posted by rogster001 View Post
    first, learn some indentation so you can post your queries in a nice way for people to read.

    you need to put a loop in to have the program ask you the questions more than once, you can use a for loop, or a while loop for example.

    this is a way of getting a random number that you can then test in your switch or whatever >

    Code:
    //remember to include a header like time.h with a random number function in it
    
    const int QMAX = 3; //this so that you can set the max number of questions to choose from
    
    int main()
    {
        int qnum = 0;
        int count = 0;
        
        srand(time(NULL)); //always leave this outside of any loop and 
                           //only use once in the program, usually at the beginning like here for example.
        
        for(count = 0; count < 10; count++) //loop ten times
        {
            qnum = rand() % QMAX;          //choose a random number 0, 1 or 2
            printf("question number = %d\n", qnum);
        }
        printf("\n");
        
    return 0;
    
    }
    now if you think about it you can put more stuff inside the loop, so it asks questions each time etc etc.
    before the loop starts you can do your first display, the title and categories etc,...

    get rid of the goto part that you had before.
    thanks it really helpful
    btw how about if the number is use the number will not appear

    for example
    the q1 is now answered then
    on the next question the q1 will not appear anymore aside in use again the category

    sorry for my english

  9. #9
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Have another array of type int parallel to your questions array. Have this array initially initialized to 0. When a question is answered, mark the integer at the index of the question in the int array as 1. Then, when you choose questions never choose a question that has its status set to 1 in the int array.

  10. #10
    Registered User
    Join Date
    Mar 2010
    Posts
    10
    can u give some example of that sorry im noob only here..

  11. #11
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Well let's say that you have:

    Code:
    char *q[] = {"question1","question2","question3"};
    /*declare a parallel array of integers */
    int status[3] = {0,0,0};
    Now whenever you select one of the questions say question i (where 1 <= i <= 3) you:
    check if status[i] == 1 (if so that means this question was used before so skip it)
    else use question and set status[i] = 1; (to mark this question as used for when you check the next time)

  12. #12
    Registered User
    Join Date
    Mar 2010
    Posts
    10
    i can't get it..

  13. #13
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    What have you tried so far? Show us your code.

  14. #14
    Registered User
    Join Date
    Mar 2010
    Posts
    10
    i already do it thanks how about if i use the case how i will put the if statement to dont repeat it again

  15. #15
    Registered User
    Join Date
    Mar 2010
    Posts
    10
    anyone know how about the case statement will not repeat when the case is used

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem Displaying a Struct
    By rockstarpirate in forum C++ Programming
    Replies: 16
    Last Post: 05-05-2008, 09:05 AM
  2. added start menu crashes game
    By avgprogamerjoe in forum Game Programming
    Replies: 6
    Last Post: 08-29-2007, 01:30 PM
  3. Dynamic Binding
    By gpr1me in forum C++ Programming
    Replies: 1
    Last Post: 03-24-2006, 08:01 AM
  4. Game Independent Anti-cheat Project Needs Programmers
    By GIA Project Lea in forum Projects and Job Recruitment
    Replies: 3
    Last Post: 09-15-2005, 07:41 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21