Here is my code:
What I want it to do is after you finish either the game or the calculator, go back to the "menu" at the beginning and loop until you enter "exit". I thought that the do..while loop would work but it didn't. Any ideas on what I could do? I am new to programming so forgive me if it is an obvious answer.
Code:
#include <stdio.h>
#include <string.h>
#include <math.h>
int main (int argc, const char * argv[]) {
// insert code here...
char name[512];
printf("Please type your name here: ");
fgets(name, 512, stdin);
if (name[strlen(name) - 1] == '\n') {
name[strlen(name) - 1] = 0;
}
int feature;
feature = 0;
printf("Welcome, %s\n\n", name);
do {
printf("What would you like to do now?\ncalculator \ngame \nexit \n");
do {
char response[20];
fgets(response, 20, stdin);
if (response[strlen(response) - 1] == '\n') {
response[strlen(response) - 1] = 0;
}
if (strcmp(response, "calculator") == 0)
feature = 1;
if(strcmp(response, "game") == 0)
feature = 2;
if(strcmp(response, "exit") == 0)
feature = 3;
if(feature == 1) {
printf("Launching calculator... \n");
}
if(feature == 2) {
printf("Starting game... \n");
}
if(feature == 3) {
printf("Goodbye! \n");
}
if(feature == 0) {
printf("Please enter calculator, game, or exit.\n");
}
} while(feature == 0);
if(feature == 1) {
printf("Would you like to add, subtract, multiply, or divide?\n");
char input[10];
int operation;
operation = 0;
do {
fgets(input, 10, stdin);
if (input[strlen(input) - 1] == '\n')
input[strlen(input) - 1] = 0;
if (strcmp(input, "add") == 0)
operation = 1;
if (strcmp(input, "subtract") == 0)
operation = 2;
if (strcmp(input, "multiply") == 0)
operation = 3;
if (strcmp(input, "divide") == 0)
operation = 4;
if (operation == 0) {
printf("Please enter add, subtract, multiply, or divide to make it work.\n");
}
} while(operation == 0);
int one;
int two;
char ones[10];
char twos[10];
printf("Please enter your first integer: ");
fgets(ones, 10, stdin);
printf("Please enter your second integer: ");
fgets(twos, 10, stdin);
if ((one = atoi(ones))) {
} else {
printf("The first thing you typed was not a number");
exit(0);
}
if ((two = atoi(twos))) {
} else {
printf("The second thing you typed was not a number");
exit(0);
}
int number;
if(operation == 1)
number = one + two;
if(operation == 2)
number = one - two;
if(operation == 3)
number = one * two;
if(operation == 4)
number = one / two;
printf("The answer is %d.\n", number);
return 0;
exit(0);
}
if(feature == 2) {
printf("We are going to play a guessing game. Here is how to play:\nI will pick a random number between 1 and 1000, and all you have to do is guess that number. Now since I am feeling generous I will tell you if your guess is too high or too low. Of course, you could always cheat by entering cheat, but you wouldn't cheat, would you? Now let's begin; make a guess:\n");
int rand;
srandom(time(NULL));
rand = random() % 1000;
rand = rand + 1;
int guess;
int guesses;
guesses = 0;
for(guess = -1; guess != rand; guesses++) {
char guessinput[20];
fgets(guessinput, 20, stdin);
if(strcmp(guessinput, "cheat\n") == 0) {
printf("My number is %d\n", rand);
guesses--;
}
if(!(guess = atoi(guessinput))) {
printf("Please guess a number.\n");
} else {
if(guess > rand)
printf("Too high\n");
else if(guess < rand)
printf("Too low\n");
}
}
if(guesses == 1) {
printf("Congratulations! You Won!\nYou guessed my number in %d guess.\n", guesses);
} else {
printf("Congratulations! You Won!\nYou guessed my number in %d guesses.\n", guesses);
}
return 0;
exit(0);
}
} while(feature != 3);
return 0;
exit 0;
}
What I want it to do is after you finish either the game or the calculator, go back to the "menu" at the beginning and loop until you enter "exit". I thought that the do..while loop would work but it didn't. Any ideas on what I could do? I am new to programming so forgive me if it is an obvious answer.