Still a beginner and a first year student. I am trying to create a simple quiz game as a practice, but when I try to create a counter that serves as the remaining chances and it diminishes every time the user answers incorrectly. The problem is the loop does not exit when the lives counter reaches 0, rather it goes to a negative value and it does not exit the loop. I still don't know if I used the while loop correctly. I hope to get a simple answer. Thanks!
#include<stdio.h>
int main (void) {
//quiz variables
int gamestart,ingame=1;
int scr1,scr2,scr3,scr4,scr5;
int ans1,ans2,ans3,ans4,ans5;
char name[25];
int total=0;
//start of quiz
printf("--------Hello welcome to the quiz game!--------\n");
printf("----Press 1 to start the game and 0 to quit----\n");
scanf("%d",&gamestart);
//quiz code
int lives=3;
while (ingame==1 && lives>0 && lives<=3){
if (gamestart==1){
printf("Enter your first name: ");
scanf("%s",&name);
printf("Hello %s! Let's start the game...\n",name);
printf("First Question: What does “www” stand for in a website browser?\n");
printf("1. Whole Wide World 2. Whole Wide Web\n");
printf("3. World Wide Web 4. World Wide Whole\n");
printf("Enter the number of your answer: ");
scanf("%d",&ans1);
switch(ans1){
case 1:
printf("Wrong Answer. You gain 0 points");
scr1=0;
break;
case 2:
printf("Wrong Answer. You gain 0 points");
scr1=0;
break;
case 3:
printf("Correct Answer! You gain 5 points!");
scr1=5;
break;
default:
printf("Wrong Answer. You gain 0 points");
scr1=0;
break;
}
total += scr1;
if (scr1 ==0 )
lives--;
printf("\nLives left: %d",lives);
printf("\nYour total score this round is %d ",total);
printf("\n");
printf("\nNext Question...\n");
printf("What is \"cynophobia\"?\n");
printf("1. Fear of dogs 2. Fear of cats\n");
printf("3. Fear of cyanide 4. Fear of the color blue\n");
printf("Enter the number of your answer: ");
scanf("%d",&ans2);
switch(ans2){
case 1:
printf("Correct Answer! You gain 5 points!");
scr2=5;
break;
case 2:
printf("Wrong Answer. You gain 0 points");
scr2=0;
break;
case 3:
printf("Wrong Answer. You gain 0 points");
scr2=0;
break;
default:
printf("Wrong Answer. You gain 0 points");
scr2=0;
break;
}
total+=scr2;
if (scr2==0)
lives--;
printf("\nLives left: %d",lives);
printf("\nYour total score this round is %d ",total);
ingame=0;
} else if (gamestart == 0) {
ingame=0;
}else{
printf("Invalid Keyword\n");
printf("----Press 1 to start and 0 to quit----\n");
scanf("%d",&gamestart);
}
printf("\n\n\nThank you!");
return 0;
}
Firstly, I don't see the reason you are using all of these switch
statements, just use if
. Also format your code probably so we can read it.
Here is my take on your code:
#include <stdio.h>
int main(void)
{
// quiz variables
int gamestart, ingame = 1;
int total = 0;
int ans;
char name[25];
// start of quiz
printf("--------Hello welcome to the quiz game!--------\n");
printf("----Press 1 to start the game and 0 to quit----\n");
scanf("%d", &gamestart);
// quiz code
int lives = 3;
while (ingame == 1 && lives > 0)
{
if (gamestart == 1)
{
printf("Enter your first name: ");
scanf("%s", &name);
printf("Hello %s! Let's start the game...\n", name);
printf("First Question: What does “www” stand for in a website browser?\n");
printf("1. Whole Wide World 2. Whole Wide Web\n");
printf("3. World Wide Web 4. World Wide Whole\n");
printf("Enter the number of your answer: ");
scanf("%d", &ans);
if (ans == 3)
{
printf("Correct Answer! you gain 5 points!");
total += 5;
}
else
{
printf("Wrong Answer! You gain 0 points!");
lives--;
if (lives == 0)
{
printf("You lost");
break; // or do something else
}
}
printf("\nLives left: %d", lives);
printf("\nYour total score this round is %d ", total);
printf("\n");
printf("\nNext Question...\n");
printf("What is \"cynophobia\"?\n");
printf("1. Fear of dogs 2. Fear of cats\n");
printf("3. Fear of cyanide 4. Fear of the color blue\n");
printf("Enter the number of your answer: ");
scanf("%d", &ans);
if (ans == 1)
{
printf("Correct Answer! you gain 5 points!");
total += 5;
}
else
{
printf("Wrong Answer! You gain 0 points!");
lives--;
if (lives == 0)
{
printf("You lost");
break; // or do something else
}
}
printf("\nLives left: %d", lives);
printf("\nYour total score this round is %d ", total);
printf("\n");
printf("\nNext Question...\n");
printf("Who named the Pacific Ocean?\n");
printf("1. Ferdinand Magellan 2. Christopher Colombus\n");
printf("3. Miguel Lopez de Legazpi 4. Antonio Pigafetta\n");
printf("Enter the number of your answer: ");
scanf("%d", &ans);
if (ans == 1)
{
printf("Correct Answer! you gain 5 points!");
total += 5;
}
else
{
printf("Wrong Answer! You gain 0 points!");
lives--;
}
printf("\nLives left: %d", lives);
printf("\nYour total score this round is %d ", total);
printf("\n");
printf("\nNext Question...\n");
printf("What was the first soft drink in space?\n");
printf("1. Fanta 2. Pepsi\n");
printf("3. Coca-Cola 4. Dr. Pepper\n");
printf("Enter the number of your answer: ");
scanf("%d", &ans);
if (ans == 3)
{
printf("Correct Answer! you gain 5 points!");
total += 5;
}
else
{
printf("Wrong Answer! You gain 0 points!");
lives--;
if (lives == 0)
{
printf("You lost");
break; // or do something else
}
}
printf("\nLives left: %d", lives);
printf("\nYour total score this round is %d ", total);
printf("\n");
printf("\n");
printf("Next Question...");
printf("Which country invented ice cream?\n");
printf("1. USA 2. China \n");
printf("3. Spain 4. Turkey\n");
printf("Enter the number of your answer: ");
scanf("%d", &ans);
if (ans == 2)
{
printf("Correct Answer! you gain 5 points!");
total += 5;
}
else
{
printf("Wrong Answer! You gain 0 points!");
lives--;
if (lives == 0)
{
printf("You lost");
break; // or do something else
}
}
printf("\nLives left: %d", lives);
printf("\n");
printf("\n");
printf("\nYour total score in this game is %d \n", total);
switch (total)
{
case 0:
printf("Too bad!");
break;
case 5:
printf("Try harder!");
break;
case 10:
printf("Fair enough");
break;
case 15:
printf("Pretty good!");
break;
default:
printf("Excellent! Genius!");
break;
}
ingame = 0;
}
else if (gamestart == 0)
{
ingame = 0;
}
else
{
printf("Invalid Keyword");
printf("\n");
printf("----Press 1 to start the game and 0 to quit----\n");
scanf("%d", &gamestart);
}
}
printf("\n");
printf("\n");
printf("\nThank you!");
return 0;
}
I didn't add newlines. Also, I just did break when the live count is 0. You can do whatever you want with that.