If function - or condition with character input

This is a discussion on If function - or condition with character input within the C Programming forums, part of the General Programming Boards category; Hello, I'm currently attempting to make a simple check which instantly reads a character which is inputted and acts upon ...

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

    If function - or condition with character input

    Hello, I'm currently attempting to make a simple check which instantly reads a character which is inputted and acts upon it. The character is either "y" or "Y". And this is essentially for a very basic do-while loop for the full main function. Here's the code I'm using:

    Character Check Function:
    Code:
    int goAgain(void) {
    	char check;
    	printf ("Would you like to go again? [Y/y to redo]: ");
    	scanf ("%c",&check);
    	if ((check == 'Y') || (check == 'y')) { system("CLS"); return 1; }
    	else return 0;
    }
    Main Function:
    Code:
    void main (void) {
    	do {
    	srand(time(NULL));
    	populateArray();
    	printf ("Minimum value in array is %d",findMinimum());
    	findValue();
    	} while (goAgain() == 1);
    	getch();
    }
    Any other details you may have noticed that might help my program are more than welcome =]

    Lupo.

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Other than being badly formatted, it looks like your code should work... so what's the problem?

  3. #3
    Registered User
    Join Date
    Apr 2011
    Posts
    5
    Quote Originally Posted by CommonTater View Post
    Other than being badly formatted, it looks like your code should work... so what's the problem?
    When debugging, the scanf statement appears to be just skimmed straight past it. I honestly ain't sure what went wrong with it. I understand I over-compress my code once I'm "done" with it, but even so, as you mentioned it should work. Having set breakpoints from the "Enter Y/y to redo" till the if condition (including scanf) it skipped straight past the scanf() and didn't actually ask me to enter anything. Apologize for not stating this beforehand, I thought I just did some silly mistake which would have been very obvious

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Ahhh, that... scanf when looking for characters will take the first character in the buffer. Sometimes that will be a leftover newline (\n) from something else. The easy fix is to call getchar() right before it...
    Code:
    getchar();
    scanf(....
    Turns out this one isn't quite so obvious...

  5. #5
    Registered User
    Join Date
    Apr 2011
    Posts
    5
    Quote Originally Posted by CommonTater View Post
    Ahhh, that... scanf when looking for characters will take the first character in the buffer. Sometimes that will be a leftover newline (\n) from something else. The easy fix is to call getchar() right before it...

    Turns out this one isn't quite so obvious...
    Thanks for the reply, I inputted a getch(); above it, and once again, debugging the program and going through step by step the program pauses for me to input a char for getch() and then once again skips straight past scanf(). I also didn't quite understand the reference to the buffer, but I'm assuming that's going to be a bit above my "level of clearance"

    Edit: Having changed getch() to getchar() I noticed the program actually did work this time round. Had no idea they were different, wondering if I could get some explanation to that if it isn't too much of a hassle
    Last edited by Luponius; 04-17-2011 at 04:18 PM.

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Well, you have to learn to do this sometime... Look them up in your C library documentation.
    Half of a programmer's skill is knowing how to looks stuff up...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another character input problem
    By walkingbeard in forum C Programming
    Replies: 3
    Last Post: 11-26-2009, 10:32 AM
  2. Multiple Character Input.
    By mintsmike in forum C Programming
    Replies: 2
    Last Post: 03-23-2009, 01:15 AM
  3. get and check input as while condition
    By Marksman in forum C++ Programming
    Replies: 7
    Last Post: 09-29-2006, 09:22 PM
  4. Help with a condition function
    By swanley007 in forum C++ Programming
    Replies: 5
    Last Post: 02-24-2006, 01:20 PM
  5. putting condition on input
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 09-18-2001, 07:05 AM

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