problem writing loop condition

N

nvangogh

Hi, I have a program (an exercise from C++ primer) which should be
really easy, but I cannot work out the solution.

The program should take input until control-d is hit. It should continue
to read transactions so long as the user inputs them.

This is the program but it only takes one transaction. In an earlier
program I was able to use while(std::cin >> book) as the loop condition.
But this condition does not work with this program.

Can you tell me what you think?

#include <iostream>
#include <string>

struct Sales_data
{
std::string bookNo;
unsigned units_sold = 0;
double revenue = 0.0;
};

int main()
{
Sales_data item;
// I want to loop this - what condition can i use?
std::cout << "Input ISBN: Units Sold: Price per unit" << std::endl;
std::cin >> item.bookNo >> item.units_sold >> item.revenue;
std::cout << item.bookNo << '\t' << item.units_sold << '\t' <<
item.revenue * item.units_sold << std::endl;

return 0;
}
 
V

Victor Bazarov

Hi, I have a program (an exercise from C++ primer) which should be
really easy, but I cannot work out the solution.

The program should take input until control-d is hit. It should continue
to read transactions so long as the user inputs them.

This is the program but it only takes one transaction. In an earlier
program I was able to use while(std::cin >> book) as the loop condition.
But this condition does not work with this program.

Can you tell me what you think?

I can. I think you need to familiarize yourself with some features of
your operating system. On MS Windows, for instance, Ctrl-D will give
you nothing special in the input, but on UN*X, IIRC, it will make the
standard input end with "end of file" condition. So, if you're on UN*X
(or Linux, for instance), you could loop until the end-of-file is
encountered on 'cout'.

BTW, to get the EOF on standard input on MS Windows, enter Ctrl-Z.
#include <iostream>
#include <string>

struct Sales_data
{
std::string bookNo;
unsigned units_sold = 0;
double revenue = 0.0;
};

int main()
{
Sales_data item;
// I want to loop this - what condition can i use?
std::cout << "Input ISBN: Units Sold: Price per unit" << std::endl;
std::cin >> item.bookNo >> item.units_sold >> item.revenue;
std::cout << item.bookNo << '\t' << item.units_sold << '\t' <<
item.revenue * item.units_sold << std::endl;

return 0;
}

V
 
N

nvangogh

I can. I think you need to familiarize yourself with some features of
your operating system. On MS Windows, for instance, Ctrl-D will give
you nothing special in the input, but on UN*X, IIRC, it will make the
standard input end with "end of file" condition. So, if you're on UN*X
(or Linux, for instance), you could loop until the end-of-file is
encountered on 'cout'.

BTW, to get the EOF on standard input on MS Windows, enter Ctrl-Z.


V
ok but what is the loop condition?
while(std::cin >> item) does not compile.
 
I

Ian Collins

ok but what is the loop condition?
while(std::cin >> item) does not compile.

It's not the loop condition you are missing, it's an input operator for
Sales_data.
 
N

nvangogh

It's not the loop condition you are missing, it's an input operator for
Sales_data.
How do I make the struct have an input operator? I'm not sure this
exercise is supposed to be this complicated as it is only chapter 2.
 
V

Victor Bazarov

How do I make the struct have an input operator? I'm not sure this
exercise is supposed to be this complicated as it is only chapter 2.

RTFM, namely see member functions of 'istream'. There is probably a
couple of them that could be useful as a condition for continuing the
input. Something like

while (std::cin . <some function here> ()) { ...
^^^^^^^^^^^^^^^^^^^^

And since 'std::cin >> item.bookNo' compiles, you could try using that
as a condition. It returns the reference to the input stream itself,
and the stream class has operator void*, if memory serves me, which can
be used where a logical expression is expected...

V
 
B

Barry Schwarz

How do I make the struct have an input operator? I'm not sure this
exercise is supposed to be this complicated as it is only chapter 2.

It does seem unlikely at this early stage that the book wants you to
create a >> input operator for the structure. It is also unseemly for
a supposed C++ primer to assume you are using a Unix system.

Why did you put the three variables in a structure? Do you ever
operate on the three as a group?

What happens if you code the input statement to accept three values as
opposed to trying to process the structure as a monolithic unit?

Can you loop on the status reported by cin.eof()?
 
N

nvangogh

On Tue, 01 Apr 2014 22:00:43 +0100 in comp.lang.c++, nvangogh


Without going that far, one step in that direction might be

while (std::cin >> item.bookNo >> item.units_sold >> item.revenue)
{
std::cout << item.bookNo << '\t' << item.units_sold << '\t'
<< item.revenue * item.units_sold << std::endl;
}
Thank you - I think this is the expected solution at this stage.
 
N

nvangogh

It does seem unlikely at this early stage that the book wants you to
create a >> input operator for the structure. It is also unseemly for
a supposed C++ primer to assume you are using a Unix system.

Why did you put the three variables in a structure? Do you ever
operate on the three as a group?
Thanks brother - the three variable struct is what the book demonstrates.
What happens if you code the input statement to accept three values as
opposed to trying to process the structure as a monolithic unit?

Can you loop on the status reported by cin.eof()?
cin.eof() might work but it had not been introduced as yet.
 
N

nvangogh

On Wed, 02 Apr 2014 14:26:28 +0100 in comp.lang.c++, nvangogh


Looping on cin.eof() is almost always the wrong thing to do.
.eof() does not necessarily tell if the previous operation failed.
It doesn't predict if the next one will succeed. And it doesn't
tell you anything about other reasons for failure.

The simple answer is, loop until the input operation fails. Then
look at .eof() to see if that was the reason for the failure, if you
still care.
Thank you - this helps, though the solution was in your earlier suggestion.
 

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

Cannot find my infinite loop 1
Infinite loop problem 1
Character operations in C++ 2
Codeforces problem 0
Remove Space, Stuck on lab 1
TF-IDF 1
reading and writing a container of std::pair type 6
Crossword 14

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top