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
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!
 

garethlewis2

macrumors 6502
Dec 6, 2006
277
1
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.
 

iMasterWeb

macrumors regular
Original poster
Mar 15, 2009
158
0
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.