Name:
Sec: 06/06/2006
Continuing from your hello world program, we will be reviewing the programming and use of some of the simple building blocks of the C++ language, including simple control structures, writing functions, declaring variables and programming input/output from your C++ programs.
Review the basic control structures used in C++, including selection statements (such as if/else/else if statements) and looping statements (like for and while loops). Also look at creating new functions in C++ and passing parameters to functions as input to the function, as well as returning values from functions as the result of the function. In addition, look at simple input and output to C++ programs using the iostream library functions.
As in lab 1 you should create a new project from scratch in Visual Studio. Name the project Program1. You need to also add a new source file to your projects sources, called Program1.cpp. You should start out by using the blank “Hello World” main function as we did in the first lab.
Your first task is to create an integer variable for use in your program and use the cin and cout iostream functions to get user input and display program output. Display a prompt to the user such as “Enter an integer value (0 to quit) -> “. You should create an integer variable called count, and use cin to read in the users response to the prompt. Check that your program is working to this point by compiling and running it. You may want to print out the value of your integer variable, to ensure that your user input worked correctly. It is always a good idea, when creating a program, to work in small steps by getting a single thing to work, then checking that it works before moving on to the next thing.
Based on the value in the count integer variable, we are either going to print out a message telling the user that the program is done, or call a function. If the user entered 0, you should simple display a message such as “Goodbye” and exit the program. If the value the user entered is not zero, you will instead call a function that you will write in step 4. For now, if the value is non-zero, simply print out a message stating the value of the integer that was entered.
Write a simple function that takes a single integer as a parameter and returns nothing for a result (a void function). The body of the function should be a loop that will print out a message a number of times based on the integer parameter passed in to the function. For example, if 5 is passed in as the parameter, the function should display a message 5 times like this:
Printing out message # 1
Printing out message # 2
Printing out message # 3
Printing out message # 4
Printing out message # 5
Name your function something like 'displayMessageNTimes'. Also remember that you need to declare a prototype for your message at the top of your source code. Change your main routine to call this function with the integer the user entered if the integer was not 0.
You have now completed Program 1. As with lab 1, the final step you should perform when you complete an assignment is to upload your finished assignment to your eCollege account. Go ahead at this point and upload your Program1.cpp source file to eCollege. Once this is done you have successfully completed the programming assignment #1.