I am just wondering if there is a way to put the following code into a loop or two so that it is easier on the eyes and doesn't take us so many lines. I have not been able to think of a way to do this with so many different characters all equaling a different amount. By the way this is for a program that computes software code validity, and the program works fine as is, just trying to consolidate some code. I apologize for the wall of code here. Thank you for your help in advance.

for(int b = 0; b <= 19; b++)
{
		if(softChar[b] == 'a')
		{
			alphaValue = 10;
		}
			else if(softChar[b] == 'b') 
			{
				alphaValue = 11;
			}
			else if(softChar[b] == 'c')
			{
				alphaValue = 12;
			}
			else if(softChar[b] == 'd')
			{
				alphaValue = 13;
			}
			else if(softChar[b] == 'e')
			{
				alphaValue = 14;
			}
			else if(softChar[b] == 'f')
			{
				alphaValue = 15;
			}
			else if(softChar[b] == 'g')
			{
				alphaValue = 16;
			}
			else if(softChar[b] == 'h')
			{
				alphaValue = 17;
			}
			else if(softChar[b] == 'i')
			{
				alphaValue = 18;
			}
			else if(softChar[b] == 'j')
			{
				alphaValue = 19;
			}
			else if(softChar[b] == 'k')
			{
				alphaValue = 20;
			}
			else if(softChar[b] == 'l')
			{
				alphaValue = 21;
			}
			else if(softChar[b] == 'm')
			{
				alphaValue = 22;
			}
			else if(softChar[b] == 'n')
			{
				alphaValue = 23;
			}
			else if(softChar[b] == 'o')
			{
				alphaValue = 24;
			}
			else if(softChar[b] == 'p')
			{
				alphaValue = 25;
			}
			else if(softChar[b] == 'q')
			{
				alphaValue = 26;
			}
			else if(softChar[b] == 'r')
			{
				alphaValue = 27;
			}
			else if(softChar[b] == 's')
			{
				alphaValue = 28;
			}
			else if(softChar[b] == 't')
			{
				alphaValue = 29;
			}
			else if(softChar[b] == 'u')
			{
				alphaValue = 30;
			}
			else if(softChar[b] == 'v')
			{
				alphaValue = 31;
			}
			else if(softChar[b] == 'w')
			{
				alphaValue = 32;
			}
			else if(softChar[b] == 'x')
			{
				alphaValue = 33;
			}
			else if(softChar[b] == 'y')
			{
				alphaValue = 34;
			}
			else if(softChar[b] == 'z')
			{
				alphaValue = 35;
			}
		
		//*************CAPITAL LETTERS START HERE***************//
			
		if(softChar[b] == 'A')
		{
			alphaValue = 36;
		}
			else if(softChar[b] == 'B') 
			{
				alphaValue = 37;
			}
			else if(softChar[b] == 'C')
			{
				alphaValue = 38;
			}
			else if(softChar[b] == 'D')
			{
				alphaValue = 39;
			}
			else if(softChar[b] == 'E')
			{
				alphaValue = 40;
			}
			else if(softChar[b] == 'F')
			{
				alphaValue = 41;
			}
			else if(softChar[b] == 'G')
			{
				alphaValue = 42;
			}
			else if(softChar[b] == 'H')
			{
				alphaValue = 43;
			}
			else if(softChar[b] == 'I')
			{
				alphaValue = 44;
			}
			else if(softChar[b] == 'J')
			{
				alphaValue = 45;
			}
			else if(softChar[b] == 'K')
			{
				alphaValue = 46;
			}
			else if(softChar[b] == 'L')
			{
				alphaValue = 47;
			}
			else if(softChar[b] == 'M')
			{
				alphaValue = 48;
			}
			else if(softChar[b] == 'N')
			{
				alphaValue = 49;
			}
			else if(softChar[b] == 'O')
			{
				alphaValue = 50;
			}
			else if(softChar[b] == 'P')
			{
				alphaValue = 51;
			}
			else if(softChar[b] == 'Q')
			{
				alphaValue = 52;
			}
			else if(softChar[b] == 'R')
			{
				alphaValue = 53;
			}
			else if(softChar[b] == 'W')
			{
				alphaValue = 54;
			}
			else if(softChar[b] == 'T')
			{
				alphaValue = 55;
			}
			else if(softChar[b] == 'U')
			{
				alphaValue = 56;
			}
			else if(softChar[b] == 'V')
			{
				alphaValue = 57;
			}
			else if(softChar[b] == 'W')
			{
				alphaValue = 58;
			}
			else if(softChar[b] == 'X')
			{
				alphaValue = 59;
			}
			else if(softChar[b] == 'Y')
			{
				alphaValue = 60;
			}
			else if(softChar[b] == 'Z')
			{
				alphaValue = 61;
			}
		
				else 
			{
				alphaValue = int(softChar[b]) - int('0');
			}

Edited 6 Years Ago by Philosophy: n/a

Couldn't you use a switch instead of having all of those if statements? I'm sure they will be an even better method than that. Do you actually need the lower case bit? Or could 'z' and 'Z' be the same? If so, then you just need one line to capitalize it before you check which letter it is. Check this out:
http://www.daniweb.com/forums/thread100207.html

Couldn't you use a switch instead of having all of those if statements? I'm sure they will be an even better method than that. Do you actually need the lower case bit? Or could 'z' and 'Z' be the same? If so, then you just need one line to capitalize it before you check which letter it is. Check this out:
http://www.daniweb.com/forums/thread100207.html

Well, if I did it with a switch statement then wouldn't it still take up just as much room. This is due to the fact that the user can enter a-z or A-Z and each letter is a different value between 10 and 61 respectively. I remember seeing something once about a loop that could work with each type, not sure about that though. I just would think that there would be a way to consolidate this than to have 200+ lines of code just for placing values to letters. If you think a switch statement would work for this could you give a small example. The post that you linked was not what I am needing to do for this, as during user-entry they will be entered as either lower case or upper case and have to be handled as they are entered.

If you detect the "case" (upper/lower), then use mathematics, you should be able to save a ton of code. I think you may want to consider something like this:

if (islower(input)) {         //detect a "lower" case character
  value = 10 + (input - 'a'); //perform the translation
} else if (isupper(input)) {  //detect a "upper" case character
  value = 36 + (input - 'A'); //perform the translation
}

You'll have to test it, and possibly do some tweaking, to make sure its results are consistent, but you should be able to perform basic arithmetic operations on chars like this and save alot of code. Just make sure you comment it well.

Edited 6 Years Ago by Fbody: n/a

If you detect the "case" (upper/lower), then use mathematics, you should be able to save a ton of code. I think you may want to consider something like this:

if (islower(input)) {         //detect a "lower" case character
  value = 10 + (input - 'a'); //perform the translation
} else if (isupper(input)) {  //detect a "upper" case character
  value = 36 + (input - 'A'); //perform the translation
}

You'll have to test it, and possibly do some tweaking, to make sure its results are consistent, but you should be able to perform basic arithmetic operations on chars like this and save alot of code. Just make sure you comment it well.

Thank you for this, I will have to work with it and see what I can come up with. As for the encryption/decryption thread, this one and that one are completely different programs and I have never used stringstream before so your response didn't fully make sense. But I think I am getting close to getting that one going, thank you very much for your help.

Sorry about double, ran out of edit time.

>>Well, if I did it with a switch statement then wouldn't it still take up just as much room.
Depending on how you format things, it could take up a little less. You would be using 3 lines of code for each case instead of 4 as you've formatted it now. But you'd be trading the braces for case and break statements.

EDIT:
Oops, overlapped a little.
A stringstream is almost just like a file stream. The main difference is that it uses a string as a temporary storage "buffer" in memory instead of a physical file on a disk. Here is some information.

Edited 6 Years Ago by Fbody: n/a

Sorry about double, ran out of edit time.

>>Well, if I did it with a switch statement then wouldn't it still take up just as much room.
Depending on how you format things, it could take up a little less. You would be using 3 lines of code for each case instead of 4 as you've formatted it now. But you'd be trading the braces for case and break statements.

EDIT:
Oops, overlapped a little.
A stringstream is almost just like a file stream. The main difference is that it uses a string as a temporary storage "buffer" in memory instead of a physical file on a disk. Here is some information.

I will have to read up on that for future reference, thanks for the information. I like what you posted about using islower and isupper as opposed to using a switch statement. A switch statement may save me about 50-60 lines or so, but I would still be stuck with well over 100 lines of code. I am going to work with isupper and islower and see how that works, because just looking at the code you posted I can see how it should work properly, plus I'm still waiting on the professor to send me some valid and invalid software codes that he is going to use to test this program so I can get an actual idea if I am off on my calculations. But using ones that I made up and that another student in the class made up the program is running correctly. Thank you so much for your help, I'll post back if I have anymore problems with this program.

This question has already been answered. Start a new discussion instead.