C++ Primer section 1.6 example

A

arnuld

STATEMENT: there is a book store which records the selling of books
like this:

(ISBN) (number of books sold) (price of a book)

the shop-owner then computes the total number of books sold and then
average price of each book. here is the programme (we assume that all
books with same ISBN appear together in the input):


#include <iostream>
#include "Sales_item.hpp"

int main()
{
Sales_item total, trans;

std::cin >> total;

while(std::cin >> trans)
{
if(total.same_isbn(trans))
total = total + trans;
else
{
std::cout << total << std::endl;
total = trans;
}

}

std::cout << total << std::endl;

return 0;
}


------------- OUTPUT -------------------------
[arnuld@arch cpp] $ g++ -ansi -pedantic -Wall -Wextra 1.6.cpp
[arnuld@arch cpp] $ ./a.out
0-210-x 3 10.0
0-210-x 1 20.0
0-210-y 1 30.0
0-210-x 4 50 12.5
0-210-y 1 30 30
[arnuld@arch cpp] $


i only want to know why the author included the "if" loop at the
beginning when problem can be solved without this:

#include <iostream>
#include "Sales_item.hpp"

int main()
{
Sales_item total, trans;

std::cin >> total;

while(std::cin >> trans)
{
if(total.same_isbn(trans))
total = total + trans;
else
{
std::cout << total << std::endl;
total = trans;
}

}

std::cout << total << std::endl;

return 0;
}

------------- OUTPUT (Exactly same as earlier)------------
[arnuld@arch cpp] $ ./a.out
0-210-x 3 10.0
0-210-x 1 20.0
0-210-y 1 30.0
0-210-x 4 50 12.5
0-210-y 1 30 30
[arnuld@arch cpp] $


output is only different at one place, when we do not enter any data
and simply hit EOF. but that kind of thing does not matter at the
beginning level of C++.
 
M

Marcus Kwok

arnuld said:
#include <iostream>
#include "Sales_item.hpp"

int main()
{
Sales_item total, trans;

std::cin >> total;

while(std::cin >> trans)
{
if(total.same_isbn(trans))
total = total + trans;
else
{
std::cout << total << std::endl;
total = trans;
}

}

std::cout << total << std::endl;

return 0;
}
[snip]

i only want to know why the author included the "if" loop at the
beginning when problem can be solved without this:

#include <iostream>
#include "Sales_item.hpp"

int main()
{
Sales_item total, trans;

std::cin >> total;

while(std::cin >> trans)
{
if(total.same_isbn(trans))
total = total + trans;
else
{
std::cout << total << std::endl;
total = trans;
}

}

std::cout << total << std::endl;

return 0;
}

Both of these snippets look identical to me.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Character operations in C++ 2
C++ Primer ex 7.5 18
C++ Primer ex 7.6 17
C++ Primer ex 3.14 8
C++ Primer exercise 3.13 17
C++ Primer ex 4.16 2
Lexical Analysis on C++ 1
CIN Input #2 gets skipped, I don't understand why. 1

Members online

Forum statistics

Threads
473,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top