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

  • Home
  • SEARCH
  • 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 8629243
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T08:45:17+00:00 2026-06-12T08:45:17+00:00

Possible Duplicate: Is this a bug with getline(), or am I doing something wrong.

  • 0

Possible Duplicate:
Is this a bug with getline(), or am I doing something wrong. Right way to use getline()?

I tried to learn this topic on STL lists and Strings. So as an integration, i tried out this program:

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

int main(){
    list<string> obj1, obj2;
    string obj;
    int n;
    cout<<"Enter the number of elements in string list 1:\t";
    cin>>n;
    cin.clear();
    cout<<"Enter the string:\n";
    for( int i=0; i<n; i++){
        getline(cin, obj);
        cout<<"The string is:\t"<<obj<<" and i is "<<i<<endl;
        obj1.push_back(obj);
    }
    obj1.sort();
    cout<<"The string in sorted order is:\n";
    list<string>::reverse_iterator rit;
    for( rit = obj1.rbegin(); rit != obj1.rend(); rit++)
        cout<<*rit<<endl;
    return 0;
}

I get the following output:

Enter the number of elements in string list 1:  4
Enter the string:
The string is:   and i is 0
goat
The string is:  goat and i is 1
boat
The string is:  boat and i is 2
toad
The string is:  toad and i is 3
The string in sorted order is:
toad
goat
boat

The error in the program is that the first string is a blank one that is automatically inserted into the list. In order to avoid this, i tried using cin.clear() but i am not able to overcome the error. Can anyone please identify the bug and help me with the answer.

  • 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-12T08:45:19+00:00Added an answer on June 12, 2026 at 8:45 am

    One must take special care when using operator>> and getline in the same program. The operator>> leaves an end-of-line indicator in the input stream, which getline accepts.

    Try adding std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n') before the getline.

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

Sidebar

Related Questions

Possible Duplicate: Is this a good way to generate a string of random characters?
Possible Duplicate: Why does this intentionally incorrect use of strcpy not fail horribly? Below
Possible Duplicate: Floating point inaccuracy examples PHP rounding issue - Is this a bug?
Possible Duplicate: Workarounds for JavaScript parseInt octal bug I would think it has something
Possible Duplicate: Why is this microtime showing up weird in PHP I use this
Possible Duplicate: Workarounds for JavaScript parseInt octal bug Given this script: <script type=text/javascript> var
Possible Duplicate: Workarounds for JavaScript parseInt octal bug well i have this http://jsfiddle.net/gMDfk/1/ The
Possible Duplicate: How does this CSS triangle shape work? Please help me i need
Possible Duplicate: Why do this Ruby object have two to_s and inspect methods that
Possible Duplicate: Why is this code invalid in C#? Conditional operator cannot cast implicitly?

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.