PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, August 12, 2022

[FIXED] How do i add an "(if)" that allows only letters between a-f || A-F?

 August 12, 2022     c, char, decimal, hex, scanf     No comments   

Issue

I wrote a program that does conversion from hex to decimal. all I got left is to check if the char is between a-f or A-F, maybe 0-9 as well. if it is not between them it will print "Illegal input".

My code:

int n, i;
char currentDigit;
unsigned long int sum, currentDigitInt;

printf("Enter the number of digits in the Hexadecimal number:");
scanf_s("%d", &n);
sum = 0;

printf("Enter the Hexadecimal number:\n");
for (i = n - 1; i >= 0; i--) {
    scanf_s(" %c", &currentDigit);

    if (currentDigit >= 'a') {
        currentDigitInt = (currentDigit - 'a') + 10;
    }
    else if (currentDigit >= 'A') {
        currentDigitInt = (currentDigit - 'A') + 10;
    }
    else
        currentDigitInt = currentDigit - '0';
    sum += currentDigitInt * pow(16, i);
}

printf("The decimal number is: %u", sum);

The output I need:

Enter the number of digits in the Hexadecimal number: 2
Enter the Hexadecimal number: QQ
Illegal input

Solution

There are multiple problems in your code:

  • you can test character ranges with the && operator. For example:

    if (currentDigit >= 'a' && currentDigit <= 'f')
    
  • you combine these tests in a series of if / else and complain if none of the tests match.

  • note also that the expression for sum is incorrect too. sum += currentDigitInt * pow(16, i); should be;

    sum = sum * 16 + currentDigitInt;
    
  • the printf format for unsigned long is %lu, not %u.

  • you should test the return value of scanf_s to properly detect a potential end of file.

  • scanf_s expects 2 extra arguments for conversion specifier %c and may not be available on all systems.

  • note too that you do not need to ask for the number of hex digits, just break from the loop when the character entered is a newline for the second time.

Here is a corrected version:

#include <stdio.h>

int main() {
    int currentDigit;
    unsigned long int sum;
    int invalid = 0;

    printf("Enter the Hexadecimal number: ");
    sum = 0;
    while ((currentDigit = getchar()) ! EOF && currentDigit != '\n') {
        int currentDigitInt;
        if (currentDigit >= 'a' && currentDigit <= 'f') {
            currentDigitInt = (currentDigit - 'a') + 10;
        } else
        if (currentDigit >= 'A' && currentDigit <= 'F') {
            currentDigitInt = (currentDigit - 'A') + 10;
        } else
        if (currentDigit >= '0' && currentDigit <= '9') {
            currentDigitInt = currentDigit - '0';
        } else {
            invalid = 1;
            continue;     // keep scanning the input until end of line
        }
        sum = sum * 16 + currentDigitInt;
    }
    if (invalid) {
        printf("Invalid input\n");
    } else {
        printf("The decimal number is: %lu\n", sum);
    }
    return 0;
}

Notes:

  • The C standard does guarantee that 0 through 9 are consecutive but purists will argue that the letters a through f and A through F might not be consecutive the execution character set. While the are correct, obscuring newbie programmers with these considerations is counter productive and quite excessive as these character ranges are consecutive in both ASCII and EBCDIC (the gaps in EBCDIC are between i and j and between r and s in both cases).


Answered By - chqrlie
Answer Checked By - Marilyn (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing