1.12 __ Chapter 1 comprehensive quiz
Question 1
Write a single-file program (named main.cpp) that reads two separate integers from the user, adds them together, and then outputs the answer. The program should use three functions:
- A function named “readNumber” should be used to get (and return) a single integer from the user.
- A function named “writeAnswer” should be used to output the answer. This function should take a single parameter and have no return value.
- A main() function should be used to glue the above functions together.
Hint: You do not need to write a separate function to do the adding (just use operator+ directly).
Hint: You will need to call readNumber() twice.
Hint: If you’re using visual studio with precompiled headers, don’t forget to #include “stdafx.h”.
Hint: You will need to call readNumber() twice.
Hint: If you’re using visual studio with precompiled headers, don’t forget to #include “stdafx.h”.
Question 2
Modify the program you wrote in exercise #1 so that readNumber and writeAnswer() live in a separate file called “io.cpp”. Use a forward declaration to access them from main().
Hint: If you’re having problems, make sure io.cpp is properly added to your project so it gets compiled (see lession 1.8 -- programs with multiple files for more information on how to do this).
Question 3
Modify the program you wrote in #2 so that it uses a header file (named io.h) to access the functions instead of using forward declarations directly in your code (.cpp) files. Make sure your header file uses header guards.
io.cpp:
io.h:
main.cpp:
1
2
3
4
5
6
7
8
9
|
#include "io.h"
int main()
{
int x = readNumber();
int y = readNumber();
writeAnswer(x+y);
return 0;
}
|
No comments:
Post a Comment
whatiscpp.blogspot.com is a free website devoted to teaching you how to program in C++. Whether you’ve had any prior programming experience or not, the tutorials on this site will walk you through all the steps to write, compile, and debug your C++ programs, all with plenty of examples.