Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8057999
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T09:12:53+00:00 2026-06-05T09:12:53+00:00

This code is from the book Data Structures and Problem Solving in C++(2003) void

  • 0

This code is from the book Data Structures and Problem Solving in C++(2003)

void getInts( vector<int> & array )
{

    int itemsRead = 0;
    int inputVal;

    cout << "Enter any number of integers: ";

    while( cin >> inputVal )
    {
        if( itemsRead == array.size( ) )
        array.resize( array.size( ) * 2 + 1 ) ;
        array[ itemsRead++ ] = inputVal;

    }

    array.resize( itemsRead ) ; 

}

int main()
{

    vector<int> array;

    getInts( array ) ;

    for( int i = 0; i < array.size( ) ; i++ )
        cout << array[ i ] << endl;

    return 0;

}

When I type a number in the console and press enter, the program does nothing. After that if I type CTRL+Z as EOF the program gives me the number that I typed. Since I’m resizing the vector and giving inputValue to each part of it, I expect to see many numbers instead of one, as much as the program generates before I type CTRL+Z, but that doesn’t happen. The vector’s size seems to be 1 after all. What is the reason for this?

Also my other question is, when I use the while loop as

while( cin >> inputVal )

Will the program work until I type CTRL+Z. Or typing 0 would do the same?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T09:12:55+00:00Added an answer on June 5, 2026 at 9:12 am

    The while loop will continue while cin can read integers from the terminal. It trys to read an integer every time you press enter.

    Each read integer is appended to the vector.

    If something other than an integer is entered, the fail bit on cin will be set.

    cin >> inputVal returns an istream, which provides operator void* (), which will return 0 if the fail bit is set.

    That’s why the while loop breaks out if you enter something other than an integer eg. ‘q’

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I get this code from a book but i could not get it work.
Note: This code actually codes from a tutorial book I'm reading at the moment,
I have the following piece of code from a book. There is this function
in the book i'm learning from i came across this code snippit: while (i
I'm trying to understand this code from Deitels 5th edition c book. If I'm
I have a simple code from a book and the code should display data
I came across the following piece of code in the book on data structures
hI, I'm trying to get this code from Larry Nyhoff's book to compile in
I'm downloaded the code from John Papa's book here: http://silverlight-data.com/ and am sucessfully running
This is exert from a book about data alignment of primitive types in memory.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.