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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T05:18:12+00:00 2026-05-23T05:18:12+00:00

I am writing a program that must print numbers at first which was entered

  • 0

I am writing a program that must print numbers at first which was entered at last.

So here is my code:

#include <iostream>
using namespace std;
void recursive()
{
    long long n;

     if((cin>>n)&&(!cin.eof())) // Dont work because enter is ignored in cmd window os 
          recursive();

    cout<<n<<endl;
    return;
}
int main()
{
    recursive();
    return 0;
}

Any idea how to stop function when nothing is input? Or how to solve problem in another way?

Thanks

  • 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-05-23T05:18:13+00:00Added an answer on May 23, 2026 at 5:18 am

    As your program is right now it will read the long long’s until cin’s operator void* indicates that the value read was not compatible (but still prints the n , filled with garbage,that it failed to read), as you notice it ignores the blank lines.

    If I understand correctly, what you want is that when the user inputs a blank line the program should output the numbers read, a simple way to do this would be to read the input as a string, then check the string’s length, and use a stringstream to convert it to the appropriate type.

    #include <iostream>
    #include <sstream>
    #include <string>
    void recursive()
    {
        std::string       input;
        long long         n;
    
        if ( std::getline(std::cin,input) && input.length() > 0 )
        {
            std::istringstream ss(input);
            if (ss >> n)
            {
                recursive();
                std::cout << n << std::endl;        
            }
        }
    }
    
    
    int main ()
    {
        recursive();
        return 0;
    }
    

    edit

    Here’s another way, if your input comes in the form 46 798 4163 4654 32132, it uses std::cin.peek() that returns the next character in the input without extracting it, the result of peek() is compared to ‘\n’ to test if the user pressed enter, and if that’s the case it prints the number it just read without waiting for the next number

    #include <iostream>
    #include <sstream>
    #include <string>
    
    void recursive_in_a_line ()
    {
        long long n;
        if (std::cin >> n)
        {
            if (std::cin.peek() != '\n')
            {
                recursive();
            }
            std::cout<< n << std::endl;
        }
    
    }        
    
    
    int main ()
    {
        recursive_in_a_line();
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing a program using the Sleuth Kit Library that is designed to
I am writing a program that must register time of starting a process such
I'm writing a shell program that must handle signals. My relevant signal handling related
I’m writing a small C program that must accept an input stream larger than
I am writing a program that must communicate with a PHP / web based
Currently I am writing a program that must iterate through and arraylist inside of
I am writing a program which write statistical tests in Delphi (must be Delphi)
Im writing a program that should read input via stdin, so I have the
I'm writing a program that sends an email out at a client's specific local
If you are writing a program that is executable from the command line, you

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.