Thread: Text-based quiz game

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

    Text-based quiz game

    Hi,

    I'm wants to make a quiz game, where I have 2 2-dimentional arrays, 1 with questions and 1 with the answers. Where there are a number of questions and the user starts from question 1 and continues to the end, and then when he/she has answered all the questions the program would tell how many correct answers the user had.

    So I was wondering if anybody in here have some good tips that would be useful? Maybe about how to organize the code for/while loop. Anything?

    And then a question about the comparisment of the correct- and persons answer, I could use strcmp to compare them, but as far as I remember strcmp takes to const char variables, and the answer variable would not be const because I would like to use the same variable through the program, I this going to be a problem?

    And if you have anything else you want to warn me about, or tell your very welcome
    -//Marc Poulsen -//MipZhaP

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

  2. #2
    and the Hat of Clumsiness GanglyLamb's Avatar
    Join Date
    Oct 2002
    Location
    between photons and phonons
    Posts
    1,110
    I did something like a quiz game a while back (more then one year ago ). The way i organized the whole thing was that i putted questions and answers in one file, the file would be checked wether it was a correct file for my quiz app, so it wouldnt mistakenly load wrong information which would make the prog to crash...(you could do this with something like [headers] ie[Quizapp], [question] and [answer]...

    Anyway on top of that i had a feature to encode the question answers files so that a " smart" user wouldnt just go and look at the answers in the files...Of course you also need a decoder for that to decode the file while printing it to the screen and to work with all the date during the whole quiz.

    Then for
    And then a question about the comparisment of the correct- and persons answer, I could use strcmp to compare them, but as far as I remember strcmp takes to const char variables, and the answer variable would not be const because I would like to use the same variable through the program, I this going to be a problem?
    First of all no you can use strcmp to compare the user input and other data, but id suggesust using strcmpi ( )
    #include <string.h>
    int strcmpi(const char *s1, const char *s2);

    Description

    Compares one string to another, without case sensitivity.
    strcmpi performs an unsigned comparison of s1 to s2, without case sensitivity (same as stricmp
    --implemented as a macro).
    It returns a value (< 0, 0, or > 0) based on the result of comparing s1 (or part of it) to s2 (or part of it).
    - this function isnt case sensitive else somebody would fill in an answer like GANGLYLAMB while in your data it is ganglylamb thus counting the answer as wrong...)

    The way i did this first was just multiple answers-choice and you would get a score on how many wrong, right and not answered questions...afterwards i changed this into something like this=>

    The answers will exist of something like 3 seperate words, now the user can answer in a sentence like answer, the prog will now compare wether all the keywords are in the answer given by the user which are also in the keywords of the file...

    With the quiz i made any user was able to write/ edit etc his own quiz and save it on the HD in any subdirectory of the game...then on every startup of the quiz it would look for all quiz´s which were either in the same or any subdirectory of the quiz...

    I dont know wether i helped you out in any way but i surely gave you some good things to think about when you want to take it to another level and dont want to have it hard-coded (which is all answers and questions into your source code )

    Greets,

    Ganglylamb

    PS: sry if there are any typos, im on azerty and its already getting late.
    PS2: Supposing you started this thread like 40 HD formats ago i would´ve given you pieces of the source and much more detailed information on this...
    Last edited by GanglyLamb; 09-07-2004 at 03:07 PM.

  3. #3
    the Wizard
    Join Date
    Aug 2004
    Posts
    109
    Thx, but about the strcmpi function:
    When I try to compare the two, I get an compiler error because that my answer array isn't constant. Because as you wrote yourself the parameters have to be of const char *..
    If I make it const, I have another problem, because my array is 2-dimentional: answer[current_answer][number_of_characters], so how do I solve this problem then?
    Do I really have to but the answers in a file?
    -//Marc Poulsen -//MipZhaP

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

  4. #4
    and the Hat of Clumsiness GanglyLamb's Avatar
    Join Date
    Oct 2002
    Location
    between photons and phonons
    Posts
    1,110
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main( ){
    
    
    	char user_input[BUFSIZ];
    	char answer[]= "ganglylamb\n";
    	printf("quiz where answers and questions are hard-coded\n");
    	printf("this means that the answers and the questions are to be found inside the code\n\n");
                    printf("What is my name?\n\n");
    	
                    fgets(user_input,BUFSIZ,stdin);
    
    	if(strcmpi(answer,user_input)==0){
    		printf("Thats correct!!!\n");
    		}
    	else{
    		printf("No you are terrible wrong here.\n");
    		}
    
    	return 0;
    	}
    Well i dont know but keep it as simple as possible the above code should work perfect and no need to work with 2d arrays or whatsoever...+ if you would post the code that you alreday have so far and which is giving you problems you should post it ...
    (dont forget to use the code tags)

    Greets,

    Ganglylamb.
    Last edited by GanglyLamb; 09-08-2004 at 10:37 AM.

  5. #5
    the Wizard
    Join Date
    Aug 2004
    Posts
    109
    That's completely correct, but did you forget the fact that I have more than 1 answer so that I can't just make a normal array, I have to have a 2-dimentional, well unless that I would have an char variable for every answer, and I don't want that. Well I'll try to avoid it, let me say it that way..
    Or did I misunderstand what you wrote?
    Here's the example(array) from my code that makes up the trouble:

    Code:
     const char answer[MAXANSWERS][20] = 
     {
      "Bowling",
      "Memphis",
      "Danmarksstrædet",
      "Alderney",
      "Aorta",
      "Århus"
     };
    -//Marc Poulsen -//MipZhaP

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

  6. #6
    the Wizard
    Join Date
    Aug 2004
    Posts
    109
    Jimbob1989, I can see that you write a post, but not what you write.... Just so you know it... I can only see the half of your title...
    -//Marc Poulsen -//MipZhaP

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

  7. #7
    and the Hat of Clumsiness GanglyLamb's Avatar
    Join Date
    Oct 2002
    Location
    between photons and phonons
    Posts
    1,110
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main( ){
    
    	int num=0;
    	char user[BUFSIZ];
    	const char answer[][16] =
     {
      "Bowling\n",
      "Memphis\n",
      "Danmarksstrædet\n",
      "Alderney\n",
      "Aorta\n",
      "Århus\n",
      "end\n",
     };
    
    
    	do{
    
    		fgets(user,BUFSIZ,stdin);
    		if(strcmpi(user,answer[num])==0){
    			printf("correct\n");
    			}
    		else{
    			printf("wrong\n");
    			}
    
    	  num++;
    	  }
    	  while(strcmp(answer[num],"end\n")!=0);
    
    	  printf("End of quiz at least no more answers left :-d\n");
    
    	return 0;
    	}
    I think this should be enough for now...

    Greets,

    Ganglylamb.

    PS: sorry for any sloppy coding...dont have too much time to pretty-fi anything ...and sry for my previous post i was my mind was set on using a data file with all answers and questions in while at the same doing it the hard -coded way, can sound strange but ive got some strange braincells up here...
    Last edited by GanglyLamb; 09-08-2004 at 02:16 PM.

  8. #8
    the Wizard
    Join Date
    Aug 2004
    Posts
    109
    Okey, thx again.. It's okey, as long as were finding the solution, and I think we have
    -//Marc Poulsen -//MipZhaP

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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Text based game
    By wipeout4wh in forum C Programming
    Replies: 12
    Last Post: 03-26-2009, 04:39 PM
  2. Text based game
    By beene in forum Game Programming
    Replies: 10
    Last Post: 05-12-2008, 07:52 PM
  3. C Programming 2d Array Question
    By jeev2005 in forum C Programming
    Replies: 3
    Last Post: 04-26-2006, 03:18 PM
  4. easiest way to make text based game?
    By oceanrats in forum C++ Programming
    Replies: 7
    Last Post: 08-13-2005, 02:17 PM
  5. Saving a text game
    By Unregistered in forum C++ Programming
    Replies: 4
    Last Post: 03-27-2002, 01:33 PM