//Name: Gem Nikki S. Carpio
//Problem 1 : Program accepts values that are not hex (ie, E-Z, symbols)
//Fix: Escape the while loop using a flag variable for values not 0-9, a-z or A-Z
//Problem 2: Starting at the second input, the decimal value is wrong
//Fix: Reset the total
#include <stdio.h>
#include <stdlib.h>
int main(){
int value;
int flag = 0;
char character;
int total = 0;
while (1)
{
character = getchar();
while(character != '\n'){
if((character >= 'A') && (character <= 'F'))
value = character - 'A' + 10;
if((character >= 'a') && (character <= 'f'))
value = character - 'a' + 10;
if((character >= '0') && (character <= '9'))
value = character - '0';
if(((character > '9') && (character < 'A'))||((character > 'F') && (character < 'a'))||character > 'f')
{flag=1;
break;} //turns on flag for inputs not hex
total = total * 16 + value;
character = getchar();
}
if (flag!=1)
printf("%d\n", total); //it only prints if input is valid
else
{printf("Invalid input");
break;};
total = 0; //reset the total for next input
}
system("pause");
return 0;
}