Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Mar 12th, 2006, 5:15 PM   #1
TCStyle
Programmer
 
Join Date: Jan 2005
Location: Albany, NY
Posts: 43
Rep Power: 0 TCStyle is on a distinguished road
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...
TCStyle is offline   Reply With Quote
Old Mar 12th, 2006, 5:27 PM   #2
Jimbo
Battle Programmer
 
Jimbo's Avatar
 
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 763
Rep Power: 3 Jimbo is on a distinguished road
you have cin >> vale rather than cin >> value
Jimbo is offline   Reply With Quote
Old Mar 12th, 2006, 5:49 PM   #3
Jason Isom
Programmer
 
Join Date: Dec 2005
Posts: 53
Rep Power: 3 Jason Isom is on a distinguished road
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").
Jason Isom is offline   Reply With Quote
Old Mar 12th, 2006, 5:51 PM   #4
TCStyle
Programmer
 
Join Date: Jan 2005
Location: Albany, NY
Posts: 43
Rep Power: 0 TCStyle is on a distinguished road
To Jimbo:
That was just a typo when I re-wrote it.

To Jason:
All that does is break my app.
__________________
meh...
TCStyle is offline   Reply With Quote
Old Mar 12th, 2006, 5:53 PM   #5
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Old Mar 12th, 2006, 5:56 PM   #6
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,223
Rep Power: 5 grumpy is on a distinguished road
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?
grumpy is offline   Reply With Quote
Old Mar 12th, 2006, 7:22 PM   #7
Jason Isom
Programmer
 
Join Date: Dec 2005
Posts: 53
Rep Power: 3 Jason Isom is on a distinguished road
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?
Jason Isom is offline   Reply With Quote
Old Mar 12th, 2006, 7:39 PM   #8
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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.
__________________
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
DaWei is offline   Reply With Quote
Old Mar 13th, 2006, 5:28 AM   #9
nnxion
Programming Guru
 
nnxion's Avatar
 
Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5 nnxion is on a distinguished road
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.
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for."
-- Socrates
nnxion is offline   Reply With Quote
Old Mar 13th, 2006, 6:55 PM   #10
TCStyle
Programmer
 
Join Date: Jan 2005
Location: Albany, NY
Posts: 43
Rep Power: 0 TCStyle is on a distinguished road
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 7:11 PM.
TCStyle is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 10:33 PM.

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