Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Jun 21, 2009, 12:58 PM   #1
iMasterWeb
macrumors regular
 
Join Date: Mar 2009
Why isn't this working? (C)

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.
iMasterWeb is offline   0 Reply With Quote
Old Jun 21, 2009, 01:11 PM   #2
lee1210
macrumors 68040
 
lee1210's Avatar
 
Join Date: Jan 2005
Location: 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
lee1210 is offline   0 Reply With Quote
Old Jun 21, 2009, 01:16 PM   #3
iMasterWeb
Thread Starter
macrumors regular
 
Join Date: Mar 2009
Thanks

Thank you! I took them all, except the last ones, out and it works perfectly. Thanks!!
iMasterWeb is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
iMessage isn't working christinarule iPhone Tips, Help and Troubleshooting 3 Jul 16, 2014 04:37 PM
Why isn't this command working iroh Mac Applications and Mac App Store 6 Dec 26, 2013 08:25 AM
AirDrop isn't working on my 5s AT ALL. viperGTS iPhone 11 Oct 27, 2013 02:11 PM
Please help. ML isn't working right for me karbowaboshots OS X 1 Feb 12, 2013 05:31 PM
MyRepoSpace.com isn't working? FileTran Jailbreaks and iOS Hacks 0 Jul 19, 2012 12:00 PM

Forum Jump

All times are GMT -5. The time now is 02:27 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC