C++ Primer #01 Getting Started
1. CHAPTER 1 GETTING STARTED
1.1 Writing a Simple C++ Program
Exercise 1.2:
Change the program to return -1. A return value of -1 is often treated as an indicator that the program failed. Recompile and rerun your program to see how your system treats a failure indicator from main.
Return -1:
C++ code:
xxxxxxxxxx
using namespace std;
int main()
{
std::cout << "Hello world!" << std::endl;
return -1;
}
Console:
xxxxxxxxxx
Hello world!
Process returned -1 (0xFFFFFFFF) execution time : 1.729 s
Press any key to continue.
Return 0:
C++ code:
xxxxxxxxxx
using namespace std;
int main()
{
std::cout << "Hello world!" << std::endl;
return 0;
}
Console:
xxxxxxxxxx
Hello world!
Process returned 0 (0x0) execution time : 6.631 s
Press any key to continue.
1.2 A First Look at Input/Output
Exercise 1.4:
Our program used the addition operator, +, to add two numbers. Write a program that uses the multiplication operator, *, to print the product instead.
C++ code:
xxxxxxxxxx
using namespace std;
int main()
{
std::cout << "Enter two numbers:" << std::endl;
int num1, num2;
std::cin >> num1 >> num2;
std::cout << "The product of " << num1 << " and " << num2 << " is " <<
num1*num2 << std::endl;
return 0;
}
Shell:
xxxxxxxxxx
Enter two numbers:
10
28
The product of 10 and 28 is 280
Process returned 0 (0x0) execution time : 40.071 s
Press any key to continue.
Exercise 1.6
Explain whether the following program fragment is legal.
xxxxxxxxxx
std::cout << "The sum of " << v1;
<< " and " << v2;
<< " is " << v1 + v2 << std::endl;
If the program is legal, what does it do? If the program is not legal, why not? How would you fix it?
Answer:
This program is not legal. Because there are 3 semicolons in between the source code which is unable for the compiler to compile. The fixed code is the following:
xxxxxxxxxx
std::cout << "The sum of " << v1
<< " and " << v2
<< " is " << v1 + v2 << std::endl;
1.3 A Word about Comments
Exercise 1.8:
Indicate which, if any, of the following output statements are legal:
✔️
xxxxxxxxxx
std::cout << "/*";
It prints out: /*
✔️
xxxxxxxxxx
std::cout << "*/";
It prints out: */
✔️
xxxxxxxxxx
std::cout << /* "*/" */;
It prints out: /*
✔️
xxxxxxxxxx
std::cout << /* "*/" /* "/*" */;
It prints out: /*
1.4 Flow of Control
Exercise 1.9
Write a program that uses a while to sum the numbers from 50 to 100.
Code:
xxxxxxxxxx
using namespace std;
int main()
{
int sum = 0, add = 50;
while(add <= 100){
sum+=add;
add++;
}
std::cout << "The sum from 50 to 100 is: " << sum << std::endl;
return 0;
}
Console:
xxxxxxxxxx
The sum from 50 to 100 is: 3825
Process returned 0 (0x0) execution time : 0.103 s
Press any key to continue.
Exercise 1.10:
In addition to the ++ operator that adds 1 to its operand, there is a decrement operator (--) that subtracts 1. Use the decrement operator to write a while that prints the numbers from ten down to zero.
Code:
xxxxxxxxxx
using namespace std;
int main()
{
int num = 10;
while(num >= 0){
std::cout << num << " ";
num--;
}
return 0;
}
Console:
xxxxxxxxxx
10 9 8 7 6 5 4 3 2 1 0
Process returned 0 (0x0) execution time : 0.104 s
Press any key to continue.
Exercise 1.11
Write a program that prompts the user for two integers. Print each number in the range specified by those two integers.
Code:
xxxxxxxxxx
using namespace std;
int main()
{
int num1, num2;
std::cout << "Please enter two number: " << std::endl;
std::cin >> num1 >> num2;
int big, small;
if(num1>num2){
big = num1;
small = num2;
}
else{
big = num2;
small = num1;
}
std::cout << "The range of two value is: " << std::endl;
while(small<=big){
std::cout << small << " ";
small++;
}
return 0;
}
Console:
xxxxxxxxxx
Please enter two number:
10
28
The range of two value is:
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
Process returned 0 (0x0) execution time : 8.490 s
Press any key to continue.
Exercise 1.13
Rewrite the first two exercises from § 1.4.1 (p. 13) using for loops.
Rewrite 1.10:
xxxxxxxxxx
using namespace std;
int main()
{
for(int num=10; num >=0; num--){
std::cout << num << " ";
}
return 0;
}
Rewrite 1.11:
xxxxxxxxxx
using namespace std;
int main()
{
int num1, num2;
std::cout << "Please enter two number: " << std::endl;
std::cin >> num1 >> num2;
int big, small;
if(num1>num2){
big = num1;
small = num2;
}
else{
big = num2;
small = num1;
}
std::cout << "The range of two value is: " << std::endl;
for(small; small<=big; small++){
std::cout << small << " ";
}
return 0;
}
⚠️There is another solution which specify the start and end(aka. user defines the small and big)
xxxxxxxxxx
using namespace std;
int main()
{
int start_val, end_val;
std::cout << "Please enter the start and end of the range" << std::endl;
std::cin >> start_val >> end_val;
std::cout << "The range of two value is: ";
while(start_val<=end_val){
std::cout << start_val++ << " ";
}
return 0;
}
You can even increment the value inside the loop:
xxxxxxxxxx
while(start_val<=end_val){
std::cout << start_val++ << " ";
}
Exercise 1.16
Write your own version of a program that prints the sum of a set of integers read from cin.
Oh my dear lord, I can't believe the method can works:
xxxxxxxxxx
using namespace std;
int main()
{
int sum = 0, value = 0;
std::cout << "Starting..." << std::endl;
for(sum; std::cin >> value; sum+=value){
}
std::cout << "The sum of all your numbers typed in is: " << sum << std::endl;
return 0;
}
⚠️ Notice the for loop!!! Actually it makes sense once you take a breath and think. So the first argument sum
is just an initial, std::cin >> value
is always true until you exit, sum+=value
is the incrementation. Cooooooool!!!
Example 1.4.4
Although this is not an exercise, it still deserves for a note. The program is expected to do the following:
If I input 10 10 10 28 28
, it will prompt a message that 10 occurs 3 times
right after I typed 28
.
xxxxxxxxxx
using namespace std;
int main()
{
std::cout << "Starting..." << std::endl; //Start
int current_val, input_val; //Declare 2 values
int count_times = 0;
//current_val is the first parameter to feed which make sure we have something
if(std::cin >> current_val){
count_times = 1; //start counting
while(std::cin >> input_val){
if(input_val == current_val){
count_times++; //if the next input is ok, count++
}else{
//otherwise, print out what is the number and its count right now
std::cout << current_val << " occurs " << count_times << " times." << std::endl;
count_times = 1;
current_val = input_val;
}
}
//similar to the else block
std::cout << current_val << " occurs " << count_times << " times." << std::endl;
}
return 0;
}
Console:
xxxxxxxxxx
Starting...
1994
10
1994 occurs 1 times.
10
28
10 occurs 2 times.
28
28
28
28
28
^Z
28 occurs 6 times.
Process returned 0 (0x0) execution time : 26.917 s
Press any key to continue.
1.5 Introducing Classes
Exercise 1.20:
http://www.informit.com/title/0321714113 contains a copy of Sales_item.h
in the Chapter 1 code directory. Copy that file to your working directory. Use it to write a program that reads a set of book sales transactions, writing each transaction to the standard output.
xxxxxxxxxx
using namespace std;
using std::cin; //similar to python's "import xx as xx"
using std::cout;
using std::endl;
int main()
{
cout << "Starting..." << endl;
for (Sales_item item; cin >> item; cout << "Transaction: " << item << endl);
return 0;
}
Console:
xxxxxxxxxx
Starting...
0-201-78345-X
3
20.00
Transaction: 0-201-78345-X 3 60 20
9788842069102
2
35
Transaction: 9788842069102 2 70 35
^Z
Process returned 0 (0x0) execution time : 199.145 s
Press any key to continue.
Exercise 1.21:
Write a program that reads two Sales_item objects that have the same ISBN and produces their sum.
xxxxxxxxxx
using namespace std;
using std::cin;
using std::cout;
using std::endl;
int main()
{
cout << "First Transaction: " << endl;
Sales_item item1;
cin >> item1;
cout << "Second Transaction: " << endl;
Sales_item item2;
cin >> item2;
if(item1.isbn() == item2.isbn()){
cout<< item1+item2 << endl;
}
return 0;
}
Console:
xxxxxxxxxx
First Transaction:
1-86092-042-X
5
10.00
Second Transaction:
1-86092-042-X
10
12.00
1-86092-042-X 15 170 11.3333
Process returned 0 (0x0) execution time : 44.223 s
Press any key to continue.
⚠️ There is something missing from the above code: the else
code block.
Hence we should add the following:
xxxxxxxxxx
if(item1.isbn() == item2.isbn()){
cout << item1+item2 << endl;
return 0;
}else{
cerr << "The ISBN of transaction is different!";
return -1;
}
The full version should be:
xxxxxxxxxx
First Transaction:
1-86092-042-X
5
20.00
Second Transaction:
1-86092-004-7
3
15.00
The ISBN of transaction is different!
Process returned -1 (0xFFFFFFFF) execution time : 59.879 s
Press any key to continue.
1.6 The Bookstore Program
This is the code example of this section. However, I wrote something wrong which might be helpful for me to better understand and revise the code.
xxxxxxxxxx
using std::cout;
using std::cin;
using std::endl;
using std::cerr;
int main()
{
cout << "Transaction program starts... You can fill the data now: " << endl;
Sales_item total_item;
if(cin >> total_item){
int transaction_counts = 1;
Sales_item processing_item;
while(cin >> processing_item){
if(total_item.isbn() == processing_item.isbn()){
transaction_counts++;
}else{
total_item+=processing_item;
cout << "The transaction of " << total_item.isbn() << " occurs " << transaction_counts << " times." << endl;
transaction_counts = 1;
total_item = processing_item;
}
} cout << "The transaction of " << total_item.isbn() << " occurs " << transaction_counts << " times." << endl;
return 0;
}else{
cerr << "There is no transaction feeding in!" << endl;
return -1;
}
}
Console:
xxxxxxxxxx
Transaction program starts... You can fill the data now:
1-86092-042-X
10
25.00
1-86092-042-X
5
30.00
1-86092-004-7
20
15.00
The transaction of 1-86092-042-X occurs 2 times.
^Z
The transaction of 1-86092-004-7 occurs 1 times.
Process returned 0 (0x0) execution time : 41.402 s
Press any key to continue.
❌The return value of
0
should not be inside the outmostif
statement!! Instead, it should be after of theif
statement like:xxxxxxxxxx
if{
}
return 0;
- I did not use the
+
operator to sum the object ofSales
class.
However, it runs quite well. ✔️
The implementation should be:
xxxxxxxxxx
using std::cout;
using std::cin;
using std::endl;
using std::cerr;
int main()
{
cout << "Transaction program starts... You can fill the data now: " << endl;
Sales_item total_item;
if(cin >> total_item){
int transaction_counts = 1;
Sales_item processing_item;
while(cin >> processing_item){
if(total_item.isbn() == processing_item.isbn()){
total_item+=processing_item; //sum the record if ISBN is the same
}else{
cout << "The transaction record of " << total_item.isbn() << " is \n" << total_item << endl;
total_item = processing_item;
}
}
cout << "The transaction record of " << total_item.isbn() << " is \n" << total_item << endl; //print the last value once out of while loop
}else{
cerr << "There is no transaction feeding in!" << endl;
return -1;
}
return 0;
}
Console:
xxxxxxxxxx
Transaction program starts... You can fill the data now:
1-86092-042-X
10
15.00
1-86092-042-X
5
10.00
1-86092-004-7
20
5.00
The transaction record of 1-86092-042-X is
1-86092-042-X 15 200 13.3333
^Z
The transaction record of 1-86092-004-7 is
1-86092-004-7 20 100 5
Process returned 0 (0x0) execution time : 32.923 s
Press any key to continue.
That is the end of this chapter. Keep coding!