Thread: input does not work properly

  1. #1
    Registered User
    Join Date
    Jul 2011
    Location
    Pune, Maharashtra, India
    Posts
    33

    input does not work properly

    Hello guys
    In the following code last input doesn't work
    And the next line output prints some negative garbage value
    why
    thank you
    Code:
    // Test.cpp : Defines the entry point for the console application.
    //
    
    #include "stdafx.h"
    #include <iostream>
    #include "conio.h"
    
    int _tmain(void)
    {
    	int sum= 0, val = 1 , a ;
    	while(std::cin >> val){
    						sum +=val;
    						++val;
    	}
    	std::cout << " Sum Of  Entery are " << sum << std::endl;
    	std::cin >> a ;// does not  take any value
    	std::cout<< a ;
    	_getch();
    	return 0;
    }

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Since the only way to exit your loop is to place the input stream into an error state you'll need to clear() the errors and empty the input buffer before you will be able to accept any more input from the stream.


    Jim

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. qsort() won't work properly..
    By gevni in forum C Programming
    Replies: 5
    Last Post: 03-21-2013, 12:02 PM
  2. trying to get a few functions to work properly...
    By lemonwaffles in forum C++ Programming
    Replies: 3
    Last Post: 07-15-2009, 11:00 PM
  3. Can't get fgets to work properly
    By lonkz in forum C Programming
    Replies: 18
    Last Post: 01-03-2009, 01:43 PM
  4. Can't Get This Program To Work Properly
    By jbyers19 in forum C Programming
    Replies: 5
    Last Post: 03-09-2006, 10:59 PM
  5. GetAsyncKeyState() won't work properly!
    By SyntaxBubble in forum Windows Programming
    Replies: 1
    Last Post: 04-30-2002, 03:03 PM