Thread: QuizProgram

  1. #1
    the Wizard
    Join Date
    Aug 2004
    Posts
    109

    QuizProgram

    As the title refers to, I'm trying to make a quiz program, and I'm not getting any errors, but when I answer the first question (haven't made the right/wrong code yet) the program mysteriously writes a random number of questions, I can be from 1 to 14 times that it repeats a random question, and I can't find my "error".. So I hope that somebody has an idea.

    If I've forgotten anything just let me know

    Here's my code:

    Code:
    #include <iostream.h>
    #include <stdlib.h>
    #include <string.h>
     
    #define MAXQAS 10 //Max Questions and Answers
    
    //random number function..
    int rNum(int lowest_number, int highest_number) {
     int range = highest_number - lowest_number + 1;
     return lowest_number + int(range * rand()/(RAND_MAX + 1.0));
    }
    int main() {
     //Making an array with questions
     char questions[MAXQAS][256] = 
     {
      "I hvilken sport bruger man en bane, der er 19,2 m. lang og 1,06 m. bred?",
      "I hvilken by blev Martin Luther King myrdet?",
      "Hvad hedder farvandet mellem Island og Grønland?",
      "Hvad hedder den kanalø, der ligger tættest på Frankrig?",
      "Hvad hedder kroppens hovedpulsårer?",
      "I hvilken by ligger Danmarks første plakatmuseum?"
     };
     
     //Array with answers
     char answers[MAXQAS][50] = 
     {
      "Bowling",
      "Memphis",
      "Danmarksstrædet",
      "Alderney",
      "Aorta",
      "Århus"
     };
     
     //defining program variables
     char op = 'n';
     char ops;
    
     system("cls");
     
     while(op != 'exit') {
      //
      // Random select a number to decide question, and get question lenght..
      //
      int ranQuestion = rNum(0,5);
      int questionLenght = strlen(questions[ranQuestion]);
      //
      // Write the question out to the user
      //
      for(int i = 0;i<=questionLenght;i++) {
       cout << questions[ranQuestion][i];
      }
      cout << endl;
      cout << "Svaret: ";
      cin >> ops;
      cout << endl << endl;
     }
     return 0;
    }
    -//Marc Poulsen -//MipZhaP

    He sat down, he programmed, he got an error...

  2. #2
    ~viaxd() viaxd's Avatar
    Join Date
    Aug 2003
    Posts
    246
    Code:
    while(op != 'exit') {
    this is wrong, a char is only big enough to store a single character, like 'e' or 'q', not the whole word.
    :wq

  3. #3
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    This is specifically a C Programming board, but I notice you're posting a C++ program.
    If you understand what you're doing, you're not learning anything.

  4. #4
    the Wizard
    Join Date
    Aug 2004
    Posts
    109
    viaxd: Thx for your help.. I'll change it to something different then
    itsme86: Yes, your completely correct, but I accidently hit the wrong board without noticing.. Not on purpose..
    -//Marc Poulsen -//MipZhaP

    He sat down, he programmed, he got an error...

Popular pages Recent additions subscribe to a feed