![]() |
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>When I run my application and enter values it doesn't get passed the while loop (it expects more values are to be entered). |
you have cin >> vale rather than cin >> value
|
Quote:
|
To Jimbo:
That was just a typo when I re-wrote it. To Jason: All that does is break my app. |
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.
|
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? |
Quote:
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? |
Quote:
Quote:
|
Quote:
Quote:
|
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