Hello,

I'm in an introductory computer programming course and I'm very new. I've been trying to write a program that will allow a user up to five tries to guess a randomly generated number. Here is what I have so far:

#include <stdio.h> //enables printf and scanf functions
#include <stdlib.h> //enables random number function

int main()
{
	//LOCAL DECLARATION SECTION.
	int random_number;
	int guess;

	//Setting the seed to generate a random number
	srand (313);

	//Getting the random number.
	random_number = rand () % ((20 - 1 + 1) + 1);

	[B]printf("\n\nPlease guess the number between 1 and 20: ");
	scanf("%d", &guess);
	
	
	if (guess != random_number)
	{
		printf("Sorry.  You have guessed the wrong number.");
		
	}
	 
		if (guess > random_number)
		{
			printf("\nYour guess is greater than the number.  Try again.");
		}
		else if (guess < random_number)
		{
			printf("\nYour guess is lower than the number.  Try again.");
		}

		else if (guess = random_number)
		{
			printf("\nCONGRATULATIONS!  You have guessed the correct number.");
		}[/B]

It's works fine so far but I don't know how to get the program to repeat itself up to five times until the user guesses the right number. The program is suppose to terminate if the user guesses the right number OR has guessed five times. We haven't learned anything about loops so is there a method in which to do this without using loops? The only thing I can think of right now is to copy the part in bold-face five times. However, it'll still continue to ask the user to guess the number even if he gets it before the fifth try. Any help would be appreciated.

Thanks,

William

Ancient Dragon commented: Thanks for taking the time to use code tags correctly :) +24

i think the only way you can do this is by looping...well that is wat i can only think of since you want the program to run atleast 5 times...it doesnt matter if you have covered the looping or not cuz ur lecturer requires you to do some research...i think a for loop would do

Yes, you need a loop and a counter to count how many time your loop executes. Exit when counter == 5 OR guess == random_number

Also be careful using = and ==
They are not the same and you have to use the right one for the right purpose.

Is there a way to accomplish this using nested "if" statements?

Perhaps. But why not take the good advice from WaltP. That would be the easiest solution by far.

Niek

why are u sticking so much to using if statements?? you will come across the loop sonner or later if you are trying to avoid it...try studying it..it shouldnt be hard to get the concepts and once you have got them your problem should be a piece of cake...the if is just complicating your life

Also, here is a piece of logical truth. Given

if (a < 10)
    do this...
else if (a > 10)
    do this...
else
    when you reach here, isn't a == 10 automatically? No need to test it.

I'm not trying to avoid learning loops. I stated in my first post that we haven't covered it yet. We'll learn that next week. The reason why I'm sticking with "if" statements is because the assignment specifies using them. But I agree, loops would make this so much easier. Unfortunately, we are only suppose to use the knowledge we have covered thus far in class. Anyways, thanks for the help.

Then you need one if for each guess. They don't have to be nested, either. Each if simply tests the guess with the number and if they aren't the same, do what you need to do.

Within each if, get another number, test it for > and < and output the appropriate message. No need to even consider equal until all the ifs are finished.

As you can see, it helps to give us all the information so we don't waste your time giving you advice you can't use.

Thanks. I will try to be more specific next time, but I'm glad I got through. Programming just isn't my thing.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.