#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    101
    Rep Power
    12

    Question Random Questions variable?? Me = C++ newb:)


    I am just learning C++ in High School, but we are moving a little to slow for me in there and I was wondering what a command is for random questions in C++ and what i would have to #include?? I am wondering this because I came up with an idea for our Bible Quiz team at church, I want to create a program that randomly askes questions from our practice books. (We have Bible Quizzing which is where some people from our church memorize verses and go to tourneyments and try to get questions pertaining to the verses right and/or quote them.) I think this would be very effective in helping us practice this season (which has not started yet). So any help on this question would be much appreciated.
    Last edited by lildrummerboy; September 20th, 2003 at 01:00 PM.
  2. #2
  3. No Profile Picture
    status unknown
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    262
    Rep Power
    12
    Open up your C++ book or your compiler help and look up the functions rand() and srand(). They're in <cstdlib>. That should get you started. If you try that and still have problems, post your code and we'll try to help you further.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    101
    Rep Power
    12
    thank you much...I actually am not allowed to bring book home because the school has one set...But i shall look it up online though thanks. Here is the code anyways...I just started after I posted this and to be honest like i said I am a newb and i think this is probably really confusing...and I have hours and hours before its going to be done...and i am probably not doing this the easiest way in fact we havent even learned anything in my C++ class yet except for the COUT command...using #include <iostream> that is why i said we were going to slow. So i looked it up and I have put together this sloppy code so far...this is just a test i put together for all the if statements since i hadnt tried them till today. It all compiled and i am not to the point where i need random questions yet. but like i said i will ask if i need some more help. hopefully i wont cause this is very sloppy code probably. I could use some help on a loop though...Dont know if thats what its called i think it is called that. I want to make it so that after a key is pressed the program can go back to the main Screen or main question

    Code:
    #include <stdio.h>
    #include <iostream.h>
    
    
    main()
    {
    	float main_question; //Variable to be used for my first question;
        float bible_verse_chapter;  //Variable for first question, first selection
        float psalm_chapter1verse1; // Variable for selecting a bible quiz 2003 verse to use.
    
    	cout << "Press \"1\" to get a bible quiz verse" //Question to user
    		 << "\n"                                    //Question to user
    		 << "Press \"2\" for Questions"             //Question to user
    		 << "\n"                                    //Question to user
    		 << "Press \"3\" for Verse Quotation"       //Question to user
    		 << "\n";                                   //Question to user
        cin  >> main_question; //input for main Question above
    		if (main_question == 1) //In case the user pressed one to get a bible quiz verse
    		{cout << "What Verse would you like to view??" //Question to the user
    			  << "\n"
    			  << "press \"1\" for Psalm 1:?";
        cin  >> bible_verse_chapter;                    //input for the question above to view
    			  if (bible_verse_chapter == 1)         //if cin = 1 then ask the following.
    			  {cout << "Press \"1\" for Psalm 1:1"; //question to user
        cin  >> psalm_chapter1verse1;                   //input to the above question
    			  if (psalm_chapter1verse1 == 1)        //if cin = 1 then output the following.
    			  {cout << "Psalm 1:1 - Blessed is the man that walketh not in the council of the ungodly" //verse asked for
    					<< "\n"
    					<< "nor standeth in the way of sinners nor sittith in the seat of the scornful";   //verse asked for
    			  }
    			  }
    		}
    		return 1;
    }
  6. #4
  7. No Profile Picture
    status unknown
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    262
    Rep Power
    12
    A quick search on Google for C++ random numbers threw up the following link, which might help you.

    http://www.robertjacobs.fsnet.co.uk/random.htm
  8. #5
  9. No Profile Picture
    status unknown
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    262
    Rep Power
    12
    Here's some sample code for a menu program. It's effectively a template that you can adapt for your own use. I appreciate that it's probably too advanced for you right now, but you might still find it useful in some way.


    Code:
    /*---------------------------------------------------------------------
     *
     *  Menu Demonstration Program
     *
     *  This is a simple menu program for demonstration purposes. It can 
     *  easily be adapted for your own simple menu driven program. The 
     *  code has been written to be portable across C++ compilers that 
     *  meet the ISO standard.
     *
     *  The demonstration program lists 5 menu options. Replace these 
     *  with your own list of options and rename the called functions
     *  accordingly. Replace the dummy code in the called functions with
     *  your own code. Add or remove options to suit your needs.
     *
     *  A dummy clear screen function is provided. Place your own compiler
     *  dependent screen clearing code or function call inside 
     *  clearScreen().
     *
     ------------------------------------------------------------------- */
    
    #include <iostream>
    #include <limits>
    
    // clearScreen()
    // Calls your compiler dependent clear screen code (optional)
    //
    void clearScreen()
    {
        // Place your compiler's screen clear code or function call here
    }
    
    // displayMenuHeader()
    // Displays the list of menu options. Change the option names to suit.
    //
    void displayMenuHeader()
    {
            std::cout << "\n***** PROGRAM MENU *****\n\n";
            std::cout << "[1] Option 1\n";
            std::cout << "[2] Option 2\n";
            std::cout << "[3] Option 3\n";
            std::cout << "[4] Option 4\n";
            std::cout << "[5] Option 5\n";
            std::cout << "[Q] Quit\n" << std::endl;
    }
    
    // option1()
    // Replace this function with the code to be run when option 1 is selected.
    //
    void option1()
    {
        // Dummy message
        std::cout << "Option 1 selected, press Enter to continue: " << std::flush;
        std::cin.get();
    
        // add Option 1 code here
    }
    
    // option2()
    // Replace this function with the code to be run when option 2 is selected.
    //
    void option2()
    {
        // Dummy message
        std::cout << "Option 2 selected, press Enter to continue: " << std::flush;
        std::cin.get();
    
        // add Option 2 code here
    }
    
    // option3()
    // Replace this function with the code to be run when option 3 is selected.
    //
    void option3()
    {
        // Dummy message
        std::cout << "Option 3 selected, press Enter to continue: " << std::flush;
        std::cin.get();
    
        // add Option 3 code here
    }
    
    // option4()
    // Replace this function with the code to be run when option 4 is selected.
    //
    void option4()
    {
        // Dummy message
        std::cout << "Option 4 selected, press Enter to continue: " << std::flush;
        std::cin.get();
    
        // add Option 4 code here
    }
    
    // option5()
    // Replace this function with the code to be run when option 5 is selected.
    //
    void option5()
    {
        // Dummy message
        std::cout << "Option 5 selected, press Enter to continue: " << std::flush;
        std::cin.get();
    
        // add Option 5 code here
    }
    
    // confirmQuit()
    // prompts for user confirmation to quit
    //
    bool confirmQuit()
    {
        bool quitProgram;
        char userConfirmation = '0';
        std::cout << "Are you sure you want to quit? (y/n): " << std::flush;
        while(userConfirmation=='0'){
            std::cin.clear();
            std::cin.ignore(std::numeric_limits<int>::max(),'\n');
            std::cin >> userConfirmation;
            switch(userConfirmation){
            case 'y':
            case 'Y':
                quitProgram = true;
                break;
            case 'n':
            case 'N':
                quitProgram = false;
                break;
            default:
                std::cout << "Invalid selection, please try again: " << std::flush;
                userConfirmation = '0';
                break;
            }
        }
        return quitProgram;
    }
    
    // doMenu()
    // Displays the menu options, gets user selection, calls the
    // corresponding function or quits the program
    //
    bool doMenu()
    {
        clearScreen();
        displayMenuHeader();
        std::cout << "Choose 1, 2, 3, 4, 5, or Q to quit: " << std::flush;
        bool repeatMenu = true;
        char menuSelection;
        std::cin >> menuSelection;
        switch (menuSelection) {
        case '1':
            option1();
            break;
        case '2':
            option2();
            break;
        case '3':
            option3();
            break;
        case '4':
            option4();
            break;
        case '5':
            option5();
            break;
        case 'q':
        case 'Q':
            if(confirmQuit()){
                repeatMenu = false;
            }
            break;
        default:
            std::cout << "Invalid selection, press Enter to continue: " << std::flush;
            std::cin.get();
            break;
        }
        std::cin.clear();
        std::cin.ignore(std::numeric_limits<int>::max(),'\n');
        return repeatMenu;
    }
    
    int main (){
        while(doMenu());
    }

IMN logo majestic logo threadwatch logo seochat tools logo