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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T11:35:15+00:00 2026-06-02T11:35:15+00:00

I’m writing a template function that will check if the user has assigned the

  • 0

I’m writing a template function that will check if the user has assigned the correct type of data that the answer should be in. For example:

int main(){

    int E;
    cout<<"Enter an integer: "<<endl;
    E = clear_and_read<int>(cin);
    cout<<E<<endl;

}

where the function clear_and_read is defined as:

template <class T> T clear_and_read(istream& inputstream){
    cin.sync();//removes anything still in cin stream
    cin.clear();
    T input;
    inputstream>>input;       
    while(inputstream.fail()){
        cout<<endl<<"Please ensure you are inputting correct data type. Suggested data type: "<< typeid(input).name() <<"."<<endl;
        cin.sync();
        cin.clear();
        inputstream>>input;  
    }
    return input;
}

Now this works if I try to enter a string instead of an integer, but when I enter a double it just assigns it’s first value. e.g. 5.678 becomes 5.

What can I do inside the template function to flag if a double is being read into an int?

  • 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-02T11:35:18+00:00Added an answer on June 2, 2026 at 11:35 am

    I’d try a slightly different take than you have. Specifically, I would not try to modify the error state of the input stream:

    // Untested
    template <class T> T clear_and_read(istream& inputstream) {
      std::string inputString;
      while(1) {
        try {
    
          // Grab one maximally-sized whitespace-delimited chunk of input
          inputstream >> inputString;
    
          // Note: lexical_cast throws if there are any extraneous characters.
          return boost::lexical_cast<T>(inputString);
    
        } catch (boost::bad_cast&) {
          std::cout << "\nPlease ensure you are inputting correct data type. Suggested data type: "<< typeid(input).name() <<".\n";
        }
      }
    }
    

    Note: If you don’t have boost available in your compilation environment, lexical_cast is fairly trivial to implement yourself. If you get stuck, just ask for help.


    References:

    • http://www.boost.org/doc/libs/1_49_0/doc/html/boost_lexical_cast.html

    EDIT Here is a fully tested version, that does not rely upon Boost.

    #include <exception>
    #include <sstream>
    #include <string>
    #include <iostream>
    #include <typeinfo>
    
    template <class T> T clear_and_read(std::istream& inputstream) {
      std::string inputString;
      while(inputstream) {
          // Grab one maximally-sized whitespace-delimited chunk of input
          inputstream >> inputString;
          std::istringstream itemstream(inputString);
    
          // Convert it:
          T t;
          itemstream >> t;
    
          // See if conversion worked and consumed everything
          if(itemstream && (itemstream.get() == EOF)) {
            // SUCCESS
            return t;
          }
    
          // oops
          std::cout << "\nPlease ensure you are inputting correct data type. Suggested data type: "<< typeid(T).name() <<".\n";
      }
      std::cout << "What to do with EOF?\n";
      return T();
    }
    
    int main () {
      clear_and_read<int>(std::cin);
      clear_and_read<double>(std::cin);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I want to construct a data frame in an Rcpp function, but when I
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
Basically, what I'm trying to create is a page of div tags, each has
I have a French site that I want to parse, but am running into

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.