Daily Archives: January 4, 2012

C++ || Using If Statements, Char & String Variables

As previously mentioned, you can use the “int/float/double” data type to store numbers. But what if you want to store letters? Char and Strings help you do that.

==== SINGLE CHAR ====

This example will demonstrate a simple program using char, which checks to see if you entered the correctly predefined letter.

Notice in line 13 I declare the char data type, naming it “userInput.” I also initialized it as an empty variable. In line 19 I used an “If/Else Statement” to determine if the user inputted value matches the predefined letter within the program. I also used the “OR” operator in line 19 to determine if the letter the user inputted was lower or uppercase. Try compiling the program simply using this
if (userInput == 'a') as your if statement, and notice the difference.

The resulting code should give this as output

Please try to guess the letter I am thinking of: K
Sorry, that was not the correct letter I was thinking of

==== CHECK IF LETTER IS UPPER CASE ====

This example is similar to the previous one, and will check if a letter is uppercase.

Notice in line 19, an If statement was used, which checked to see if the user entered data fell between letter A and letter Z. We did that by using the “AND” operator. So that IF statement is basically saying (in plain english)

IF ('userInput' is equal to or greater than 'A') AND ('userInput' is equal to or less than 'Z')

THEN it is an uppercase letter

C++ uses ASCII codes to determine letters, so from looking at the table, the letter ‘A’ would equal ASCII code number 65, letter ‘B’ would equal ASCII code number 66 and so forth, until you reach letter Z, which would equal ASCII code number 90. So in literal terms, the program is checking to see if the user input is between ASCII code number 65 thru 90. If it is, then the number is an uppercase letter, otherwise it is not.

The resulting code should give this as output

Please enter an UPPERCASE letter: G
G is an uppercase letter

==== CHECK IF LETTER IS A VOWEL ====

This example will utilize more if statements, checking to see if the user inputted data is a vowel or not. This will be very similar to the previous example, utilizing the OR operator once again.

This program should be very straight forward, and its basically checking to see if the user inputted data is the letter A, E, I, O, U or Y.

The resulting code should give the following output

Please enter a vowel: O
Correct, O is a vowel!

==== HELLO WORLD v2 ====

This last example will demonstrate using the string data type to print the line “Hello World!” to the screen.

Notice in line 10 we have to add “#include string” in order to use the getline function, which is used on line 17. Rather than just simply using the “cin” function, we used the getline function instead to read in data. That is because cin is unable to read entire sentences as input. So in line 17, the following code reads a line from the user input until a newline is entered.

The resulting code should give following output

Please enter a sentence: Hello World!
You Entered: Hello World!

C++ || Simple Math Using Integer & Double

This page will display the use of int and double data types.

==== ADDING TWO NUMBERS TOGETHER ====

To add two numbers together, you will have to first declare your variables by doing something like this.

Notice in lines 12-14, I declared my variables, giving them a name. You can name your variables anything you want, with a rule of thumb as naming them something meaningful to your code (i.e avoid giving your variables arbitrary names like “x” or “y”). In line 22 the actual math process is taking place, storing the sum of “num1” and “num2” in a variable called “sum.” I also initialized my variables to zero. You should always initialize your variables.

The above code should give you the following output:

Please enter the first number: 8
Please enter the second number: 24
The sum of 8 and 24 is: 32

==== SUBTRACTING TWO NUMBERS ====

Subtracting two numbers works the same way as the above code, and we would only need to edit the above code in one place to achieve that. In line 22, replace the addition symbol with a subtraction sign, and you should have something like this:

The above code should give you the following output:

Please enter the first number: 8
Please enter the second number: 24
The difference between 8 and 24 is: -16

==== MULTIPLYING TWO NUMBERS ====

This can be achieved the same way as the 2 previous methods, simply by editing line 22, and replacing the designated math operator with the star symbol “*”.

The above code should give you the following output:

Please enter the first number: 8
Please enter the second number: 24
The product of 8 and 24 is: 192

==== DIVIDING TWO NUMBERS TOGETHER ====

This one is a little different from the other three. Before we would use integer variables to store our data. In division, when you divide numbers together, sometimes they end in decimals. Integer data types can not store decimal data (try it yourself and see), so here is where we use a floating point data type to store the values.

So the resulting code will basically be the same as the other previous three, only instead of our variables being of type int, they will be of type double.

The above code should give the following output:

Please enter the first number: 8
Please enter the second number: 24
The quotient of 8 and 24 is: 0.333333

==== MODULUS ====

If you wanted to capture the remainder of the quotient you calculated from the above code, you would use the modulus operator (%).

From the above code, you would only need to edit line 22, from division, to modulus.

The above code should give the following output:

Please enter the first number: 24
Please enter the second number: 8
The remainder of 24 and 8 is: 0