Like Tree1Likes
  • 1 Post By iMalc

how do i make this loop???

This is a discussion on how do i make this loop??? within the C Programming forums, part of the General Programming Boards category; Code: main() { int x; int r; unsigned NULL; printf("I have a number between 1 and 10.\n"); printf("Can you guess ...

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    22

    how do i make this loop???

    Code:
    main()
    {
    int x;
    int r;
    unsigned NULL;
    printf("I have a number between 1 and 10.\n");
    printf("Can you guess my number?\n");
    printf("Please type your first guess.\n");
    scanf("%d", &x);
    srand( time(NULL) );
    r = 1 + ( rand() % 10 );
    if ( r == x ){
    	printf("Excellent! You guessed the number!\n");
    	printf("Would you like to play again (y or n)?\n");
    }
    else
    while ( r != x ){
    	if ( r > x ){
    	printf("Too low. Try Again.\n");
    }
    	if ( r < x ){
    	printf("Too high. Try Again. \n");
    }
    		printf("Please type your second guess.\n");
    		scanf("%d", &x);
    }
    	printf("Excellent! You guessed the number!\n");
    	printf("Would you like to play again (y or n)?\n");
    }
    trying to design a game that allows user to guess a computer generated number but once the user is correct i don't know how to loop back to play again. Any suggestions on how to create this loop? thanks

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Cprogramming.com Tutorial: Loops

    Everything that you want to repeat needs to be inside the loop.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Apr 2011
    Posts
    22
    i know where the loop needs to be my question was more along the lines of how do i create the loop while (?) and how do i use the user's respons (y or n) to return a true or false for the loop?

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    So since you weren't sure how to make your loop work, you thought a tutorial on loops wouldn't be useful at all, is that it?

    Also:
    Code:
    unsigned NULL;
    What is it you are trying to do here?


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Apr 2011
    Posts
    22
    how should i define NULL in srand( time(NULL) );? i thought unsigned was the correct way?

    Also i just went through the tutorial on loops and it didn't tell me anything about changing a user's response "y" or "n" into a value that can be used in repeating a loop. if you could help me with this i was appreciate it.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You aren't reading Y/N input from them. You print out "want to play again", but you never actually check to see what they type. You have to actually read what they type if you want to do something with it. You aren't doing that.

    NULL is already defined (in stdio.h or stddef.h). It's not a variable, it's a value. It's like if I ask you what 500 is. You say it's a number, I say ok, you don't need to make a variable called 500 to see if something equals 500, you just check to see if something is 500.


    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,304
    Keep in mind that you must not have this part inside the loop:
    Code:
    srand( time(NULL) );
    quzah likes this.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  8. #8
    Registered User
    Join Date
    Apr 2011
    Posts
    22
    yeah i am unsure how to recieve their response, is it possible with a scanf? if so, how do i store their response? scanf("%d?

    thank you i was getting an undefined symbol message but i put the stdio.h header and it went away

  9. #9
    Registered User
    Join Date
    Apr 2011
    Posts
    22

    Talking

    Code:
    #include<stdio.h>
    
    main()
    {
    int x;
    int r;
    char response;
    srand( time(NULL) );
    do{
    printf("I have a number between 1 and 10.\n");
    printf("Can you guess my number?\n");
    printf("Please type your first guess.\n");
    scanf("%d", &x);
    r = 1 + ( rand() % 10 );
    if ( r == x ){
    	printf("Excellent! You guessed the number!\n");
    	printf("Would you like to play again (y or n)?\n");
    }
    else
    while ( r != x ){
    	if ( r > x ){
    	printf("Too low. Try Again.\n");
    }
    	if ( r < x ){
    	printf("Too high. Try Again. \n");
    }
    		printf("Please type another guess.\n");
    		scanf("%d", &x);
    }
    printf("Excellent! You guessed the number!\n");
    printf("Would you like to play again (y or n)?: \n");
    scanf("  %c", &response);
    }while ( response == 'y' );}
    finally figured it out thanks quizah couldnt have done it without ur help

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Histogram function
    By davidjg in forum C Programming
    Replies: 14
    Last Post: 11-23-2010, 11:21 PM
  2. Turning off While loop in realtime
    By baikal_m in forum C Programming
    Replies: 13
    Last Post: 04-22-2010, 01:56 PM
  3. Can a "switch" be inside a loop?
    By gmk0351 in forum C Programming
    Replies: 5
    Last Post: 03-28-2008, 05:47 PM
  4. loop needed also how to make input use letters
    By LoRdHSV1991 in forum C Programming
    Replies: 3
    Last Post: 01-13-2006, 04:39 AM
  5. I need help as soon as possible.
    By hyrule in forum C++ Programming
    Replies: 7
    Last Post: 11-09-2005, 04:49 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21