![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programmer
Join Date: Jan 2005
Location: Albany, NY
Posts: 43
Rep Power: 0
![]() |
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).
__________________
meh... |
|
|
|
|
|
#2 |
|
Battle Programmer
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 769
Rep Power: 3
![]() |
you have cin >> vale rather than cin >> value
|
|
|
|
|
|
#3 | |
|
Programmer
Join Date: Dec 2005
Posts: 53
Rep Power: 3
![]() |
Quote:
|
|
|
|
|
|
|
#4 |
|
Programmer
Join Date: Jan 2005
Location: Albany, NY
Posts: 43
Rep Power: 0
![]() |
To Jimbo:
That was just a typo when I re-wrote it. To Jason: All that does is break my app.
__________________
meh... |
|
|
|
|
|
#5 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#6 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,254
Rep Power: 5
![]() |
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? |
|
|
|
|
|
#7 | |
|
Programmer
Join Date: Dec 2005
Posts: 53
Rep Power: 3
![]() |
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? |
|
|
|
|
|
|
#8 | ||
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Quote:
Quote:
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
||
|
|
|
|
|
#9 | ||
|
Programming Guru
![]() Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5
![]() |
Quote:
Quote:
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for." -- Socrates |
||
|
|
|
|
|
#10 |
|
Programmer
Join Date: Jan 2005
Location: Albany, NY
Posts: 43
Rep Power: 0
![]() |
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.
__________________
meh... Last edited by TCStyle; Mar 13th, 2006 at 8:11 PM. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|