major .. serious .. C problem plz HELP!

This is a discussion on major .. serious .. C problem plz HELP! within the C Programming forums, part of the General Programming Boards category; please someone could test this program with c and tell me what the error is in this program, the way ...

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    2

    major .. serious .. C problem plz HELP!

    please someone could test this program with c and tell me what the error is in this program, the way i see it is that it excutes the if and the else at the same time. I won't be long here's the program ....

    Code:
    #include<stdio.h>
    main()
    {
    	float guess, incr;
    	char ch;
    	printf("Think of a number between 1 and 99, and\n");
    	printf("I'll guess what it is. type 'e'' for equal , 'g' for greater and 'l' for less than\n");
    	incr = guess = 50;
    	while (incr > 1.0)
    	{
    		printf("\nIs your number greater than or less than %.0f?\n",guess);
    		scanf("%c",&ch);
    		incr = incr / 2;
    	    if (ch == 'g' )
    			guess = guess + incr;
    		else 
    			guess = guess - incr;
    	}
    	printf("\n The number is %.0f. Am I not clever? \n",guess);
    }
    One last note i tried the getch() function of course with the conio.c but it didn't make much diffrence so try it too, it might work with you

  2. #2
    * noops's Avatar
    Join Date
    Jun 2008
    Posts
    108
    scanf is reading stdin which actually has two characters e, g, or l and '\n' for when the user presses enter.

  3. #3
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Errors:
    1) You should have
    Code:
    if (ch == 'g')
    ....
    else if (ch == 'l')
    ....
    else
       break;
    so you end the while-loop when it is equal.

    2) You use float for guess and incr. Are you sure you don't want int? I think you do want int, because you are guessing an integer from 1 to 99 not a number with decimal(sp?). It would work just fine with int by the way.

    3) What do you mean it executes the if and the else the same time? That is impossible in whatever way you mean it. And what is the problem exactly. I guess you don't get the correct result? Generally specify what is your problem to get more to the point

  4. #4
    Registered User
    Join Date
    Mar 2006
    Posts
    150
    Quote Originally Posted by C_ntua View Post
    Errors:
    2) You use float for guess and incr. Are you sure you don't want int? I think you do want int, because you are guessing an integer from 1 to 99 not a number with decimal(sp?). It would work just fine with int by the way.
    No, int wont work, because incr is 50 initially, and in the loop it gets divided by two until the number is right, in other words:

    first time through loop:

    incr = 50

    second time:

    incr = 50 / 2 or 25

    third time:

    incr = 25 / 2 or 12.5

    if you use int, the .5 will be discarded. It may not cause a problem, but then again, it might at some point

  5. #5
    * noops's Avatar
    Join Date
    Jun 2008
    Posts
    108
    I'll expand a little bit because I am learning C currently and I want to make sure I understand it:

    when a user types something and hits enter all of those characters + \n go to a queue (stdin). scanf("%c", &ch) will read one character from stdin if there is a character there, otherwise it will wait until stdin is filled with some data. So each call to scanf will only eat one character.

    The original poster will want scanf in a loop and ignore bad characters.

    You can test this idea by entering
    Code:
    lllll
    at the first prompt.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 05:24 PM
  2. plz help me with run problem
    By onlinegeek in forum C++ Programming
    Replies: 9
    Last Post: 12-14-2005, 10:46 PM
  3. Replies: 5
    Last Post: 11-07-2005, 10:34 PM
  4. compiling problem! plz help!
    By aaroroge in forum C++ Programming
    Replies: 9
    Last Post: 10-24-2005, 09:57 PM
  5. searching problem
    By DaMenge in forum C Programming
    Replies: 9
    Last Post: 09-12-2005, 01:04 AM

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