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 8672293
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T19:10:04+00:00 2026-06-12T19:10:04+00:00

So if i have a program that takes stdin such as 1 5 2

  • 0

So if i have a program that takes stdin such as

1
5
2
4

How exactly can i go through each line and say print that value, This is what im thinking:

#include <iostream>
using namespace std;

int main()
{

  while ( // input has ended// ) {
  cout << //current line//

  //increment to next line//

 }     

  return 0;
}

Is there such a way or no?

  • 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-12T19:10:05+00:00Added an answer on June 12, 2026 at 7:10 pm

    The pattern that I like is:

    while (!cin.eof())
    {
        string line;
        getline(cin, line);
    
        if (cin.fail())
        {
            //error
            break;
        }
    
        cout << line << endl;
    }
    

    As in the other answers, you can type CTRL+Z to send an EOF to STDIN. If STDIN is a pipe, then EOF will be sent when the stream has no more data.

    To save into a vector:

    vector<int> numbers;
    
    while (!cin.eof())
    {
        string line;
        getline(cin, line);
    
        if (cin.fail())
        {
            //error
            break;
        }
    
        cout << line << endl;
    
        istringstream iss(line);
        int num;
        iss >> num;
        numbers.push_back(num);
    }
    

    If you want a C-style array (although I would recommend std::vector:

    size_t START_SIZE = 100;
    
    size_t current_size = START_SIZE;
    size_t current_index = 0;
    
    int* numbers = new int[current_size];
    
    while (!cin.eof())
    {
        string line;
        getline(cin, line);
    
        if (cin.fail())
        {
            //error
            break;
        }
    
        cout << line << endl;
    
        if (current_index == current_size)
        {
            current_size += START_SIZE;
            int* tmp_arr = new int[current_size];
    
            for (size_t count = 0; count < current_index; count++)
            {
                tmp_arr[count] = numbers[count];
            }
    
            delete [] numbers;
            numbers = tmp_arr;
        }
    
        istringstream iss(line);
        int num;
        iss >> num;
    
        numbers[current_index] = num;
        current_index++;
    }
    
    delete [] numbers;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple command line c++ program shown below that takes stdin from
I have a program, that takes a long time to load. Because of this,
I have a command-line program which takes input from stdin. What's the best way
I have a command-line java program that takes a password and it's verification from
I have created this program that takes two inputs and prints them out (simple,
Question cribbed from here : I have a program that takes input from stdin
I have a python program that takes the md5 & sha1 hash values of
I have a perl program that takes input and output file arguments, and I'd
I have been assigned wit the task to write a program that takes a
I have a program that has this code : #include<stdio.h> main(){ int input; char

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.