can u guess my number?

This is a discussion on can u guess my number? within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> #include <stdlib.h> #include <string.h> int main() { int guess = -1; int num = 0; int answer; ...

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    16

    can u guess my number?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    #include <string.h>
    
    
    int main()
    {
    	int guess = -1;
    	int num = 0;
    	int answer;
    	int i;
    	unsigned seed;
    	
    	printf{"Enter seed:\n");
    	scanf("%d",&seed);
    
    	srand(seed)
    
    		for(i=1;i<=1000,i++); {
    
    	answer = rand() % 100 + 1;
    
    	while ( guess != answer ) {
    		num++;
    		printf("Enter guess %d: ",num);
    		
    		if ( guess < answer )
    			printf("Too high,try again\n");
    		if ( guess > answer ) 
    			printf("Too low,try again\n");
    	}
    	printf("Correct! you guessed the number!\n\n",num);
    	}
    	
    	return 0;
    }
    im trying to write a number guessing game in C, when trying to run this program i got the following error:

    expression evaluates to a function which is missing an argument list

    expression evaluates to a function which is missing an argument list
    what does that mean and how should i fix it?
    Last edited by strider496; 03-22-2005 at 03:09 PM.

  2. #2
    I like code Rouss's Avatar
    Join Date
    Apr 2004
    Posts
    131
    Which lines are the errors on?

    My guess would be the
    Code:
    unsigned seed
    unsigned what?
    Code:
    unsigned int seed //?? maybe

  3. #3
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,159
    unsigned seed is legitimate syntax. The compiler assumes int.

    The problem is likely here:
    Code:
    srand(seed)
    See anything missing? It looks like the missing piece fell down to the for() loop. You should take it from there and put it back on that srand() line.

    After that you'll have other problems to fix, but let's see if you can take it from here.
    If you understand what you're doing, you're not learning anything.

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Code:
    printf{"Enter seed:\n");
    And as pointed out...
    Code:
       scanf("%d",&seed);
    
       srand(seed);
    
       for ( i=1;i<=1000;i++ )/*;*/ {
    Last edited by Dave_Sinkula; 03-22-2005 at 03:29 PM.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    I like code Rouss's Avatar
    Join Date
    Apr 2004
    Posts
    131
    Quote Originally Posted by itsme86
    unsigned seed is legitimate syntax. The compiler assumes int.
    Ah, learn something new everyday... Can't believe I missed the semicolon though...

    Good show

  6. #6
    Registered User
    Join Date
    Mar 2005
    Posts
    16
    i fixed the syntax now it runs but infinite numbers and too high,try again scrolls down
    when i input 6 as seed

  7. #7
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Code:
       int guess = -1;
       /* ... */
          answer = rand() % 100 + 1;
          while ( guess != answer )
    Since answer is non negative, and guess is always -1, what would you expect?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  8. #8
    Registered User
    Join Date
    Mar 2005
    Posts
    16
    my revised code
    Code:
    #include <stdio.h>
     #include <stdlib.h>
     
     #include <string.h>
     
     
     int main()
     {
     	int guess;
     	int num = 0;
     	int answer;
     	int i;
     	unsigned seed;
     	
     	printf("Enter guess:\n");
     	scanf("%d",&guess);
     
     	srand(guess);
     
     		for(i=1;i<=1000;i++); {
     
     	answer = rand() % 100 + 1;
     
     	while ( guess != answer ) {
     		num++;
     		printf("Enter guess %d: ",num);
    		scanf("%d",&guess);
     		
     		if ( guess > answer )
     			printf("Too high,try again\n");
     		if ( guess < answer ) 
     			printf("Too low,try again\n");
     	}
     	printf("Correct! you guessed the number!\n\n",num);
     	}
     	
     	return 0;
     }
    i think i got the program to run correctly except

    the run result i got was

    Enter guess: /* want to eliminate this line */
    6
    Enter guess 1: 4
    Too low,try again
    Enter guess 2: 67
    Too high,try again
    Enter guess 3: 54
    Too low,try again
    Enter guess 4: 51
    Too low,try again
    Enter guess 5: 49
    Too low,try again
    Enter guess 6: 50
    Too low,try again
    Enter guess 7: 52
    Too low,try again
    Enter guess 8: 54
    Too low,try again
    Enter guess 9: 56
    Too low,try again
    Enter guess 10: 58
    Too low,try again
    Enter guess 11: 60
    Too high,try again
    Enter guess 12: 59
    Correct! you guessed the number!

    Press any key to continue

  9. #9
    Registered User
    Join Date
    Mar 2005
    Posts
    16
    if i want to revise my program so when the player guess the number in less than 10 tries i want to print out "you're good", do i just add
    Code:
    if (i=1;i <10;++i)
    {
    printf("you're good\n");
    }
    after the printf("Correct! you guessed the number!\n\n",num);
    Edit/Delete Message
    Last edited by strider496; 03-22-2005 at 05:15 PM.

  10. #10
    Registered User
    Join Date
    Mar 2005
    Posts
    140
    you already keep track of how many guesses in num
    no need for another loop.

    just test

    if(num < 10)

  11. #11
    Registered User
    Join Date
    Mar 2005
    Posts
    16
    thx guys

  12. #12
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,159
    This loop:
    Code:
    	for(i=1;i<=1000;i++); {
    ...does nothing for you. You could take that whole line out and its matching brace and you wouldn't even notice a difference in the way your program runs.

    Also, in this line of code:
    Code:
    printf("Correct! you guessed the number!\n\n",num);
    Why are you passing num to printf() if the format string doesn't have a place for it?

    EDIT: There's also a bug in your program where if the user types the correct random value at the first prompt, the main loop won't even run at all.
    If you understand what you're doing, you're not learning anything.

  13. #13
    Registered User
    Join Date
    Mar 2005
    Posts
    16
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #include <string.h>
    
    
    int main()
    {
    	int guess=-1;
    	int num = 0;
    	int answer;
    	int i;
    
    	srand((unsigned)time(NULL));
    
    	answer = (rand() % 99)+1;
    
        while ( guess != answer )
        {
    		num++;
    		printf("Enter guess %d: ",num);
            scanf("%d", &guess);
    
    		if ( guess > answer )
    			printf("Too high.Try again\n\n");
    
    		if ( guess < answer )
    			printf("Too low.Try again\n\n");
    
        }
    	printf("Excellent! you guessed the number Would you like to play again (y or n)\n\n",num);
    	
    	if (num < 10) 
    	{
    		printf("Either you know the secret or you got lucky\n");
    	}
    	 else if (num = 10)
    	{
    		printf("Ahah! You know the secret!\n");
    	}
    	else
    	{
    		printf("You should be able to do better\n");
    	}
    
    	system("pause");
    	return 0;
    }
    when i guessed the number in the 8th try for some reason it prints out both
    "Either you know the secret or you got lucky" and "Ahah! You know the secret!"

  14. #14
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,159
    Code:
    if (num = 10)
    Look closely at this. What operator are you using?
    If you understand what you're doing, you're not learning anything.

  15. #15
    customized user title!
    Join Date
    Mar 2005
    Posts
    24
    if(10 = num)
    If you do that, you will get an error at compile time. Which can save you from code that does unexpected things.

    If(num = 10)
    Will not give you a compile time error, which will not save you.

    That is why some people like to put the constant first when comparing.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 02-19-2009, 06:19 PM
  2. Issue w/ Guess My Number Program
    By mkylman in forum C++ Programming
    Replies: 5
    Last Post: 08-23-2007, 01:31 AM
  3. Random number + guessing game trouble
    By Ravens'sWrath in forum C Programming
    Replies: 16
    Last Post: 05-08-2007, 03:33 AM
  4. small problem
    By ucfmustang in forum C Programming
    Replies: 1
    Last Post: 02-10-2003, 03:55 PM
  5. Newton-Raphson number squaring method/pointers
    By inakappeh in forum C Programming
    Replies: 2
    Last Post: 08-29-2001, 11:04 AM

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