Question: I'm currently working on this project for my C Programming class (NOT C++): Hexadecimal numerals are integers written in base 16. The 16 digits used

I'm currently working on this project for my C Programming class (NOT C++): Hexadecimal numerals are integers written in base 16. The 16 digits used are 0 through 9 plus a for digit 10, b for the digit 11, c for the digit 12,d for the digit 13, e for the digit 14, and f for the digit 15. For example, the hexadecimal numeral is same as base 10 numeral 0-9 and the hexadecimal 1d is same the base 10 numeral 29. Write a program to perform addition of two hexadecimal numerals each up to 10 digits. If the result of the addition is more than 10 digits long, then simply give the output message Addition Overflow and not the result of addition. Use arrays to store hexadecimal numerals as arrays of characters. Include a loop to repeat this calculation for new numbers until the user she or he wants to end the program.

This is my program so far:

#include

#include

#include

#include

int main()

{

//Constant value that can be used to set an array size

const int size = 10;

//Creating more variables to use throughout the program for user input and calculations

char choice;

double decimal = 0.0,counter;

int hex;

//Creating an array with a constant size of 10

//char ar[size];

//Creating a for loop that will loop through adding of the numbers until the value is larger than 10 (the array size)

for(counter = 0; counter< size; counter++)

{

//Asks user for a hexadecimal number

printf("Enter your Hexadecimal number");

//Scans in the user's input

scanf("%d",&hex);

//Sets the counter value as hex (the user's input)

counter = hex;

//Asks user if they want to see the addition of the hexadecimal as a decimal

printf("Do you want to show the addition of hexadecimal number in decimal (Y/N) :");

//Scans in the user's input of Y or N

scanf("%c",&choice);

//If the user's input is Y or y then it works through the if statement

if(choice == 'y' || choice=='Y')

{

//If the counter is equal to 0 then the hexadecimal is equal to 0, therefore it will go through the if statement to calculate the value of the hex in decimal

if(counter == 0)

{

decimal = (counter* (pow(16,0)));

printf("The decimal equivalent of your hexadecimal is %lf", decimal);

}

//If the counter is equal to 1 then the hexadecimal is equal to 1, therefore it will go through the if statement to calculate the value of the hex in decimal

else if(counter == 1)

{

decimal = ((counter * pow(16,2)))+(counter * (pow(16,0)));

printf("The decimal equivalent of your hexadecimal is %lf", decimal);

}

//If the counter is equal to 2 then the hexadecimal is equal to 2, therefore it will go through the if statement to calculate the value of the hex in decimal

else if(counter == 2)

{

decimal = ((counter * (pow(16,2)))+ (counter*(pow(16,2)))+(counter*(pow(16,0))));

printf("The decimal equivalent of your hexadecimal is %lf", decimal);

}

//If the counter is equal to 3 then the hexadecimal is equal to 3, therefore it will go through the if statement to calculate the value of the hex in decimal

else if(counter == 3)

{

decimal = ((counter * (pow(16,3)))+ (counter*(pow(16,2)))+(counter*(pow(16,1)))+ (counter * (pow(16,0))));

printf("The decimal equivalent of your hexadecimal is %lf", decimal);

}

//If the counter is equal to 4 then the hexadecimal is equal to 4, therefore it will go through the if statement to calculate the value of the hex in decimal

else if(counter == 4)

{

decimal = ((counter * (pow(16,4)))+ (counter*(pow(16,3)))+(counter*(pow(16,2)))+ (counter * (pow(16,1)))+(counter * (pow(16,0))));

printf("The decimal equivalent of your hexadecimal is %lf", decimal);

}

//If the counter is equal to 5 then the hexadecimal is equal to 5, therefore it will go through the if statement to calculate the value of the hex in decimal

else if(counter == 5)

{

decimal = ((counter * (pow(16,5)))+ (counter*(pow(16,4)))+(counter*(pow(16,3)))+ (counter * (pow(16,2)))+(counter * (pow(16,1)))+(counter * (pow(16,0))));

printf("The decimal equivalent of your hexadecimal is %lf", decimal);

}

//If the counter is equal to 6 then the hexadecimal is equal to 6, therefore it will go through the if statement to calculate the value of the hex in decimal

else if(counter == 6)

{

decimal = ((counter * (pow(16,6)))+ (counter*(pow(16,5)))+(counter*(pow(16,4)))+ (counter * (pow(16,3)))+(counter * (pow(16,2)))+(counter * (pow(16,1)))+ (counter * (pow(16,0))));

printf("The decimal equivalent of your hexadecimal is %lf", decimal);

}

//If the counter is equal to 7 then the hexadecimal is equal to 7, therefore it will go through the if statement to calculate the value of the hex in decimal

else if(counter == 7)

{

decimal = ((counter * (pow(16,7)))+ (counter*(pow(16,6)))+(counter*(pow(16,5)))+ (counter * (pow(16,4)))+(counter * (pow(16,3)))+(counter * (pow(16,2)))+ (counter * (pow(16,1)))+ (counter * (pow(16,0))));

printf("The decimal equivalent of your hexadecimal is %lf", decimal);

}

//If the counter is equal to 8 then the hexadecimal is equal to 8, therefore it will go through the if statement to calculate the value of the hex in decimal

else if(counter == 8)

{

decimal = ((counter * (pow(16,8)))+ (counter*(pow(16,7)))+(counter*(pow(16,6)))+ (counter * (pow(16,5)))+(counter * (pow(16,4)))+(counter * (pow(16,3)))+ (counter * (pow(16,2)))+ (counter * (pow(16,1)))+ (counter * (pow(16,0))));

printf("The decimal equivalent of your hexadecimal is %lf", decimal);

}

//If the counter is equal to 9 then the hexadecimal is equal to 9, therefore it will go through the if statement to calculate the value of the hex in decimal

else if(counter == 9)

{

decimal = ((counter * (pow(16,9)))+ (counter*(pow(16,8)))+(counter*(pow(16,7)))+ (counter * (pow(16,6)))+(counter * (pow(16,5)))+(counter * (pow(16,4)))+ (counter * (pow(16,3)))+ (counter * (pow(16,2)))+ (counter * (pow(16,1)))+ (counter * (pow(16,0))));

printf("The decimal equivalent of your hexadecimal is %lf", decimal);

}

//If the counter is equal to 10 then it will consider it as addition overflow and tell the user of the predictiment and break out of the loop.

else if (counter == 10)

{

printf("Addition overflow");

break;

}

}

else if (choice != 'y' || choice != 'Y')

{

printf(" in Hexa-Decimal=");

scanf("%lf", counter);

printf(" in Decimal=");

scanf("%lf", decimal);

break;

}

}

return 0;

}

My problem right now is that it only goes throught the loop once and not completely through either. For some reason right after it asks the user the Y/N question it ends, doesn't let the user input anything. Which also means it doesn't go through all the if statements which is crucial! Please any help would be so so great, thank you!

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!