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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T16:59:10+00:00 2026-05-26T16:59:10+00:00

if I don’t have istring.clear.() in my code, the output would be nan%. everything

  • 0

if I don’t have istring.clear.() in my code, the output would be “nan%”. everything works well and output is 60% if it’s there. What does it really do there? why it makes a difference? (p.s my input is “y n y n y”)

#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
using namespace std;
//inline function
inline ifstream& read(ifstream& is, string f_name);
//main function
int main ()
{
    string f_name=("dcl");
    ifstream readfile;
    read(readfile, f_name);
    string temp, word;
    istringstream istring;
    double counter=0.0, total=0.0;
    while(getline(readfile,temp))
    {
        istring.str(temp);
        while(istring>>word)
        {
            if(word=="y")
                ++counter;
            if(word=="n" || word=="y")
                ++total;
        }
        istring.clear();
    }
    double factor=counter/total*100;
    cout<<factor<<"%"<<endl;
    return 0;   
}

inline ifstream& read(ifstream& is, string f_name)
{
    is.close();
    is.clear();
    is.open(f_name.c_str());
    return is;
}
  • 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-26T16:59:11+00:00Added an answer on May 26, 2026 at 4:59 pm

    clear() resets the error flags on a stream (as you can read in the documentation). If you use formatted extraction, then the error flag “fail” will be set if an extraction fails (e.g. if you’re trying to read an integer and there isn’t anything parsable). So if you’re using the error state to terminate the loop, you have to make the stream usable again before going into the next loop.

    In your particular case, though, your code is just poorly written and violates the “maximum locality principle”. A saner version, that as a bonus doesn’t require clear(), would be like this:

    std::string temp;
    while (std::getline(readfile, temp))
    {
      std::istringstream iss(temp);
      std::string word;
    
      while (iss >> word)
      {
          std::cout << word << "_" << std::endl;
          if (word == "y") ++counter;
          if (word == "y") ++total;
      }
    }
    

    Some people would even write the outer loop as for (std::string temp; std::getline(readfile, temp); ) { /* ... */ }, though others consider this abuse.

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

Sidebar

Related Questions

Don't let below code scare you away . The question is really simple, only
Don't they both have to convert to machine code at some point to execute
Don't overthink this - there's a very commonly used term and I ... have
Don't think there's much to say, here's my code for (int i = 0;
Don't know the term for the red validation border, does it have one? I
Don't you hate it when you have class Foobar { public: Something& getSomething(int index)
Don't be scared of the extensive code. The problem is general. I just provided
Don't dismiss this as a newbie question! It's not, I'm not, I've tried everything,
Don't really know how to formulate the title, but it should be pretty obvious
Don't know how to google for such, but is there a way to query

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.