Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

iMasterWeb

macrumors regular
Original poster
Mar 15, 2009
158
0
Here is my code:

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.
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
return will stop execution of the current function immediately and return a value.
exit will send the particular value to the shell, and terminate immediately.

You have these statements in multiple places. Once you hit one of these statements (return in main, or exit anywhere) your program is done.

Look into this and see if you can change things so execution continues as you would like it to.

-Lee
 

iMasterWeb

macrumors regular
Original poster
Mar 15, 2009
158
0
Thanks

Thank you! I took them all, except the last ones, out and it works perfectly. Thanks!!
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.