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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
// ============================================================================ // Author: Kenneth Perkins // Date: Jan 4, 2012 // Updated: Feb 8, 2021 // Taken From: http://programmingnotes.org/ // File: char.cpp // Description: Demonstrates using char variables // ============================================================================ #include <iostream> int main() { // Declare empty variable char userInput = ' '; std::cout << "Please try to guess the letter I am thinking of: "; std::cin >> userInput; // Use an If Statement to check equality if (userInput == 'a' || userInput == 'A') { std::cout <<"\nYou have Guessed correctly!"<< std::endl; } else { std::cout <<"\nSorry, that was not the correct letter I was thinking of"<< std::endl; } return 0; }// http://programmingnotes.org/ |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
// ============================================================================ // Author: Kenneth Perkins // Date: Jan 4, 2012 // Updated: Feb 8, 2021 // Taken From: http://programmingnotes.org/ // File: uppercase.cpp // Description: Demonstrates checking if a char variable is uppercase // ============================================================================ #include <iostream> int main() { // Declare empty variable char userInput = ' '; std::cout << "Please enter an UPPERCASE letter: "; std::cin >> userInput; // Checks to see if entered data falls between uppercase values if ((userInput >= 'A') && (userInput <= 'Z')) { std::cout << "\n" << userInput << " is an uppercase letter"<< std::endl; } else { std::cout <<"\nSorry, "<< userInput <<" is not an uppercase letter"<< std::endl; } return 0; }// http://programmingnotes.org/ |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
// ============================================================================ // Author: Kenneth Perkins // Date: Jan 4, 2012 // Updated: Feb 8, 2021 // Taken From: http://programmingnotes.org/ // File: vowel.cpp // Description: Demonstrates checking if a char variable is a vowel // ============================================================================ #include <iostream> int main() { // Declare empty variable char userInput = ' '; std::cout << "Please enter a vowel: "; std::cin >> userInput; // Checks to see if inputted data is A,E,I,O,U,Y if ((userInput == 'a')||(userInput == 'A')||(userInput == 'e')|| (userInput == 'E')||(userInput == 'i')||(userInput == 'I')|| (userInput == 'o')||(userInput == 'O')||(userInput == 'u')|| (userInput == 'U')||(userInput == 'y')||(userInput == 'Y')) { std::cout <<"\nCorrect, "<< userInput <<" is a vowel!"<< std::endl; } else { std::cout <<"\nSorry, "<< userInput <<" is not a vowel"<< std::endl; } return 0; }// http://programmingnotes.org/ |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
// ============================================================================ // Author: Kenneth Perkins // Date: Jan 4, 2012 // Updated: Feb 8, 2021 // Taken From: http://programmingnotes.org/ // File: string.cpp // Description: Demonstrates using string variables // ============================================================================ #include <iostream> #include <string> int main() { // Declare empty variable std::string userInput = " "; std::cout << "Please enter a sentence: "; std::getline(std::cin, userInput); std::cout << "\nYou Entered: "<<userInput << std::endl; return 0; }// http://programmingnotes.com |
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!
Leave a Reply