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");
		}
	}
}

My eyeballs get tired just studying your code. ;)

Instead of two arrays to convert letters from one case to the other, why not use toupper() or tolower() (part of ctype.h)? Another easy way would be to simply use the ascii values. Since there is a constant difference of 32 between the uppercase letter and it's lowercase equivalent, we can use:

ch += 32; //to change char ch from uppercase to lowercase
ch -= 32; //lowercase to uppercase

Both the above work ONLY for letters, and ONLY if your system uses ASCII or other character values with this 32 offset (most do). Try it on your system.

You're using u and y for for loop iterators - please don't. Use the standard idiom of i and j and k if you need it. When you have no comments in your code, it's just so much easier if you use standard C idiom's.

You should be using strtoi, atoi or sscanf() to take your string digits, and convert them into a (real) number. It's so easy.

I'm all for repentance from sin, but go to confession, instead of beating yourself up with C. ;)

Be as simple and concise and as clear, as you can be in your algorithm and code. You'll be glad you did.

Edit: set your editor up to insert 3-5 spaces, instead of full tabs, when you hit the tab key. Your indentation is so great, that your line of code has to wrap around the forum editor, making it much harder to study.

Edited 5 Years Ago by Adak: n/a

Comments
thanks for useful advice

thanks for the tips. but wat was the reason that the upper2lower didnt work b4 number calculations.

This article has been dead for over six months. Start a new discussion instead.