Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C++ (http://www.programmingforums.org/forum15.html)
-   -   Problem reading unknown # of inputs (http://www.programmingforums.org/showthread.php?t=8830)

TCStyle Mar 12th, 2006 6:15 PM

Problem reading unknown # of inputs
 
I brought C++ primer fourth additon yestarday and I've already ran into a problem on page 19. There is a sample program that adds up an unkown number of inputs and then outputs that number. This sample concept is used in several other chapters/examples but I doesn't seem to work for me. The code:
:

#include <iostream>
using namespace std;

int main()
{
int sum = 0, value;

while (cin >> vale) {
sum += value;
}
cout << "Sum is: " << sum << endl;
return 0;
}


When I run my application and enter values it doesn't get passed the while loop (it expects more values are to be entered).

Jimbo Mar 12th, 2006 6:27 PM

you have cin >> vale rather than cin >> value

Jason Isom Mar 12th, 2006 6:49 PM

Quote:

Originally Posted by TCStyle
When I run my application and enter values it doesn't get passed the while loop (it expects more values are to be entered).

Your program is set to run until "cin >> value" fails. You can make it fail by passing something other than an int, so when you're done entering values try typing "quit" (or just "q").

TCStyle Mar 12th, 2006 6:51 PM

To Jimbo:
That was just a typo when I re-wrote it.

To Jason:
All that does is break my app.

DaWei Mar 12th, 2006 6:53 PM

I wrote an example in the last couple of months illustrating the input of multiple values (of differing types), whether they occurred on one line, or on a series of lines. If you have any interest in that sort of thing, a search should turn it up. It would be in the C or C++ forum.

grumpy Mar 12th, 2006 6:56 PM

The only way your loop with finish is if an error occurs on cin (eg an end of file is encountered). One characteristic of cin (when reading from the keyboard) is that it will wait for input if none is there. Which effectively means you have an infinite loop unless the user triggers an error condition.

Essentially, you need to define a better condition for ending your loop. For example, do you want the loop to terminate in response to some action by the user, or to terminate after a specified number of iterations?

Jason Isom Mar 12th, 2006 8:22 PM

Quote:

Originally Posted by TCStyle
To Jason:
All that does is break my app.

What do you mean it breaks your app? Breaks out of the loop sounds more likely, but I admittingly haven't used C++ for some time now.

As Grumpy already pointed out, the loop will occur until std::cin fails and by inputting something other than an int should cause such a condition.

So I ask again, what do you mean it breaks your app? What compiler are you using?

DaWei Mar 12th, 2006 8:39 PM

Quote:

You can make it fail by passing something other than an int, so when you're done entering values try typing "quit" (or just "q").
Quote:

So I ask again, what do you mean it breaks your app? What compiler are you using?
He means precisely what he's saying. If one enters a non-integer when the input method used is expected to perform a conversion, the stream will break. It won't function properly again until it is repaired. Subsequent calls, of whatever kind, will simply zoop right on by the call without performing. The failure is quite common, particularly among novices. Checking the stream status with such things as good (), fail (), bad (), and fixing the problems with clear () or clearing out unused crap in the buffer are all provided simply because the stream is a mechanism and not a psychic miracle worker.

nnxion Mar 13th, 2006 6:28 AM

Quote:

Originally Posted by DaWei
I wrote an example in the last couple of months illustrating the input of multiple values (of differing types), whether they occurred on one line, or on a series of lines. If you have any interest in that sort of thing, a search should turn it up. It would be in the C or C++ forum.

DaWei means this post.

Quote:

Originally Posted by DaWei
simply because the stream is a mechanism and not a psychic miracle worker.

That reminds me, I need to get me one of those psychic micacle workers, maybe he/she can make me a billionaire so I can quit working and go hobbying around.

TCStyle Mar 13th, 2006 7:55 PM

DaWei I read your post but it's alittle over my head. I'm still stuck on his problem and its probably best if I rephrase my question.

I understand that the code isn't working becuase the while loops is continuously proven false. So, assuming this is indeed the problem, I need to have the while loop proven false when the enter key is pressed, and after the while loop has executed sucessfully.

Could this possibly be a problem do to the use of different compilers? I can't imagine someone would write a book without troubleshooting the codes.


All times are GMT -5. The time now is 9:30 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC