By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,960 Members | 990 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,960 IT Pros & Developers. It's quick & easy.

quiz game in c

P: 18
hi!
I'm trying to read lines from a text file line by line and store it in an array. the txt file has some question in it which will be asked to the player !

Expand|Select|Wrap|Line Numbers
  1. 1: När kom potatisen till Europa?;A:1300-talet; B:1500-talet; C:900-talet;D:1700-talet\n
  2. rätt svar : B
  3.  
  4. 2: I vilken enhet mats elektrisk spänning ?;A:Ampere;B:Volt;C:Joule;D:Watt\n
  5. Rätt svar: A
  6.  
  7. 3: Från vilket land har vi fått lego?;A:Tyskland;B:Australien;C:Japan;D:Danmark\n
  8. rätt svar : D




I want it to produce the following output:




Expand|Select|Wrap|Line Numbers
  1. 1: När kom potatisen till Europa?
  2. A:1300-talet
  3.  B:1500-talet 
  4. C:900-talet
  5. D:1700-talet
When the user chooses it moves on to the next question ! it's a quiz i'm kinda of new to the language !



here is the code but i run into errors !! idk why!


Expand|Select|Wrap|Line Numbers
  1. #pragma warning(disable:4996)
  2. #include<stdio.h>
  3. #include<stdlib.h>
  4. #include<string.h>
  5.  
  6.  
  7.  
  8.  
  9. struct quiz
  10. {
  11.     char questions[50];
  12.     char* alt[4];
  13.     char correctanswer[1];
  14. };
  15.  
  16. int main(){
  17.  
  18.     struct quiz all_ques[10];
  19.     int i = 0;
  20.  
  21.     FILE *haidar;
  22.     haidar = fopen("gameee.txt", "r");
  23.     char str[500];
  24.     char *ptr;
  25.  
  26.     while (fgets(str, 500, haidar))               // read 500 characters
  27.     {
  28.         ptr = strtok(str, ";");           // split our findings around the " "
  29.         strcpy(all_ques[i].questions, ptr);    // store the question
  30.  
  31.         ptr = strtok(NULL, ";");            // and keep splitting
  32.         all_ques[i].alt[0] = malloc(10);
  33.         strcpy(all_ques[i].alt[0], ptr);    // store the first option
  34.  
  35.         ptr = strtok(NULL, ";");       // and keep splitting
  36.         all_ques[i].alt[1] = malloc(10);
  37.         strcpy(all_ques[i].alt[1], ptr);    // store the second option
  38.  
  39.         ptr = strtok(NULL, ";");       // and keep splitting
  40.         all_ques[i].alt[2] = malloc(10);
  41.         strcpy(all_ques[i].alt[2], ptr);    // store the third option
  42.  
  43.         ptr = strtok(NULL, ";");       // and keep splitting
  44.         all_ques[i].alt[3] = malloc(10);
  45.         strcpy(all_ques[i].alt[3], ptr);    // store the fourth option
  46.  
  47.         fgets(str, 500, haidar);
  48.         strcpy(all_ques[i].correctanswer, str);    // store the correct answer
  49.  
  50.         i++;
  51.     }
  52. }
Dec 2 '15 #1
Share this Question
Share on Google+
1 Reply


weaknessforcats
Expert Mod 5K+
P: 9,197
Try this:

Expand|Select|Wrap|Line Numbers
  1. int main()
  2. {
  3.     char str[] = "1: När kom potatisen till Europa ? ; A:1300 - talet; B:1500 - talet; C:900 - talet; D:1700 - talet\n"
  4.     "rätt svar : B";
  5.  
  6.     int size = strlen(str);
  7.     int i = 0;
  8.     while (i < size)
  9.     {
  10.         if (str[i] != ';') 
  11.         {
  12.             cout << str[i];
  13.         }
  14.         else
  15.         {
  16.             cout << '\n';
  17.         }
  18.         ++i;
  19.  
  20.     }
  21.  
  22.     cout << '\n';
  23.  
  24. }
The logic is to display character-by-character until you see a ;. Don't display the ; but display a \n instead.

If you reach the end of the string display a final \n.
Dec 2 '15 #2

Post your reply

Sign in to post your reply or Sign up for a free account.