# Trouble understanding the concepts behind the code

• 10-12-2012
curlygeek
Trouble understanding the concepts behind the code
For the assignment, I have to add upon the previous by including a function to detect that the characters inputted are appropriate. They can't be letters or symbols, can only be numbers, one decimal, and only two places from the decimal.

Before I start writing code and make it really obvious I don't know what I'm doing, I'm trying to get the concepts through my head with pseudocode. I can't even get the pseudocode down because I have trouble getting the concepts.

I know in order to check characters, there needs to be conversion going on-- but why? Is it to check values? To store data? I don't understand.

This is where I'm stuck. A classmate told me the code has to check for fractions to make sure the decimal isn't over 100 but beyond that it went into one ear and out the other. I can't write pseudocode if I can't get the basic concepts. Can anyone help me out?
• 10-12-2012
curlygeek
My assignment involves entering values for pay and wages. After calculating deductions, a function returns the values as net pay. However, I need to understand the how and why behind checking each character entered.
• 10-12-2012
grumpy
Quote:

Originally Posted by curlygeek
I know in order to check characters, there needs to be conversion going on-- but why? Is it to check values? To store data? I don't understand.

If you enter the characters "56" at the keyboard when prompted to enter an integer, the keyboard sends two characters with values 53 (the '5') and 54 (the '6'). (Assuming your operating system or compiler works with the ASCII character set, which most do but not all do). To interpret those two characters as meaning a value 56, it is necessary to interpret the meaning of both characters, and then interpret the pair of them as meaning 56.

Your description is not entirely accurate, because there are many possible ways of interpreting the pair of consecutive characters '5' and '6' as the value 56. Some of the techniques for doing that involve conversion (which means translate a value of type X to a value of type Y) and some don't. Some methods of interpreting data may even involve multiple conversions.

In the example I described, there are often I/O functions to interpret a set of characters as a value. For example, scanf()'s %d format tells scanf() to interpret coming data as if it represented an integral value.
• 10-12-2012
curlygeek
Quote:

Originally Posted by grumpy
If you enter the characters "56" at the keyboard when prompted to enter an integer, the keyboard sends two characters with values 53 (the '5') and 54 (the '6'). (Assuming your operating system or compiler works with the ASCII character set, which most do but not all do). To interpret those two characters as meaning a value 56, it is necessary to interpret the meaning of both characters, and then interpret the pair of them as meaning 56.

Your description is not entirely accurate, because there are many possible ways of interpreting the pair of consecutive characters '5' and '6' as the value 56. Some of the techniques for doing that involve conversion (which means translate a value of type X to a value of type Y) and some don't. Some methods of interpreting data may even involve multiple conversions.

In the example I described, there are often I/O functions to interpret a set of characters as a value. For example, scanf()'s %d format tells scanf() to interpret coming data as if it represented an integral value.

The classmate who had showed me his code explained it to me this way, but he used one method of doing so.

Why do the characters get interpreted as 53 and 54? The compiler Bloodshed uses ASCII from what I recall.
• 10-12-2012
grumpy
All characters need distinct values, in order to tell them apart.

There are various specifications that assign values to particular characters (the letters in the alphabet, digits, punctuation characters, various types of whitespace, symbols, etc). ASCII (American National Code for Information Interchange) is just one of the specifications. It is one of the older ones, but still in common use.

As to why digits don't map exactly to their value, it is quite common in several programming languages that a character with value zero represents "end of string" (where a string is a sequence of characters that collectively have some meaning, such as "hello") or "no data in this character". It would not be a good thing to give the digit '0' a value of zero .... For example, that would make it impossible to represent the value 100 in a string. The digit '0' therefore has a non-zero value in most specifications. That affects other digits as well since, with most character sets (usually digits map to consecutive values, just not a set starting with zero).

There are other character sets too, such as Unicode, EBCDIC, and others. All of them are just a specification mapping particular characters (human readable or not) into numeric values.
• 10-13-2012
curlygeek
Okay, in a step by step process, how does this look exactly? I think if someone explained the process to me in steps I'd understand better. Say for example, what a user inputted was 44.75.
• 10-13-2012
grumpy
Try writing down on paper how you would convert a string containing the characters "44.75" into the value (or values) you want.

Imagine you are writing step-by-step instructions to do that, which will be followed by a pedantic ignoramus who will do what you tell him, no more no less, and will electrocute you if he gets confused.

You'll learn more by working out the steps for yourself, than you will if someone simply gives you the steps.
• 10-14-2012
curlygeek
Okay, the sarcasm is not needed considering all I was asking for was an example.
• 10-14-2012
oogabooga
Quote:

Originally Posted by curlygeek
Okay, the sarcasm is not needed considering all I was asking for was an example.

Read the post again. There is absolutely no trace of sarcasm whatsoever.

grumpy is describing how to come up with the algorithm yourself, attempting to teach you how to fish instead of simply cramming a trout down your gullet.
• 10-14-2012
jsuite
One thing that I think you're not understanding - could be wrong, is this:

If you say define a variable as char, and then assign it a number, like this:

char num = 26; then if I'm not mistaken it is actually a letter because of the ascII table.
• 10-14-2012
grumpy
Quote:

Originally Posted by curlygeek
Okay, the sarcasm is not needed considering all I was asking for was an example.

I was not being sarcastic. You are better off formulating your own answer to your question, rather than having someone else do it for you.

I gave a pointer to how you might do that.

Quote:

Originally Posted by jsuite
If you say define a variable as char, and then assign it a number, like this:

char num = 26; then if I'm not mistaken it is actually a letter because of the ascII table.

You are very much mistaken. ASCII 26 is a control character, known as the "substitute character" (I won't go into the reason for that name). It is the code generated by low level keyboard drivers when the user hits CTRL-Z (holds down the CTRL key and the Z key at the same time). It is also used under some older disk operating systems (like MS-DOS) to represent end of file.