C++ Tutorial #19: Character Sequences

Facebooktwittergoogle_plusredditpinterestlinkedinmail

In previous tutorials, we have talked about strings. Strings are used to handle a series of characters, but you can also use Character Sequences to accomplish the same task. The difference between the two is that with character sequences, you can set a maximum capacity. Think of them as those name boxes where you place each letter of your name in the box. To create character sequences In C++ Tutorial #2 when I discussed variables and types, I’ve said that char represents a single character. This is true, however, you can also store an array of characters in char. How is it done?

 

Just like creating an array, but since we’re handling characters; simply create type char, assign a name, and set the number of elements.

 

type variable [elements]

 

 

It should look like the example below:

 

 

 

There are two ways you can assign values to the character sequence. The first method, you can do the same thing we try with arrays, and that’s by placing a value in curly braces and separating each element with a comma. Since we’re dealing with characters, remember to surround each character with single quotes. If your character sequence hasn’t reached the maximum capacity of the array, you can place a ‘\0’ at the end of it. ‘\0’ is a null character, meaning no value.

 

 

 

The second method is a lot easier; You can just place a string in double-quotes. The null character will automatically append at the end of the string.

 

 

Be sure you know how to do both. The first is most important to know, that way you can make changes to the character sequence. For example, you might want to change “frank” to “stank”. You can simply accomplish this by using the array method:

 

 

I believe there is a way to also accomplish this with string functions, but I’ll write about this in another tutorial. Now that you have an idea of how character sequences work, we’re going to make a little game using goto statements. Let’s get started on writing a program.

 

 

Code:

 

 

 

Your program should look something like this:

 

What is your name? Kevin
What is the color of the sky? blue
What is the opposite of down? up

Kevin blue up! HAHAHA! GAME OVER!!!

Would you like to play again (Yes/No)? No

 

Let me explain what our code does:

 

In this section of code, we create our character sequences: A1 – A4 for the answer input and Q1 – Q4 for the questions. We also create a string called tempStr; this string takes the value of each character sequence: A1 – A4. Without it, we’ll be unable to compare conditions using the if statements.

 

 

Question 1 is part of the goto statement, The program will jump back to it if the user decides to play the game again. The next line prints out the first question stored in character sequence Q1. The final line allows the user to input an answer.

 

 

Then the program moves on to question #2. First line of this is also part of the goto statement. This makes the user move back to the question if he/she gets the answer wrong. After the question and answering tempStr is used to convert A2 to a string. The if statement is then used to check if the user’s answer is correct. If correct, the program will move on to question #3. If the user is wrong, the program will jump back to question #2.

 

 

This section of code does the same thing as question #2. If question #3 is correct, the user will advance to question #4.

 

 

At question #4 the game is over. The program asks the user if they want to play again. If the user inputs “yes” or “Yes”, the game reset. Any other answer given will end the game.

 

I hope you have enjoyed this tutorial. I know it’s about the same as using strings, but being able to use character sequences uses less memory. I’m basically showing you more than one way to skin a cat. Don’t forget to like, share, and subscribe to my newsletter. I will be writing more tutorials soon.

Facebooktwittergoogle_plusredditpinterestlinkedinmail

About Justin

Computer programmer and owner of kauergames.com.
Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *