Thread: I am comfortable with Java Programming but C... I need help

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    178

    I am comfortable with Java Programming but C... I need help

    Well I am stumped. Must be scanf but tried fgets and no dice.

    See what you can do with this.

    The program runs nicely through until ready to do it again and then the program loops forever.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    int
    main(void){
    	int decision = 121, guessedNum;
    	while(decision == 121){
    		srand( time(NULL));
    		int target = rand() % 100 + 1, count = 0;
    		while (guessedNum != target){
    			printf("Guess a number between 1 and 100: ");
    			scanf("%d", &guessedNum);
    			fflush(stdin);
    			if (guessedNum > target){
    				printf("The number is lower. Try again.\n");
    				count += 1;
    			}
    			else if(guessedNum < target){
    				printf("The number is higher. Try again.\n");
    				count += 1;
    			}
    			else
    			{
    				printf("You guessed correctly!\n");
    				printf("It took you %d tries.\n", count);
    			}
    		}
    		printf("Would you like to play again? y(es) or n(o) :");
    		scanf("%d", &decision);
    	}
    
    	return 0;
    }

  2. #2
    Registered User Swarvy's Avatar
    Join Date
    Apr 2008
    Location
    United Kingdom
    Posts
    195
    What exactly are you typing when it asks you to play again? Are you typing a 'y' or 'Y', because, if so, that is your problem. Your code asks for a number and if that number is 121 then it will loop around again.

    So, when it gets to this line of code:
    Code:
    printf("Would you like to play again? y(es) or n(o) :");
    Try typing '121' and seeing what happens. Does it loop properly?

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You are trying to read decision as an int with scanf, but you expect 'y' or 'n' as input. That does not make sense. Use getchar() instead of scanf, and then compare with 'y', not the magic number 121.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    178
    Well my frustration has come to an end. Oddly enough I never thought to use getchar and the endless looping has stooped. Thank you for the suggestions.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > fflush(stdin);
    Ewww - unclean!

    There's a FAQ - time to read it.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    178
    Well I am still unsure as to how to get one character typed at the kb into my program for comparison to a simple while loop.

    Using getchar does not work or scanf or anything else.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What is your current code?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    May 2010
    Posts
    178
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    int
    main(void){
    	int decision = 'y', guessedNum;
    	while(decision == 'y'){
    		srand( time(NULL));
    		int target = rand() % 100 + 1, count = 0;
    		while (guessedNum != target){
    			printf("Guess a number between 1 and 100: ");
    			scanf("%d", &guessedNum);
    			fflush(stdin);
    			if (guessedNum > target){
    				printf("The number is lower. Try again.\n");
    				count += 1;
    			}
    			else if(guessedNum < target){
    				printf("The number is higher. Try again.\n");
    				count += 1;
    			}
    			else
    			{
    				printf("You guessed correctly!\n");
    				printf("It took you %d tries.\n", count);
    			}
    		}
    		printf("Would you like to play again? y(es) or n(o): ");
    		decision = fgetc(stdin);
    	}
    
    	return 0;
    }

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    It works for me, though I would change:
    Code:
    fflush(stdin);
    to:
    Code:
    getchar();
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User
    Join Date
    May 2010
    Posts
    178
    If I remove fflush(stdin) and use decision = getchar() no matter what I do the program ends after a successful guess.

    What give with this language. How is that getchar() doesn't even stop to actually get a char from stdin??

  11. #11
    Registered User Swarvy's Avatar
    Join Date
    Apr 2008
    Location
    United Kingdom
    Posts
    195
    Not:
    Code:
    decision = getchar();
    Just:
    Code:
    getchar();
    Don't assign decision to the output of that function.

  12. #12
    Registered User
    Join Date
    May 2010
    Posts
    178
    But whole point is to choose if you wish to run the program again or not. I tested getchar in another project independently and it performs as intended. Why it does not in Eclipse on a MacBook Pro is beyond my understanding. The program should stop and get input ad that it is not doing so makes no sense.

  13. #13
    Registered User Swarvy's Avatar
    Join Date
    Apr 2008
    Location
    United Kingdom
    Posts
    195
    You only call the function 'fflush' once during your program and it isn't at the end of the while loop when you are testing to see if the user wants to replay.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    int main(void){
    	int decision = 'y', guessedNum;
    	while(decision == 'y'){
    		srand( time(NULL));
    		int target = rand() % 100 + 1, count = 0;
    		while (guessedNum != target){
    			printf("Guess a number between 1 and 100: ");
    			scanf("%d", &guessedNum);
    			fflush(stdin);
    			if (guessedNum > target){
    				printf("The number is lower. Try again.\n");
    				count += 1;
    			}
    			else if(guessedNum < target){
    				printf("The number is higher. Try again.\n");
    				count += 1;
    			}
    			else
    			{
    				printf("You guessed correctly!\n");
    				printf("It took you %d tries.\n", count);
    			}
    		}
    		printf("Would you like to play again? y(es) or n(o): ");
    		decision = fgetc(stdin);
    	}
    	
    	return 0;
    }
    Change that fflush to:
    Code:
    getchar();
    Everything else is fine.

  14. #14
    Registered User
    Join Date
    May 2010
    Posts
    178
    I fixed it and it works perfectly now.
    decision - getchar()
    decision = getchar()

    The first line eats a new line after the successful entry of the correct guess. The second line actually prompts for and gets the character from stdin.

    Library for stdio is crap in this crappy language and only thing worth anything is fgets but if the stupid buffer wont flush itself or reset or return to 0 and holds onto new lines and EOF then crap.

  15. #15
    Registered User Swarvy's Avatar
    Join Date
    Apr 2008
    Location
    United Kingdom
    Posts
    195
    Trust me on this one, C isn't a crappy language it is just very unforgiving - you have to be very precise with your code. Java allows a little more flexibility because it is run through a virtual machine.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C#, Java, C++
    By incognito in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 10-05-2004, 02:06 PM
  2. First Java Class at college
    By GanglyLamb in forum A Brief History of Cprogramming.com
    Replies: 24
    Last Post: 09-29-2004, 10:38 PM
  3. The Java language is being expanded
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 26
    Last Post: 06-11-2004, 09:07 PM
  4. Java woes
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 07-06-2003, 12:37 AM
  5. Visual J#
    By mfc2themax in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 10-08-2001, 02:41 PM