1. ## Random question simulator

Ok, I'm fairly new to programming and I have been working on one for my mother for her 4th grade class. They are learning Texas history and i just have an assortment of questions about that. Also, is the random number simulator used in the same way a random question simulator would be used? anyways here is my program so far:
Code:
```#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<math.h>

int compute (int x,int y,char r);
int t3();

int main ()
{
char z;
int a,e,f,i;

srand(time(NULL));

printf("Hello, this is a standardized history test for 4th graders\n");
printf("press 1 to begin. Press any key to end.\n");
scanf("%d",&a);
while(a=1)

rand=t3();
if(i==1)
{
printf("Davy Crockett lost his life at the _______\n");
scanf("%d",&e);
printf("1-Shreveport, Louisiana\n2-The Battle of the Alamo\n3-The Battle of San Jacinto\n4-The Seige of Bexar\n");
if(e==2)
printf("That's right!\n");
else
printf("Nope, try again\n");

}
if(i==2)
{
printf("How many flags have flown over Texas?\n");
scanf("%d",&e);
printf("1-two\n2-three\n3-four\n4-five\n5-six\n");
if(e==5)
printf("Good job!\n);
else
printf("Sorry, that's wrong\n);
}
if(i==3)
{
printf("What is David Bowie famous for?\n");
scanf("%d",&e);
printf("1-Killing Colonel Travis\n2-The Bowie knife\n3-Leader of the mexican army\n4-
}
if(i==4)
{
printf("Who led the Battle of Conception?\n");
scanf("%d",&e);
printf("1-David Bowie\n2-Davey Crockett\n3-Colonel William Travis\n4-Sam Houston\n);
if(e==1)
printf("That's right!\n);
else
printf("No, that is wrong);
}
if(i==5)
{
printf("What kind of terrain would you find in the Trans-Pecos Region?\n");
scanf("%d",&e);
printf("1-grasslands\n2-mountains\n3-desert\n4-forestry\n");
if(e==3)
printf("That's correct\n");
else
printf("Nope, I'm sorry.\n");
}
if(i==6)
{
printf("Where is the region known as the Piney Woods located?\n");
scanf("%d",&e);
printf("1-North Texas\n2-South Texas\n3-West Texas\n4-East Texas\n");
if(e==4)
printf("Yay, you got that one!\n");
else
printf("Oops, you missed that one!\n");
}
if(i==7)
{
printf("Which river borders Oklahoma and Texas\n");
scanf("%d",&e);
printf("1-The Mississippi River\n2-The Red River\n3-The Rio Grande\n4-The Nile\n");
if(e==2)
printf("That's right!\n");
else
printf("You missed that one!\n");
}
if(i==8)
{
printf("Which river borders Texas and Mexico?\n");
scanf("%d",&e);
printf("1-The Mississippi River\n2-The Red River\n3-The Rio Grande\n4-The Nile\n");
if(e==3)
printf("Right!\n");
else
}
return 0;```
I have about 30 more of those questions but i deleted them to save some space. What i also wanted to add was something that would add up all the missed and correct problems and tell you your percentage at the end. Would something like this work?:
Code:
```if (ans= = correct)
right=right+1;

else
wrong=wrong+1;```
I have a few more questions but that is the main jist of it. Thanks everyone.

2. That is coded in C, this is a C++ board.
Also try to indent your code, makes reading and debugging it much easier.

What i also wanted to add was something that would add up all the missed and correct problems and tell you your percentage at the end. Would something like this work?
Yes your example would surfice as a way to acomplish that. But as you are only adding
by 1 each time, increment the sum instead of doing it the long way.

eg:
Code:
```// add number by 1
int num = 5; // original value
num++; // add 1 to it by increment```
You could also put a loop round all the code that asks the question again if it is wrong, and
will only increment the "correct" value if it is right, thus changing the question. I suggest
using a for loop

3. I may just be missing it, but I don't see where i is ever initialized for all this if (i == 1) if (i== 2) etc.

plus you may want to look into creating a file with all the questions answers etc. in it.
this way 1 for loop could handle all the reading, questioning etc. and it would be easier to modify and add questions

4. Originally Posted by sl4nted
I may just be missing it, but I don't see where i is ever initialized for all this if (i == 1) if (i== 2) etc.

plus you may want to look into creating a file with all the questions answers etc. in it.
this way 1 for loop could handle all the reading, questioning etc. and it would be easier to modify and add questions
How exactly would i do that?

5. you would have a file setup as follows:

q=1
question
choices
q=2
question
choices
q=3
question
choices

or any format like that as long as it is consistant.

you could then loop though

print question
print choices
if correct increase score

6. I was board so wrote a little program as an example for you.

Code:
```#include <stdio.h>
#include <stdlib.h>

#define BUFFSIZE 256

void flush_input()
{
int   ch;

while ((ch = getchar()) != '\n' && ch != EOF);
}

int main(int argc, char **argv)
{
FILE *fp;
char buffer[BUFFSIZE];
int correct = 0;
int total = 0;
int i = 0;

if ((fp = fopen("questions.txt", "r")) == NULL)
{
perror("Unable to open file");
exit(1);
}

while (!feof(fp))
{
fgets(buffer, BUFFSIZE, fp);

switch (i)
{
/* question number case */
case 0: i++; break;     /* can add functionality here if wanted */

case 1: printf("Question: %s", buffer); i++; break;

case 2: printf("Choose one: %s", buffer);
printf("> ");
flush_input();
i++;
break;

case 3: if (buffer[0] == answer)    /* could also use strcmp and check a full answer */
{
printf("Correct!\n");
correct++;
}
else printf("Incorrect\n");

total++;
i = 0;
}
}

fclose(fp);

printf("Your Score: %d out of %d\n", correct, total);
printf("%f percent\n", ((float)correct / (float)total) * 100);
return 0;
}```
File contents:
q=1
This is question 1
c
q=2
This is question 2
a) another a, b) another b, c) another c
b
q=3
Final question
a) choice 1, b) choice 2, c) choice 3
a

Output:

Question: This is question 1
> c
Correct!
Question: This is question 2
Choose one: a) another a, b) another b, c) another c
> a
Incorrect
Question: Final question
Choose one: a) choice 1, b) choice 2, c) choice 3
> a
Correct!
Your Score: 2 out of 3
66.666667 percent

so basically you can just keep adding questions to the file and the program doesn't need to change

7. Originally Posted by sl4nted
I was board so wrote a little program as an example for you.

Code:
```#include <stdio.h>
#include <stdlib.h>

#define BUFFSIZE 256

void flush_input()
{
int   ch;

while ((ch = getchar()) != '\n' && ch != EOF);
}

int main(int argc, char **argv)
{
FILE *fp;
char buffer[BUFFSIZE];
int correct = 0;
int total = 0;
int i = 0;

if ((fp = fopen("questions.txt", "r")) == NULL)
{
perror("Unable to open file");
exit(1);
}

while (!feof(fp))
{
fgets(buffer, BUFFSIZE, fp);

switch (i)
{
/* question number case */
case 0: i++; break;     /* can add functionality here if wanted */

case 1: printf("Question: %s", buffer); i++; break;

case 2: printf("Choose one: %s", buffer);
printf("> ");
flush_input();
i++;
break;

case 3: if (buffer[0] == answer)    /* could also use strcmp and check a full answer */
{
printf("Correct!\n");
correct++;
}
else printf("Incorrect\n");

total++;
i = 0;
}
}

fclose(fp);

printf("Your Score: %d out of %d\n", correct, total);
printf("%f percent\n", ((float)correct / (float)total) * 100);
return 0;
}```
File contents:
q=1
This is question 1
c
q=2
This is question 2
a) another a, b) another b, c) another c
b
q=3
Final question
a) choice 1, b) choice 2, c) choice 3
a

Output:

Question: This is question 1
> c
Correct!
Question: This is question 2
Choose one: a) another a, b) another b, c) another c
> a
Incorrect
Question: Final question
Choose one: a) choice 1, b) choice 2, c) choice 3
> a
Correct!
Your Score: 2 out of 3
66.666667 percent

so basically you can just keep adding questions to the file and the program doesn't need to change
Wow, that is awesome. But would you mind explaining what the void thing does? I am unfamiliar with that.

8. void just means it doesn't return anything
the code inside flushes the input buffer, its actually pretty much straight out of the FAQ. you can go and read up on it if you like.

9. > while (!feof(fp))
> fgets(buffer, BUFFSIZE, fp);
Why you shouldn't do this is in the FAQ.

while ( fgets(buffer, BUFFSIZE, fp) != NULL )
Achieves the same thing, and does the right thing when you get to the end of the file.

10. You need to make your program more "general" at any time you want to allow your code to be added upon in a moments notice. You can use giant "if" statements with a chance that without a i>9 ending statement that could not loop back up to the question without restarting the program. I would recomend you use a Switch, It will allow you to test conditionals keep them in a loop (if you dont type break and run through every statement without the user from asking to be questioned again. Within each Case I would have a function. I would continue with a persudo code but I have learned my lession from that past, the best way to help someone is to allow them to code it themselfs so they can become more concrete in there work, not copy pase in others. Good luck homie