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