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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T15:15:47+00:00 2026-06-06T15:15:47+00:00

It might not be a bug, but I don’t know what is going wrong.

  • 0

It might not be a bug, but I don’t know what is going wrong.
My first entry is repeated for str1 on 2nd iteration, and is same way from then. Only first iteration goes good.

#include <iostream>
#include <string>
using namespace std;

int main () {

cout << " \n Enter two words. \n " ;
char c = 'y';
string str;
string str1;
while (c == 'y'){

    getline(cin,str);

    getline (cin,str1);

    cout << " \n\n str : " << str << " str1 : " << str1 ;
    cout << " \n Continue ? \n " ;
    cin >> c;
}

return 0;
}

The output is :

 Enter two words.
 hello world
this is mr


 str : hello world str1 : this is mr
 Continue ?
 y
hello world


 str :  str1 : hello world
 Continue ?
 n


  • 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-06T15:15:50+00:00Added an answer on June 6, 2026 at 3:15 pm

    Add

    cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );
    

    after your

    cin >> c;
    

    Consider the following input:

        dog
        cat
        y
        owl
        fish
        n
    

    If we examine the characters that are present in the input stream individually, we’ll see:

        d o g \n c a t \n y \n o w l \n f i s h \n n \n
    

    The first call to getline consumes dog\n; the second consumes cat\n, leaving this:

        y \n o w l \n f i s h \n n \n
    

    The first call to cin >> c consumes only y but not the subsequent newline, leaving this:

        \n o w l \n f i s h \n n \n
    

    Now, the fun begins: What happens during the next call to getline? Why it reads up to the next newline, of course. So the next call to getline returns an empty line, and leaves owl... in the input stream.

    The solution, as I outlined above, is to consume the remainder of the (now useless) input line.

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

Sidebar

Related Questions

I don't know if I'm doing something wrong, but I'm 100% sure it's the
This might not have a major usecase in projects, but I was just trying
The title might not explain my problem fully but I wasn't sure how to
I know what I asking might not make a lot of sense for C#
Reading the documentation it seems this might not be possible, but it seems that
A serious bug has popped up and I don't have a clue why. Not
This might not be possible but it would be hugely helpful with debugging. Sometimes
The question might not be clear, so i will explain further. I saw some
The tags might not be accurate since I am not sure where the problem
(I am not a native speaker and might not be correct in terms of

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.