CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Sep 2003
    Location
    In a swamp (sort-of) in North Carolina
    Posts
    11

    simple console i/o question

    Given these requirements for a console application:
    Enter a series of integers seperated by whitespace. The first integer represents the quantity of integers to follow.
    Ex. 3 5 18 21
    or. 5 17 -46 3 198 2

    I have been attempting the following to read the integers and store them in a vector:
    scanf("%d", &cnt);
    for(int i = 1; i <= cnt + 1; i++)
    {
    scanf("%d", &n);
    aInts.push_back(n);
    }

    This works fine, unless someone enters fewer integers than the initial integer indicates. (ex. 3 19 5)
    How can I test to see if the end of the input stream has been reached?

    I have attempted the same type of logic using cin, to no avail.

    Thank you in advance for your help.

  2. #2
    Join Date
    Dec 2002
    Posts
    47
    Roger - your question contains a paradox.

    A program that accepts input must wait for the user to take action. In the case of text, the program needs to know when the user's input is complete.

    If the user can enter a variable number of arguements, then why have them specify the number of arguements to begin with? Instead, have the user type something to indicate they are done typing.

    This could be a period, semi-colon, special keyword, or a blank line.

    Ex: 5 17 -46 3 198 2 ;

    -rick

  3. #3
    Join Date
    Jul 2003
    Location
    Maryland
    Posts
    762
    You can use getline and use " " as a delimiter to seperate each into a part of a vector

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured