473,387 Members | 1,456 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

C++ Primer section 1.6 example

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

Jul 13 '07 #1
1 1282
arnuld <ge*********@gmail.comwrote:
#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.

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Jul 13 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: asdf | last post by:
any guys read C++ primer 4th edition? I think that there is a small mistake on the page 163. Where can I find the errata?
6
by: asdf | last post by:
how to use c_str() ? if I compile the following program, there are errors. #include<iostream> #include<fstream> void main() { ofstream outfiloe(ofile.c_str());
7
by: Lycan. Mao.. | last post by:
Hello, I am a newbie in C++ and I'm in trouble in choosing books, I hope some one who can give me some tips. I'm already know C and a little about Scheme, C#, Python, Lua and so on, and now I want...
1
by: pauldepstein | last post by:
I am reading Section 13.5 Managing Pointer Members of C++ Primer by Lippman, Lajoie and Moo Section 13.5.2 Defining Valuelike Classes Here is some of code to illustrate the value semantics...
2
by: W. Watson | last post by:
Is there a primer out there on these two items? I have the Python tutorial, but would like either a Tkinter tutorial/primer to supplement it, or a primer/tutorial that addresses both. Maybe there's...
11
by: arnuld | last post by:
i have solved the problem but it is quite length. as usual, i wanted some coding-standards or good-design opinions: /* C++ Primer 4/e * chapter 3 - Library Types * exercise 3.13 *...
20
by: arnuld | last post by:
I get an error, can't find what is the problem: /* C++ Primer - 4/e * * Chapter 8, exercise 8.3 * STATEMENT * write a function that takes and returns an istream&. the function should read...
2
by: xianwei | last post by:
First, typedef struct pair { Node *parent; Node *child; } Pair; static Pair SeekItem(cosnt Item *pI, const Tree *pTree) { Pair look;
1
by: Kveldulv | last post by:
Hi all, here is the code: http://pastebin.com/m6e74d36b I'm stuck at highfink constructor, last line before #endif. As parameters, I have reference to one parent class and int member of another...
1
by: mmdspam | last post by:
Hi, Can someone please tell me the answer to exercise 1.20 in C++ Primer 4th edition. I am stuck on this question. Thank you in advance
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.