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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T11:40:11+00:00 2026-05-30T11:40:11+00:00

consider this code snippet void make(int n) { std::string user_input; std::istringstream iss(user_input); char letter;

  • 0

consider this code snippet

   void make(int n)
   {
     std::string user_input;
     std::istringstream iss(user_input);
     char letter;
     int index;
     while(n>0)
    { cout<<n<<endl;
      std::getline(std::cin, user_input);
      while (iss >> letter >> index)
      cout<<letter<<' '<<index;
      n--;
    }
   }  
   int main()
   { int n;
     cin>>n;
     make(n);
     return 0; 
   }

here loop is not running correctly
if i put n=5 then output is

5
//getline doesn’t work
4
then getline works… why this

  • 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-30T11:40:13+00:00Added an answer on May 30, 2026 at 11:40 am

    The reason why it’s doing this, is because when you’re calling getline(), it’s taking what’s in cin and putting it in the variable. However, when you initially called cin to take in the inital input, the newline character remained in the buffer after going into your make() function.

    So when you enter the loop, the first getline() takes '\n' from cin, and the buffer was cleared. That’s why it seems to “skip” the first iteration as it would seem.

    So in order to get it to function correctly, you should clear your input buffer when you call your function using cin.ignore(), like so:

     void make(int n)
     {
     cin.ignore(1000,'\n'); //ignores 1000 characters or until sees \n
     std::string user_input;
     std::istringstream iss(user_input);
     char letter;
     int index;
     while(n>0)
    { cout<<n<<endl;
      std::getline(std::cin, user_input);
      while (iss >> letter >> index)
      cout<<letter<<' '<<index;
      n--;
    }
    }  
    int main()
    { int n;
      cin>>n;
      make(n);
      return 0; 
     }
    

    I haven’t used c++ much recently, so at the moment, i’m not sure if there’s a better way to handle this, but this should give you a good direction.

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

Sidebar

Related Questions

Consider this code snippet: public static void main(String[] args) { int z1 = 0;
Consider this code snippet: class Program { static void Main(string[] args) { Console.WriteLine(Test().ToString()); }
Let’s consider this very short snippet of code: #include <stdlib.h> int main() { char*
Consider this code: int main() { int e; prn(e); return 0; } void prn(double
Consider this code: void res(int a,int n) { printf(%d %d, ,a,n); } void main(void)
Consider the following code snippet: String input = Print this; System.out.println(input.matches(\\bthis\\b)); Output false What
Consider this piece of code: struct Trade { float Price; char* time; int shares;
Consider the following code snippet private void ProcessFile(string fullPath) { XmlTextReader rdr = new
Please consider this code snippet: private static void doSomething(Double avg, Double min, Double sd)
Please consider this code snippet: var ul = $(.list_b).find(li).remove().end(); $.each(Sites, function (index, value) {

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.