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, 02:10 PM   #1
iMasterWeb
macrumors regular
 
Join Date: Mar 2009
Trying to define variable based on user input. (C)

Ok, I am a begginer so I'm sorry if this is an obvious answer. Here's my code:
Code:
#include <stdio.h>
#include <string.h>
#include <math.h>

		printf("We are going to play a guessing game, but first I need you to choose a level: easy, intermediate, or hard.\n");
	int rand;
	char level[20];
	fgets(level, 20, stdin);
		if(strcmp(level, "easy") == 0) {
			printf("Here is how to play:\nSince you picked the easy level, I will pick a random number between 1 and 100, 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");
			srandom(time(NULL));
			rand = random() % 100;
			rand = rand + 1;
			}
			if(strcmp(level, "intermediate") == 0) {
			printf("Here is how to play:\nSince you picked the intermediate level, I 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");
			srandom(time(NULL));
			rand = random() % 1000;
			rand = rand + 1;
			}
			if(strcmp(level, "hard") == 0) {
			printf("Here is how to play:\nSince you picked the hard level, I will pick a random number between 1 and 10000, 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");
			srandom(time(NULL));
			rand = random() % 10000;
			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);
}
This is the part of my code I'm having trouble with. I want integer rand to be seeded out of 100, 1000, or 10,000 based on what level the user chooses. The problem is that it isn't recognizing the user input. In other words, I type something hit enter and nothing happens, I hit enter again and it just says that I won when I didn't guess a number. Does that make sense? If anyone can help me, I'd be very grateful. Thanks!

Last edited by iMasterWeb; Jun 21, 2009 at 02:19 PM.
iMasterWeb is offline   0 Reply With Quote
Old Jun 21, 2009, 04:17 PM   #2
garethlewis2
macrumors 6502
 
Join Date: Dec 2006
I'm not even going to ask how you compiled that monstrosity without declaring the main method.

You need the line.

level[strlen(level)-1] = '\0';

After you read the line with fgets. Your string variable level contains a \n character.
garethlewis2 is offline   0 Reply With Quote
Old Jun 21, 2009, 04:25 PM   #3
iMasterWeb
Thread Starter
macrumors regular
 
Join Date: Mar 2009
Thanks

Ok, I forgot about that. And I did declare the main method, that's at the beginning. This is just the bottom third of the code.
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
Apple Acquires 18 'Axis-Based User Interface' Patents from Maya-Systems MacRumors MacRumors.com News Discussion 75 Feb 9, 2014 03:20 PM
Define An "Average Joe" Smartphone User SomeDudeAsking iPhone 37 Mar 4, 2013 04:06 PM
How to save a location with user input name chan2004 iPhone and iPod touch Apps 0 Dec 14, 2012 01:12 AM
Best lap-based input mouse/keyboard solution? bluegrass527 Mac Peripherals 1 Nov 12, 2012 06:07 AM
iMaps improvement via user input patent10021 iOS 6 44 Sep 23, 2012 01:36 PM

Forum Jump

All times are GMT -5. The time now is 06:06 AM.

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

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