bentechcomm.files.wordpress.com€¦  · web viewhow to write a c++ program that determines if a...

17
How to write a C++ Program that determines if a word is a palindrome or not Eng 3050 Mrs. Amy Ann Latawiec February 14, 2016 By Afnan Tahir Benjamin Lewis Brantley-Brandon

Upload: others

Post on 19-Sep-2020

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: bentechcomm.files.wordpress.com€¦  · Web viewHow to write a C++ Program that determines if a word is a palindrome or not. Eng. 3050. Mrs. Amy Ann . Latawiec. February 14, 2016

How to write a C++ Program that determines if a word is a palindrome or not

Eng 3050

Mrs. Amy Ann Latawiec

February 14, 2016

By

Afnan Tahir

Benjamin Lewis Brantley-Brandon

Page 2: bentechcomm.files.wordpress.com€¦  · Web viewHow to write a C++ Program that determines if a word is a palindrome or not. Eng. 3050. Mrs. Amy Ann . Latawiec. February 14, 2016

Introduction:

This instruction manual will show you how to write and test a C++ program that can detect if a word is a palindrome or not. The following instructions are intended for students who are enrolled in Computer Sciences and/or Engineering program. Simple knowledge about programming is needed, for example coding terms. We will be using Microsoft Visual Studio 2010, where you will learn how to use the C++ programming language to write a fully functional code.

You will need to download and install Microsoft Visual Studio 2010 on your computer or laptop if it isn’t installed already.

There are few parts to the instruction:

Part A of this manual will show you how to create a new project Part B of this manual will show you how to write the code correctly Part C of this manual will show you how to compile and run the program Part D is troubleshooting

Materials:

A computer/ laptop with windows 7 or any new version of windows Microsoft Visual Studio 2010 software.

Warning: Only download Microsoft Visual Studio 2010 from the following Microsoft website http://www.microsoft.com/visualstudio/en-us/try or a CD, other websites or torrents can download viruses on your computer.

Part A: How to make a new project

1. Start out by opening Microsoft Visual Studio on your computer/ laptop

2. Click on the file tab, on the top left hand corner

3. Then scroll down to new (this will open up more options)

4. Click on Project (See figure 1)

A new window will appear (See figure 2)

Page 3: bentechcomm.files.wordpress.com€¦  · Web viewHow to write a C++ Program that determines if a word is a palindrome or not. Eng. 3050. Mrs. Amy Ann . Latawiec. February 14, 2016

Figure 1: How to create a new project

Figure 2: Choose a template

5. Click on Visual C++ on the left hand side (see figure 2)

6. Now click on “Win32ConsoleApplication”. (See figure 2)

7. Down where it says Name, enter a name for your project. (See Figure 2)

Page 4: bentechcomm.files.wordpress.com€¦  · Web viewHow to write a C++ Program that determines if a word is a palindrome or not. Eng. 3050. Mrs. Amy Ann . Latawiec. February 14, 2016

A decent name would be Palindrome Code so you know what that is

Warning: If you do not put in a name you will not be able to proceed

8. On the bottom hand side click the ok button to continue. A new window will appear.

9. Click on Console Application under the Application Type setting. (See figure 3)

10. Click on Empty Project under the Additional Project setting. (See figure 3)

This will erase all previous settings

Figure 3: Application Settings

11. Click the “Finish” on the bottom right side to continue. A solution explorer screen will appear (see figure 4)

Page 5: bentechcomm.files.wordpress.com€¦  · Web viewHow to write a C++ Program that determines if a word is a palindrome or not. Eng. 3050. Mrs. Amy Ann . Latawiec. February 14, 2016

Figure 4: Solution Explorer Menu12. Right click on the folder labeled as Source Files on the bottom.

13. Scroll over to add.

14. Then click New Item.

A new window will appear allowing you to choose which file to input in your program

15. On the left hand side, click on Visual C++ (if it isn’t already chosen)

16. Click on C++ File (.cpp) on the right hand side of the window. (See figure 5)

Figure 5: Adding New Item

Page 6: bentechcomm.files.wordpress.com€¦  · Web viewHow to write a C++ Program that determines if a word is a palindrome or not. Eng. 3050. Mrs. Amy Ann . Latawiec. February 14, 2016

17. On the bottom of the window name your file in the box labeled as Name. (See figure 5)

18. Click Add, on the bottom right side of the window.

Warning: If you did not complete properly the steps written above, your program will not work. You will have to restart the whole process

(The program is ready for typing the code)

Part B: How to write the code correctly

19. Start by typing in two forward slashes and then the name of the project. (see figure 6) // is merely used for comments to help user remind what is happening; whatever you

write about these slashes are not part of the program.

Figure 6: Title of the project

20. Now press enter.

21. Type #include <iostream> (See Figure 7)

#include <iostream> allows your program to access the C++ programming dictionary.

Page 7: bentechcomm.files.wordpress.com€¦  · Web viewHow to write a C++ Program that determines if a word is a palindrome or not. Eng. 3050. Mrs. Amy Ann . Latawiec. February 14, 2016

Figure 7: C++ parameters

22. Press enter, and then type #include <fstream>. (See Figure 7) #include <fstream> is where the definition of stream is in the C++ dictionary.

23. Press enter, and then type #include <cstring> (See Figure 7) #include <cstring> makes string.h accessible, which is a stream in the programming

language C, since C++ is an advanced programming tool derived from C, we can use the C streams in our program.

24. Press enter, and then type using namespace std; (See Figure 7) using namespacestd; you are allowing the program to include all functions from the C+

+ library

25. Press enter, and then type int main() followed by a right Brace { (See Figure 7) int main() is the main function of every program. the right brace { just indicates that now, we are ready write the actual programming

code.

26. Press enter, and then type bool palindrome=true; (See Figure 8) We use the bool function because it allows the program to return either a true or false

statement.

Page 8: bentechcomm.files.wordpress.com€¦  · Web viewHow to write a C++ Program that determines if a word is a palindrome or not. Eng. 3050. Mrs. Amy Ann . Latawiec. February 14, 2016

In the coding line bool palindrome=true; we initially start out by setting a word “palindrome” as “true” (it is a palindrome). This will help us set parameters for when palindrome can be true.

Figure 8: Next step of program

27. Press enter, and the type char word[30]; (See Figure 8) This is our first parameter, whatever word the user enters it cannot be bigger than 30

characters. char just defines that; whatever the user enters the program will treat it as individual

characters.

28. Press enter, and then type cout << "Please input a word and press return" << endl; (See figure 8) Cout is a way through which the program communicates to the user. It tells the user to

enter something.

29. Enter, and then type cin>>word; (See Figure 8) cin is what processes the word that the user has entered. This is the code that lets the

user inputs a value or phrase into the program.

30. Press enter, and then type int length = strlen(word); (See Figure 9)

Page 9: bentechcomm.files.wordpress.com€¦  · Web viewHow to write a C++ Program that determines if a word is a palindrome or not. Eng. 3050. Mrs. Amy Ann . Latawiec. February 14, 2016

This piece of code tells the program to calculate the number of characters in the word, which the user has entered.

Figure 8: Continuation of code

31. Press enter, and then type int char_checks=int(length/2); (See Figure 9) This code tells the program to divide the length or the characters of the word the user has

entered by 2. This will help the program match the character’s positions. It will match the first letter

with the last letter and keep going until it comes in the middle from both sides.

32. Press enter, and then type if (length>0) followed by a right brace { (See Figure 9) Now we are telling the program that “if the word entered has a greater length then 0”

then proceed to the next step. We put another right brace because we are telling the program to only run the rest of the

program if the word length is greater 0.

33. Press enter, and then type for(int i=0;i<(char_checks);i++) followed by another right brace { (See Figure 9) This piece of code tells the program to start at the first letter and to continue searching for

that first letter until it reaches the last letter of the word or phrase.

Page 10: bentechcomm.files.wordpress.com€¦  · Web viewHow to write a C++ Program that determines if a word is a palindrome or not. Eng. 3050. Mrs. Amy Ann . Latawiec. February 14, 2016

Now the right brace is introduced to make sure that the rest of the word or phrase that follows, through the same process as the first letter has.

34. Press enter, and then type if(word[i]!=word[length-1-i]) (See Figure 9) Now we are matching each letter through its position to the same letter at an opposite

position. This will determine if the letters are the same and if they are at opposite positions. (Ex. If we entered BOB, then this line will help match the first B with last letter B, rather than matching it with O)

But in this case we are telling the program that if the characters don’t match then do the following…

35. Press enter, and then type palindrome=false; followed by two left braces} } (See Figure 9) In the previous step we told the program that if a letter doesn’t match its opposite letter

in the word or phrase, then now return the whole palindrome as false (the word is not a palindrome).

36. Press enter, and then type if(palindrome==true) followed by {. (See Figure 10) Now we are telling the program that if in step 33 and 34 the word does come out to be

palindrome then do the following… The right brace is introduced to help this line of code link with the rest of the code left.

Figure 10: Completed code

37. Press enter, and then type cout << "The word is a palindrome" << endl; followed by a left brace} (See Figure 10) This step linked to the previous one, will display the message “the word is a palindrome”

if the previous step is true.

38. Press enter, and then type else { cout << "The word is not a palindrome" << endl; } (See Figure 10) If step 36 is not true and its false then the program will display the following “The word is

not a Palindrome”.

39. Press enter, and then type cin.get(); (See Figure 10) This code tells the program to do one of the previous two commands.

40. Press enter, and then type return 0; } (See Figure 10)

Page 11: bentechcomm.files.wordpress.com€¦  · Web viewHow to write a C++ Program that determines if a word is a palindrome or not. Eng. 3050. Mrs. Amy Ann . Latawiec. February 14, 2016

This will be the last line of your code; it makes sure that the program is compiled completely.

Warning: If you have not written the code exactly as mentioned above, you program will not compile and will not run.

Part C: How to compile and run the program

41. To make sure that there are no errors in your program, click on the build tab on the top of the screen. (See Figure 11) More options will show up.

Page 12: bentechcomm.files.wordpress.com€¦  · Web viewHow to write a C++ Program that determines if a word is a palindrome or not. Eng. 3050. Mrs. Amy Ann . Latawiec. February 14, 2016

Figure 11: How to compile

42. Go down to the bottom and click compile. (See Figure 12) On the bottom of the screen you will see the program compiling. If all the steps have been followed the program will compile correctly as shown in Figure

12.

Figure 12: Completed compilation

43. On the top of the screen click on the Debug tab. (See Figure 13) More options will show up.

Page 13: bentechcomm.files.wordpress.com€¦  · Web viewHow to write a C++ Program that determines if a word is a palindrome or not. Eng. 3050. Mrs. Amy Ann . Latawiec. February 14, 2016

Figure 13: Starting the program

44. Click on the third option Start Without Debugging. (See Figure 13) A new window will appear

45. Click Yes. (See Figure 14) Now your program will start.

Figure 14: Permission to start the program

46. A black window will appear asking you to enter a word. (See Figure 15/16) Enter a word

Page 14: bentechcomm.files.wordpress.com€¦  · Web viewHow to write a C++ Program that determines if a word is a palindrome or not. Eng. 3050. Mrs. Amy Ann . Latawiec. February 14, 2016

47. Press enter and the program will tell you if your word is a palindrome or not. (See Figure 15/16)

Figure 15: It’s a Palindrome

Figure 16: Not a Palindrome

Part D: Troubleshooting

If your program doesn’t compile correctly

Page 15: bentechcomm.files.wordpress.com€¦  · Web viewHow to write a C++ Program that determines if a word is a palindrome or not. Eng. 3050. Mrs. Amy Ann . Latawiec. February 14, 2016

Make sure:

Your wrote exactly the same code mentioned above There are no spelling mistakes You have the proper version of Microsoft Visual Studio 2010