I'm trying to create a simple menu interface for my newly created program. After the user chooses option 2, how do I save his/her's name and include a return option to the main menu? After that, how can I include the name in the main program? The same goes for option 3.
I have included both the main code and the menu code below. Any advice and help would be greatly appreciated.
What I have tried:
The menu code:
#include <stdio.h>
int main()
{
int i1=1;
while (i1 != 0)
{
printf(" ********************** Welcome! *********************\n");
printf(" * Maths Quiz *\n");
printf(" * 1.Enter Quiz *\n");
printf(" * 2.Enter Name *\n");
printf(" * 3.Instructions *\n");
printf(" * 4.Quit *\n");
printf(" *****************************************************\n");
printf("Please choose one from 1-4:\n");
scanf("%d", &i1);
switch (i1)
{
case 1:
printf("Enter Quiz:\n");
i1 = 0;
break;
case 2:
printf("Enter Name:\n");
i1 = 0;
break;
case 3:
printf("Instructions:\n");
i1 = 0;
break;
case 4:
printf("Quit:\n");
i1 = 0;
break;
}
}
return 0;
}
The main code:
#include <limits.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define MAX_TESTS 10
bool myread(const char* format, void* address)
{
char buffer[1024];
fgets(buffer, sizeof buffer, stdin);
return sscanf(buffer, format, address) == 1;
}
struct struc {
int a;
int b;
int c;
int add;
int grade;
};
int sj(int n)
{
int t;
t = rand() % n;
return t;
}
void ctm_i(struct struc* t)
{
t->a = sj(101);
t->c = sj(4);
if (t->c == 1) {
t->b = sj(101 - (t->a));
t->add = (t->a) + (t->b);
}
else {
t->b = sj((t->a) + 1);
t->add = (t->a) - (t->b);
}
t->grade = 0;
}
void tcm_i(struct struc* t, int n)
{
int ad;
printf(
" ***********************************************************************"
"*********\n");
printf(
" ......................................................................."
".........\n");
printf(" Question %d\n\n", n + 1);
printf(" You have 3 attempts for this question\n\n");
if (t->c == 1)
printf(" %d+%d= ", t->a, t->b);
else
printf(" %d-%d= ", t->a, t->b);
myread("%d", &ad);
if (ad == t->add)
{
t->grade = 10;
printf("\n Very good, you earned 10 marks\n\n");
}
else {
printf("\n Incorrect, you have 2 attempts remaining\n\n");
myread("%d", &ad);
if (ad == t->add)
{
t->grade = 7;
printf("\n Good, you earned 7 marks\n\n");
}
else {
printf("\n Incorrect, you have 1 attempt remaining\n\n");
myread("%d", &ad);
if (ad == t->add)
{
t->grade = 5;
printf("\n Not bad, you earned 5 marks\n\n");
}
else {
t->grade = 0;
printf("\n Failure, 0 mark\n\n");
printf("\n The correct answer is %d\n\n", t->add);
}
}
}
printf(
" ......................................................................."
".........\n");
printf(
" ***********************************************************************"
"*********\n");
}
int main()
{
int rounds = 0;
int highest = 0;
int lowest = INT_MAX;
float allScore = 0;
float avg = 0;
int i, j, g = 0;
struct struc test[MAX_TESTS];
srand((unsigned)time(NULL));
printf(
" ***********************************************************************"
"************\n");
printf(
" ......................................................................."
"............\n");
printf(
" ************************************* Welcome! ************************************\n");
printf(" ......................This program is for students Grades 1-2......................\n");
printf("\n Program description:\n");
printf(
"(1)10 questions are randomly generated, each question is worth 10 "
"points;;\n");
printf(
"(2)Only addition and subtraction within 100 is allowed. The sum and the "
"difference of the two numbers do not exceed the range of 0-100, negative "
"numbers are not included.\n");
printf("(3)There are 3 attempts for each question.;\n");
printf(
"(4)For each question, 10 points will be awarded for the first "
"successful attempt, 7 points for the second attempt, and 5 points for "
"the third attempt.\n");
printf(
"(5)At the end of the program, enter 'P' to start a new game, 'S' to show the results, and 'Q' to quit.;\n\n");
printf(
" ......................................................................."
".........\n");
for (;;) {
rounds++;
printf(" ***********************************************************************"
"*********\n");
for (i = 0; i < MAX_TESTS; i++)
{
ctm_i(&test[i]);
for (j = 0; j < i; j++)
if (test[i].a == test[j].a && test[i].b == test[j].b && test[i].c == test[j].c)
ctm_i(&test[i]);
}
printf(" Are you ready? Please press any key to continue: ");
int c;
while ( (c = getchar()) != '\n' && c != EOF ) { }
for (i = 1; i <= 5; i++) {
printf(
" *******************************************************************"
"**"
"***********\n");
printf(
" ..................................................................."
".."
"...........\n");
}
for (i = 0; i < MAX_TESTS; i++)
tcm_i(&test[i], i);
printf(" End\n\n");
bool done = false;
bool unsure = true;
bool showS = true;
while (unsure) {
unsure = false;
puts("\n");
if (showS) {
puts(" Enter 'S' to show results");
}
puts(" Enter 'P' to play another round");
puts(" Enter 'Q' to quit");
char choice;
myread("%c", &choice);
if (choice == 'Q' || choice == 'q')
done = true;
else if (choice == 'S' || choice == 's') {
showS = false;
g = 0;
for (i = 0; i < MAX_TESTS; i++) {
g += test[i].grade;
}
allScore += g;
avg = allScore / rounds;
if (g > highest)
{
highest = g;
}
if (g < lowest)
{
lowest = g;
}
if (rounds == 1)
{
printf(" Final score: %d/100\n", g);
}
else {
printf(" Round %d score: %d/100\n", rounds, g);
printf(" Highest score: %d/100\n", highest);
printf(" Lowest score: %d/100\n", lowest);
printf(" Average score: %f\n", avg);
}
unsure = true;
}
else if (choice == 'P' || choice == 'p') {
}
else {
puts(" Invalid input!");
unsure = true;
}
}
if (done)
break;
}
}