I have a conceptually easy task however I am running into all kinds of problems. The idea is to have a C program give an menu option to the user and then repeatedly ask for strings that will be converted. (i.e. Hex to Dec). I really don't care about the conversion portion right now I am just trying to get over the inputting of strings hurdle. Here are the current problems:
1. Upon running the code the initial selection is read properly however the input buffer or something is causing the fgets() to read that same input and process once before repeating.
2. I need to input a string....do calcs/output... and then prompt the user again for another string. Over and over again. How can I exit the fgets()?? In other words what does the user do to exit the program?? Ctrl-C works but there must be a more graceful way.
Thank you in advance.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Function Prototypes
// Main Program
int main()
{
char str[25];
unsigned strl, type = 2;
printf("Please select the type of conversion you want.\n");
printf(" 1. Convert signed Hex to Dec\n");
printf(" 2. Convert signed Dec to Hex\n");
scanf_s("%u", &type);
if (type == 1)
{
// Converting from signed Hexadecimal to Decimal
strl = 0;
}
else if (type == 2)
{
// Converting from signed Decimal to Hexadecimal
while(printf("=============================\nPlease enter a signed decimal number: "), fgets(str, sizeof(str), stdin) != NULL)
{
strl = strlen(str);
printf("The string input is: %sThe string length is: %u\n", str, strl);
}
}
}