Search

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”.

Show Solution

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).

Show Solution

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.

Show Solution

2.1 -- Fundamental variable definition, initialization, and assignment
Index
1.11a -- Debugging your program (watching variables and the call stack)

417 comments to 1.12 — Chapter 1 comprehensive quiz

  • Jimi

    The calculator i made is a little unnecessarily complicated as while making it i wanted to try out things like the if and goto commands, as well as make sure i’ve got a handle on all the things discussed in older lessons.

    main.cpp

    askStuff.cpp

    mathFunctions.cpp

    askStuff.h

    • Jimi

      Oh, and all the std::cout << std::endl; statements are there just to add newlines into the program. Really they’re pretty unnecessary as they make the code more difficult to read while making the program a little prettier. I’m sure there’s a better way to add newlines but this is the only method i know.

    • nascardriver

      Hi Jimi!

      > I’m sure there’s a better way to add newlines but this is the only method i know.
      There’s ‘\n’.
      std::cout << "Hello\nWorld" << std::endl;

      Output

      General stuff
      Don’t ever use goto! All modern languages offer various other, cleaner, reader friendlier ways of achieving the same results.
      Try to limit each line to a maximum of 80 characters. Longer lines might not display correctly on small screens.

      main.cpp
      Line 12: Bad variable name

      askStuff.cpp
      Line 4: Unnecessary forward declaration, this will add extra work if you decide to change the function signature
      Line 8: Initialize your variables, this goes for all other variable declarations, I won’t list all of them
      @askInput and @recordInput could be merged into one function to prevent the creation of 1 variable and 1 function call

      mathFunctions.cpp
      Line 4: Bad function name or behavior. @doMath should do math and not print anything or have a different name.
      Line 6+: Use ‘else if’

      PS: Edit your posts instead of deleting and re-posting. Syntax highlighting will work after refreshing the page.

      • Jimi

        Thank you for the feedback!
        I agree that some variables and functions have dumb names, I initially mixed up some variables and had issues because of that.

        A few notes though.

        In doMath last.cpp, how would I use else if? Maybe I don’t fully understand the statement, but I don’t think I need to do anything if, for example, x was not 1.

        The reason recordInput is it’s own function is because it’s being used by main, askInput and askOperand. I can reuse the function that way.

        • nascardriver

          > In doMath last.cpp, how would I use else if?

Leave a Comment

Put C++ code inside [code][/code] tags to use the syntax highlighter