Thread: comparing an entered value to a char

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    2

    comparing an entered value to a char

    How do you check if a entered value is a char and then use recursion to reenter a value.
    Here is my code so far. Please help. any help would be appreciated!
    Thank you!

    I have been messing around with this for days and can't seem to figure it out. The char ch i just added as well as the if. I have been deleting adding. Checking. Its a nightmare.

    Also having pointers too =/... Again thankk you for any help.

    Code:
    /* Prompts for and inputs a fraction with input verification */
    void inputFraction(int *numerator, int *denominator)
    {	
    	char ch;
    	
    	printf("Please enter a fraction (a/b): ");
    	scanf("%d/%d", *&numerator, *&denominator);
    	
    	if ((numerator || denominator) == ch)
    	{
    		printf("Plese enter a valid fraction!");
    		inputFraction(numerator, denominator);
    	}
    }
    Last edited by hawaiian_girl; 10-21-2010 at 06:43 PM.

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    First read: << !! Posting Code? Read this First !! >>
    Then, checking for valid input is tricky and complex. But the general idea is to read a line using fgets or scanf (see SourceForge.net: Scanf woes - cpwiki), and then checking that the string is a digit and convert it.
    To check if it's a number, you can use isdigit. To convert it, you can use strtol.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    2

    confused

    ok so i read the fgets and have tried to input that into my code. i just don't know where to put it. and now i am super confused. heres what i have now and i know its wrong. if you could offer more guidiance i would appreciate it. ty

    Code:
    /* Prompts for and inputs a fraction with input verification */
    void inputFraction(int *numerator, int *denominator)
    {	
    	char ch;
    	char buffer[100];
    	
    	fgets(buffer, sizeof(buffer), stdin);
    
    	buffer[strlen(buffer) - 1] = '\0';
    	
    	printf("Please enter a fraction (a/b): ");
    	scanf("%d/%d", *&numerator, *&denominator);
    	
    	if ((numerator && denominator) == ch)
    	{
    		printf("Plese enter a valid fraction!");
    		inputFraction(numerator, denominator);
    	}
    }
    Last edited by hawaiian_girl; 10-21-2010 at 05:27 PM. Reason: trying to do code ettiquete

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    After using fgets, the easiest way to proceed would be to use sscanf to split it up. IIRC, it returns the number of "fields" correctly read. So if you extract two fields, it should return 2, or it failed. You should take a look at the documentation for sscanf.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. The UNIX System Interface
    By rrc55 in forum C Programming
    Replies: 1
    Last Post: 10-20-2009, 05:56 PM
  2. Replies: 12
    Last Post: 08-11-2008, 11:02 PM
  3. The Interactive Animation - my first released C program
    By ulillillia in forum A Brief History of Cprogramming.com
    Replies: 48
    Last Post: 05-10-2007, 02:25 AM
  4. lvalue error trying to copy between structures
    By emanresu in forum C Programming
    Replies: 2
    Last Post: 11-16-2006, 06:53 AM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 05:26 PM