Reading an Unknown Number of Inputs

This is a discussion on Reading an Unknown Number of Inputs within the C++ Programming forums, part of the General Programming Boards category; I am new to C++, and I have been reading "C++ Primer, 4th Edition" to help me learn. I do ...

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    7

    Question Reading an Unknown Number of Inputs

    I am new to C++, and I have been reading "C++ Primer, 4th Edition" to help me learn. I do the examples in the book to help me practice. This part of the chapter is covering control structures, but I can't get one of the examples to work for me.

    The code is suppose to take any values you input (regardless of quantity) and generate their collective sum. I wrote it correctly, it complied just fine.

    I input integers but it doesn't give me the sum, and the program will not stop normally unless I ex out the window or I input something that is a non integer.


    Code:
    #include <iostream>    
    int main()    
    {        
         int sum = 0, value;      
         // read till end-of-file, calculating a running total of all values read       
         while (std::cin >> value)           
              sum += value; // equivalent to sum = sum + value        
         std::cout << "Sum is: " << sum << std::endl;        
         return 0;     
    }

    Someone did post on programmersheaven.com forum about this same exact code a few years back, however it didn't help me much or maybe it isn't relevant anymore (some nonsense about ctrl+z, idk).

    It would be great if I can get a response soon. I am using Microsoft Visual C++ 2010 IDE.

  2. #2
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,490
    Quote Originally Posted by Aeias View Post
    (some nonsense about ctrl+z, idk).
    .
    Not nonsense at all, it is called the End of File character. It is different for me though (Ctrl+D).

    I did not test it, but the loop should also stop if you enter something other than a number...say a character.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    7
    Yes, but for me all ctrl+z dose is undo.
    I just don't know what to do, can you explain a little more.

    I am just not sure. Maybe it is because I am using MS Visual C++ 2010.
    Last edited by Aeias; 02-23-2012 at 03:20 PM. Reason: I had more to say.

  4. #4
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,490
    Quote Originally Posted by Aeias View Post
    Yes, but for me all ctrl+z dose is undo.

    Maybe it is because I am using MS Visual C++ 2010.
    :doh:
    Press it when running the binary, not when editing the code.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  5. #5
    Registered User
    Join Date
    Nov 2011
    Posts
    7
    I have done that also.

    What do you mean by, running the binary?

    (sorry Iam very new)

  6. #6
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,490
    Quote Originally Posted by Aeias View Post
    I have done that also.

    What do you mean by, running the binary?

    (sorry Iam very new)
    When you run the program ..and are supposed to enter a number, either enter something not a number, or possibly press Ctrl Z.
    That should stop the loop and print out the sum of the numbers you've already entered.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  7. #7
    Registered User
    Join Date
    Nov 2011
    Posts
    7
    Quote Originally Posted by manasij7479 View Post
    When you run the program ..and are supposed to enter a number, either enter something not a number, or possibly press Ctrl Z.
    That should stop the loop and print out the sum of the numbers you've already entered.
    I have done that, however all that appears in the command window is "^z", and if I press enter the program ends. And if I am not selected on the command prompt window, I hear a loud beep noise. I don't understand exactly how I am suppose to press ctrl+z.

  8. #8
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,490
    That is supposed to happen, as the program is ending before you can see the output.
    Try adding the line std::cin.get(); before the return statement.
    If that does not work, try one of the methods here: http://faq.cprogramming.com/cgi-bin/...&id=1043284385
    Last edited by manasij7479; 02-23-2012 at 03:39 PM.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  9. #9
    Registered User
    Join Date
    Nov 2011
    Posts
    7
    ok, I was using

    char response;
    std::cin >> response;

    before the return.
    However I am getting the same result, I type in some integers, I hit ctrl+z I see ^z in the command prompt. Nothing appears, so i hit enter and the console command window closes.

  10. #10
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,490
    It works fine for me.
    [manasij7479@manasijd ~]$ ./a.out
    3
    4
    3
    Sum is: 10
    [manasij7479@manasijd ~]$
    Try entering a letter instead of ^Z and report what happens.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  11. #11
    Registered User
    Join Date
    Nov 2011
    Posts
    7
    I have tried several letters just a few like A,F,C,T

    the only one that did something a little different was ctrl+c which just closed the program sooner.

    What compiler or IDE do you use?

  12. #12
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,490
    Ctrl+C should close the program on spot.
    But I am puzzled that the eof character or anything other a number does not end the loop and print the sum.
    Do exactly what I'm writing:
    Run the program
    Enter 5 . Press Enter
    Enter a . Press Enter

    What happens after that ?

    (I'm on a different OS, so stuff regarding eof will be different.)
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  13. #13
    Registered User
    Join Date
    Nov 2011
    Posts
    7
    It was but for whatever reason, the program was not waiting for input so I went into the FAQ you linked me and used

    system ("PAUSE")

    after return

    it worked this time.

    Thank you so much. I felt like I couldn't move on till I figured it out.

    that enter "a" thing you just suggested didn't do anything different.

    Again thank you so much, usually

    char response;
    cin >> response;

    works
    Last edited by Aeias; 02-23-2012 at 04:04 PM.

  14. #14
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,165
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems Reading Line With Unknown Number of Integers
    By envec83 in forum C Programming
    Replies: 3
    Last Post: 09-27-2011, 11:20 AM
  2. Reading an unknown number of data points from a table
    By Jonnyfurze in forum C Programming
    Replies: 2
    Last Post: 09-22-2011, 12:23 PM
  3. converting two keypad inputs into one decimal number
    By volkvanmyn25 in forum C Programming
    Replies: 1
    Last Post: 09-16-2010, 09:09 AM
  4. How to reprompt after user inputs wrong number?
    By gnozahs in forum C Programming
    Replies: 11
    Last Post: 10-03-2009, 03:49 PM
  5. Reading in an unknown number of numbers
    By cboard_member in forum C++ Programming
    Replies: 3
    Last Post: 02-01-2006, 03:21 AM

Tags for this Thread


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