Thread: char datatype?

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    266

    char datatype?

    why doesnt my program check to see if i entered Y or y at the end?
    it always goes to the end message, thanks

    Code:
    #include <stdio.h>
    int main() {
    int num1;
    int num2;
    char yorn;
    printf ("enter a basic number: ");
    scanf ("%d", &num1)  ;
    printf("now enter another number: ");
    scanf ("%d", &num2)  ;
    printf ("u entered %d and %d\n", num1, num2);
    printf ("-------\n");
    printf ("would you like to multiply? , Y/N\n");
    scanf ("%c", &yorn);
    getchar();
    if ( yorn == 'y' || yorn == 'Y' ){
    printf ("your numbers multiplied are %d", num1 * num2);}
    else          {
    printf ("ok then, thanks for using my basic program!");  }
    }
    Last edited by rodrigorules; 08-20-2005 at 11:53 AM.

  2. #2
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    I'm not entirely sure. I'm not a C guru, but I do notice it's a little odd to call getchar() right after calling scanf().

    You need to work on indentation too.
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  3. #3
    Registered User
    Join Date
    Aug 2005
    Posts
    266
    if i dont put the getchar();
    it prints the Y/N line and prints the "thanks for using" line as well

    (not letting me enter a Y or N)


    EDIT: thanks , ic what u meant..anyway i switched the
    getchar(); and the scanf on top of it with each other...and it worked fine.

  4. #4
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    Quote Originally Posted by rodrigorules
    if i dont put the getchar();
    it prints the Y/N line and prints the "thanks for using" line as well

    (not letting me enter a Y or N)


    EDIT: thanks , ic what u meant..anyway i switched the
    getchar(); and the scanf on top of it with each other...and it worked fine.
    No problem
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,662
    scanf in general is a poor way of reading input, and scanf with "%c" is particularly nasty.

    Most people use fgets() to read a line of input into a buffer, then use sscanf(), or some other string-to-x conversion functions to extract the information.
    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
    Aug 2005
    Posts
    266
    can u give me an example of what i would put in mine with sscanf and fget?

  7. #7
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    Quote Originally Posted by rodrigorules
    can u give me an example of what i would put in mine with sscanf and fget?
    Code:
    #include <stdio.h>
    #include <string.h>             /* for the call to strchr() */
    #include <stdlib.h>             /* for the call to exit() */
    
    int main(void)
    {
            enum {
                    BUF_MAX = 15
            };
    
            int num1;
            int num2;
            char yorn;
            char buffer[BUF_MAX] = { 0 };
            char *p;
    
            printf("Enter a basic number <ENTER>: ");
            fflush(stdout);
            if (fgets(buffer, sizeof(buffer), stdin) == NULL) {
                    perror("fgets()");
                    exit(EXIT_FAILURE);
            }
    
            if ((p = strchr(buffer, '\n')) != NULL) {
                    *p = '\0';
            }
    
            if ((sscanf(buffer, "%d", &num1)) != 1) {
                    puts("Error: call to sscanf() failed");
                    exit(EXIT_FAILURE);
            }
    
            printf("Now enter another number <ENTER>: ");
            fflush(stdout);
    	
    	 /* Get the user input into the array 'buffer' with
    	 * fgets() - if fgets() returns NULL, then there was
    	 * some sort of trouble, and we deal with it accordingly
    	 */
    
            if (fgets(buffer, sizeof(buffer), stdin) == NULL) {
                    perror("fgets()");
                    exit(EXIT_FAILURE);
            }
    
    	 /* if there is a newline stored at the end of the buffer
    	 * it most likely would be advantageous to eliminated it.
    	 * Search for a newline with strchr(), and if we find it,
    	 * replace it with a null terminator.
    	 */
    
            if ((p = strchr(buffer, '\n')) != NULL) {
                    *p = '\0';
            }
    
    	 /* sscanf() (and the other variants of scanf()) return a value
    	 * which corresponds to the number of conversions made.  We will 
    	 * check and see that, in this case, we get 1 (one) conversion
    	 * corresponding to the one int we are looking to get.  If we don't
    	 * get 1 (one), then there is a problem, and we deal with it as needed.
    	 */
    
            if ((sscanf(buffer, "%d", &num2)) != 1) {
                    puts("Error: call to sscanf() failed");
                    exit(EXIT_FAILURE);
            }
    
            printf("You entered %d and %d\n", num1, num2);
            printf("-------\n");
    
            printf("Would you like to multiply the two values? Y/N <ENTER>: ");
            fflush(stdout);
    
            yorn = getchar();
    
            if (yorn == 'y' || yorn == 'Y') {
                    printf("Your numbers multiplied are %d\n\n", num1 * num2);
            } else {
                    printf("Ok then, thanks for using my basic program!");
            }
            return 0;
    }
    Voila - don't say you never had anything done for you. Of course doing code like that could get big in a real hurry, so you might want to write your own 'getnum' function which gets an integer from the user, and returns it to be used as you wish.

    ~/
    Last edited by kermit; 08-20-2005 at 01:56 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  2. Sorting Linked Lists
    By DKING89 in forum C Programming
    Replies: 6
    Last Post: 04-09-2008, 07:36 AM
  3. Conversion Char To Char * Problem
    By ltanusaputra in forum Windows Programming
    Replies: 3
    Last Post: 03-01-2008, 02:06 PM
  4. I'm having a problem with data files.
    By OmniMirror in forum C Programming
    Replies: 4
    Last Post: 05-14-2003, 09:40 PM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM