Search

1.x — Chapter 1 summary and quiz

Quick Summary

A statement is a type of instruction that causes the program to perform some action. Statements are often terminated by a semicolon.

A function is a collection of statements that execute sequentially. Every C++ program must include a special function named main. When you run your program, execution starts at the top of the main function.

Preprocessor directives tell the compiler to perform a special task. In this chapter, we use them to #include <iostream>, which allows us to access the input/output routines in the standard library.

The rules that govern how elements of the C++ language are constructed is called a syntax. A syntax error occurs when you violate the grammatical rules of the language.

Comments allow the programmer to leave notes in the code. C++ supports two types of comments. Line comments start with a // and run to the end of the line. Block comments start with a /* and go to the paired */ symbol. Don’t nest comments.

You can use comments to temporarily disable lines or sections of code. This is called commenting out your code.

Data is any sequence of symbols that can be interpreted to mean something. A single piece of data, stored somewhere in memory is called a value.

A variable is a named piece of memory that we can use to store values. A variable’s name is called an identifier. In order to create a variable, we use a statement called a definition statement. When the program is run, each defined variable is instantiated, which means it is assigned a memory address.

A data type tells the compiler how to interpret a piece of data into a meaningful value. An integer is a number that can be written without a fractional component, such as 4, 27, 0, -2, or -12.

Copy assignment (via operator=) can be used to assign an already created variable a value.

Initialization can be used to give a variable a value at the point of creation. C++ supports 3 types of initialization: copy initialization, direct initialization, and uniform initialization.

You should prefer uniform initialization over the other initialization forms, and prefer initialization over assignment.

Although you can define multiple variables in a single statement, it’s better to define and initialize each variable on its own line, in a separate statement.

std::cout and operator<< allow us to output an expression to the console as text. std::endl outputs a new line character, forcing the console cursor to move to the next line. std::cin and operator>> allow us to get a value from the keyboard.

A variable that has not been given a value is called an uninitialized variable. Trying to get the value of an uninitialized variable will result in undefined behavior, which can manifest in any number of ways.

C++ reserves a set of names called keywords. These have special meaning within the language and may not be used as variable names.

A literal constant is a fixed value inserted directly into the source code. Examples are 5 and “Hello world!”.

An operation is a mathematical calculation involving zero or more input values, called operands. The specific operation to be performed is denoted by the provided operator. The result of an operation produces an output value.

Unary operators take one operand. Binary operators take two operands, often called left and right. Ternary operators take three operands.

An expression is a combination of literals, variables, operators, and function calls that are evaluated to produce a single output value. The calculation of this output value is called evaluation. The value produced is the result of the expression.

An expression statement is an expression that has been turned into a statement by placing a semicolon at the end of the expression.

Programming is hard, and your programs will rarely come out perfect (or close to it) the first time. Get your programs working first, then refine them into something great.

Quiz time

Question #1


What is the difference between initialization and assignment?

Show Solution

Question #2


When does undefined behavior occur? What are the consequences of undefined behavior?

Show Solution

Question #3


Write a program that asks the user to enter a number, and then enter a second number. The program should tell the user what the result of adding and subtracting the two numbers are.

The output of the program should match the following (assuming inputs of 6 and 4):

Enter an integer: 6
Enter another integer: 4
6 + 4 is 10.
6 - 4 is 2.

Show Solution


2.1 -- Introduction to functions
Index
1.10 -- Developing your first program

212 comments to 1.x — Chapter 1 summary and quiz

  • Alam

    Hello everyone.i did the quiz as below but it gives an error.let me copy it here and the error it gives

    #include "stdafx.h"
    #include
    int RealNumber()
    {
    using namespace std;
    cout <> x;
    }

    void WriteAnswer(int x)
    {
    using namespace std;
    cout << "The answe is " << x << endl;
    }

    #ifndef IO_H
    #define IO_H

    int RealNumber();
    void WriteAnswe (int x);

    #endif

    #include "io.h"

    int main()
    {
    int x = RealNumber();
    int y = RealNumber();
    WriteAnswer(x+y);
    return 0;
    }

    Error 1 error C4716: 'RealNumber' : must return a value c:usersalamdocumentsvisual studio 2012projectspractice problem3practice problem3practice problem3.cpp 12 1 practice problem3

  • adeater

    What does it mean when "[Error] ld returned 1 exit status" ?
    I'm stuck at this point. I don't know how to link these source files to run the program.
    Even if I copy and paste this exactly the same code from this page it won't work. I use Dev C++ 5.7.1
    What should I do next to get this program going?

  • The_Femo

    I don't know whats wrong...I did the quiz...didn't work......I even copied and pasted the info and got this error

    >Testing.obj : error LNK2019: unresolved external symbol "void __cdecl WriteAnswer(int)" (?WriteAnswer@@YAXH@Z) referenced in function _main
    1>Testing.obj : error LNK2019: unresolved external symbol "int __cdecl ReadNumber(void)" (?ReadNumber@@YAHXZ) referenced in function _main
    1>C:CodingTestingDebugTesting.exe : fatal error LNK1120: 2 unresolved externals
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

    #include "stdafx.h"
    #include

    int ReadNumber();
    void WriteAnswer(int x);

    int main()
    {
    int x = ReadNumber();
    int y = ReadNumber();
    WriteAnswer(x+y);
    return 0;
    }

    io.cpp

    #include
    int ReadNumber()
    {
    using namespace std;
    cout <> x;
    return x;
    }

    void WriteAnswer(int x)
    {
    using namespace std;
    cout << "The answer is " << x << endl;
    }

  • jki394

    This is what I got... Would this also be considered correct?

    #include
    using namespace std;

    int readNumber()
    {
    cout <> x;

    cout <> y;
    }

    void writeAnswer(int x; int y)
    {
    cout << x + y;
    }

    int main()
    {
    int x = readNumber();
    int y = readNumber();
    writeAnswer(x + y);

    }

  • fatin_ferrari

    Hi everyone!
    I built up my own code, 2 ways but one of them didn't work

    The 1st code-run successfully
    -------------------------------
    //io.cpp

    #include
    int ReadNumber()
    {
    using namespace std;
    cout<> j;
    return j;

    }

    int Add(int x, int y)
    {

    int z;
    z = x+y;
    return z;
    }

    void WriteAnswer(int q)
    {
    using namespace std;
    cout<<"The answer is:"<< q <<endl;
    }

    //io.h

    #ifndef IO_H
    #define IO_H

    int ReadNumber();
    int Add(int x, int y);
    void WriteAnswer(int q);

    #endif

    //main.cpp

    #include "io.h"
    int main()
    {

    int t = ReadNumber();
    int s = ReadNumber();
    int a = Add(t,s);
    WriteAnswer(a);
    return 0;
    }
    ---------------------------------------
    //
    //
    However, after I modified the code by adding other files for add() function, it didn't work. Can any experts help on this? Below is the code:
    -------------------------------------------
    //io.cpp

    #include
    int ReadNumber()
    {
    using namespace std;
    cout<> j;
    return j;

    }

    void WriteAnswer(int q)
    {
    using namespace std;
    cout<<"The answer is:"<< q <<endl;
    }

    //io.h

    #ifndef IO_H
    #define IO_H

    int ReadNumber();
    void WriteAnswer(int q);

    #endif

    //Multi.cpp

    int Add(int x, int y)
    {
    int z;
    z = x+y;
    return z;

    }

    //Multi.h

    #ifndef MULTI_H
    #define MULTI_H

    int Add(int x, int y);

    #endif

    //main.cpp
    #include "io.h"
    #include "multi.h"
    int main()
    {

    int t = ReadNumber();
    int s = ReadNumber();
    int a = Add(t,s);
    WriteAnswer(a);
    return 0;
    }

    For the second code, I succeeded to build the code but while running, it pauses at add() function (line z=x+y) shows;
    -----------------
    Enter a number:3
    Enter a number:2
    (lldb)

    Tread1: breakpoint 1.1 (at z=x+y line)

    Thanks in advanced for helping!

  • Alex, or anyone else can help in explaining why I am getting error for ReadNumber() "function does not take 0 argument" when I forward declare the readnumber as:
    int ReadNumber(int x) i.e int x defined inside the paranthesis; but if I change it to:
    int ReadNumber(); the program complied with success. This change was done for both main.cpp and io.cpp Appreciate your clarification.
    For WriteAnswer(int FinalResult) i had the variable defined inside the parenthasis, but I didn't get any errors, why only for ReadNumber ??? lost here

    • rameye

      A forward declaration is the function definition without the function body. It tells return type, function name, and the number of and type of the function parameters.

      When you call the function after the forward declaration has been given, the number of parameters in the function call has to match with the number of parameters in the forward declaration.

      So if you change the forward declaration by making it have one argument instead of zero arguments, then you have to also change the function calls to have one argument. For example from int num = ReadNumber(); to int num = ReadNumber(10);

      If you don't do that and still call the function with 0 arguments the compiler will complain about it. Mis-matched parameter list.

Leave a Reply

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">