Professional Documents
Culture Documents
Topics
Decisions and Loops
Learning Objective
Be able to write and compile basic C++ code to solve a simple programming problem continents
decision(s) and loop(s)
b. Have the user enter their name when they start the program. Add some instructions to tell
the user what the program does, what the program accepts as inputs and what the program
outputs, addressing the player by their name.
Lab 3
1
MCD4720 Lab 3 MCD4720 Fundamentals of C++
Have the user enter their first name and store this in a string object. Run a basic loop to look at
each letter in the string and calculate the users name number. It is calculated as follows:
Add up the letters based on their corresponding number in the alphabet. For example,
‘a’ = 1, ‘b’ = 2, all the way up to ‘z’ = 26
To derive your final name number, you should add up the digits that make that number.
If the total is a result of double figures, you add again. For example “matt” gives the
preliminary name number 54. Adding 5 + 4 gives a final number of 9. Another example is
“nick”. His “preliminary” name number is 37. 3 + 7 = 10, so we then add the digits 1 + 0
to arrive at the final answer of 1.
Hints… how can you get the single digits in a large number? Will modulus help?
Lab 3
2
MCD4720 Lab 3 MCD4720 Fundamentals of C++
Task 4: Is Prime?
Write a small piece of code that will determine if a number entered by the user is a prime
number. Remember that a prime number is one that is only divisible by 1 or itself.
Task 5: Troubleshooting
The following code is designed to have the user enter an integer between 1 and 10, and print the
times tables up to (but not over) 100. Alas, it doesn’t do it. Copy and paste the code into a new
project and get it working! Be sure to use real-time debugging to trace the loop. At the bottom is
an example of working code.
integer nNum = 0;
cout << "This program will accept an integer between 1 and 10,";
cout << "and display the times tables up to 100." << \n;
cout << nNum << " times i is " << (nNum * i) << endl;
i++;
Lab 3
3
MCD4720 Lab 3 MCD4720 Fundamentals of C++
Now that you have designed the game logic and created the start of the code, you can now finish the
game! Features you should now be able to add on to those coded last week include:
Lab 3
4