This program keeps adds the sum of numbers you put in. You can also quit by typing in quit, its not case sensitive since I have code which converts all uppercase letters into lowercase. This in fact is the problem. For some reason when I insert a number it works the first time but on the second it complains of an illegal character this is because for some reason the second number I put in get converted to the corresponding index of my upper[] array. Even thou I have no code converting numbers to letters. I temp fixed this by putting the code which converts upper to lower after the code which does computations. But Id like to know why doesnt the code work when the upperTolower code is before the computation?
#include <stdio.h>
#include <string.h>
void main()
{
char input[256];
int toggle = 1;
int total = 0;
printf("Welcome to the Very Silly program\n");
while (1)
{
printf("Enter a command: \n");
fgets(input, 256, stdin);
input[strlen(input)-1] = '\0';
char upper[26] = {'Q','W','F','P','G','J','L','U','Y','A','R','S','T','D','H','N','E','I','O','Z','X','C','V','B','K','M'};
char lower[26] = {'q','w','f','p','g','j','l','u','y','a','r','s','t','d','h','n','e','i','o','z','x','c','v','b','k','m'};
int u;
int y;
//for (u = 0; u != (strlen(input)); u++)
//{
// for (y = 0; y != (strlen(upper)); y++)
// {
// if (input[u] == upper[y])
// {
// input[u] = lower[y];
// }
// }
//}
int illegal = 0;
int letter = 0;
int numFlag = 1;
int i;
int d;
int ch_dig = 0;
char num[10] = {'0','1','2','3','4','5','6','7','8','9'};
int numInput[10];
int neg = 0;
if (input[0] == '-'){ch_dig++;neg++;}
for (i = 0; i != strlen(input); i++)
{
for (d = 0; d != strlen(num); d++)
{
if (input[i] == num[d])
{
ch_dig++; numInput[i] = d; break;
}
}
}
if (strlen(input) == ch_dig) {numFlag = 0;}
if (numFlag == 0)
{
if (neg == 1)
{
int v=1;
for (i = 1; i != strlen(input); i++)
{
total -= (numInput[i] * v);
v *= 10;
}
}
else
{
int v = 1;
for (i = 0; i != strlen(input); i++)
{
total += (numInput[i] * v);
v *= 10;
}
}
printf("%d\n", total);
illegal = 1;
letter = 1;
}
if (letter == 0)
{
for (u = 0; u != (strlen(input)); u++)
{
for (y = 0; y != (strlen(upper)); y++)
{
if (input[u] == upper[y])
{
input[u] = lower[y];
}
}
}
}
if (strcmp(input, "quit") == 0) {illegal=1;printf("Peace Out\n");break;}
if (illegal == 0)
{
printf("Illegal Command\n");
}
}
}