Like Tree1Likes
  • 1 Post By jimblumberg

input does not work properly

This is a discussion on input does not work properly within the C++ Programming forums, part of the General Programming Boards category; Hello guys In the following code last input doesn't work And the next line output prints some negative garbage value ...

  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
    2,755
    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
    mohsen likes this.

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, 12:43 PM
  4. Can't Get This Program To Work Properly
    By jbyers19 in forum C Programming
    Replies: 5
    Last Post: 03-09-2006, 09:59 PM
  5. GetAsyncKeyState() won't work properly!
    By SyntaxBubble in forum Windows Programming
    Replies: 1
    Last Post: 04-30-2002, 03:03 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21