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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T13:19:28+00:00 2026-05-12T13:19:28+00:00

I am trying to write an if statement but cannot find the proper expression

  • 0

I am trying to write an if statement but cannot find the proper expression form to use. I’m thinking of writing something like this:

if ( var != type(int) )

However, I am unsure exactly how to go about doing this, and this method does not work.

Am I at least thinking along the right lines?

  • 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-12T13:19:28+00:00Added an answer on May 12, 2026 at 1:19 pm

    It sounds like you’re trying to overload a function:

    void foo(int i)
    {
        // stuff
    }
    
    void foo(float f)
    {
        // stuff
    }
    
    int main(void)
    {
        int i = 10;
        float f = 1.0f;
    
        foo(i); // calls foo(int)
        foo(f); // calls foo(float)
    }
    

    If you want int-special behavior and then something else in all other cases, you can use templates:

    template <typename T>
    void foo(T t)
    {
        // T is something
    }
    
    template <>
    void foo(int i)
    {
        // for int's only
    }
    
    int main(void)
    {
        int i = 10;
        float f = 1.0f;
        double d = 2.0;
    
        foo(i); // calls specialized foo
        foo(f); // calls generic foo
        foo(d); // calls generic foo
    }
    

    According to your comment (“Task at hand is a simple program: Take two user inputted integers and add them. Restrict input to integer only. I can do it in Python and I am thinking too along those lines. if num1 != type(int): print “You did not enter an integer, please enter a integer.” else: continue”), you want something like this:

    #include <iostream>
    
    int main(void)
    {
        int i;
        std::cin >> i;
    
        if (std::cin.fail())
        {
            std::cout << "Not valid!" << std::endl;
        }
        else
        {
            // ...
        }
    }
    

    This will notify invalid input such as “@#$”, “r13”, but does not catch cases such as “34fg”, “12$#%”, because it will read the int, and stop at “fg” and “$#%”, respectively.

    To check that, you will have to read in a line of input, and then try to convert that line into the type you want. (Thanks, litb). That means your question is more like this question:

    #include <iostream>
    #include <sstream>
    #include <string>
    
    int main(void)
    {
        std::string input;
        std::getline(std::cin, input);
    
        std::stringstream ss(input);
        int i;
        ss >> i;
    
        if (ss.fail() || !(ss >> std::ws).eof())
        {
            std::cout << "Not valid!" << std::endl;
        }
        else
        {
            // ...
        }
    }
    

    This does the following: get input, and put it into a stringstream. Then after parsing the int, stream out any remaining white space. After that, if eof is false, this means there are left-over characters; the input was invalid.

    This is much easier to use wrapped in a function. In the other question, the cast was re-factored away; in this question we’re using the cast, but wrapping the input along with it.

    #include <iostream>
    #include <sstream>
    #include <string>
    
    bool parse_int(int& i)
    {
        std::string input;
        std::getline(std::cin, input);
    
        std::stringstream ss(input);
        ss >> i;
    
        return !(ss.fail() || !(ss >> std::ws).eof());
    }
    
    int main(void)
    {
        int i;
    
        if (!parse_int(i))
        {
            std::cout << "Not valid!" << std::endl;
        }
        else
        {
            // ...
        }
    }
    

    Or more generically:

    #include <iostream>
    #include <sstream>
    #include <string>
    
    template <typename T>
    bool parse_type(T& t)
    {
        std::string input;
        std::getline(std::cin, input);
    
        std::stringstream ss(input);
        ss >> t;
    
        return !(ss.fail() || !(ss >> std::ws).eof());
    }
    
    int main(void)
    {
        int i;
    
        if (!parse_type(i))
        {
            std::cout << "Not valid!" << std::endl;
        }
        else
        {
            // ...
        }
    }
    

    This let’s you parse other types with error checking.


    If you’re okay with exceptions, using lexical_cast (either from boost, or “faked”, see the other question linked in-code [same as above link]), your code would look something like this:

    #include <iostream>
    #include <sstream>
    #include <string>
    
    /* Faked lexical-cast from question:
    https://stackoverflow.com/questions/1243428/convert-string-to-int-with-bool-fail-in-c/
    */
    template <typename T>
    T lexical_cast(const std::string& s)
    {
        std::stringstream ss(s);
    
        T result;
        if ((ss >> result).fail() || !(ss >> std::ws).eof())
        {
            throw std::bad_cast("Bad cast.");
        }
    
        return result;
    }
    
    
    template <typename T>
    T parse_type(void)
    {
        std::string input;
        std::getline(std::cin, input);
    
        return lexical_cast<T>(input);
    }
    
    int main(void)
    {
        try
        {
            int i = parse_type<int>();
            float f = parse_type<float>();
        }
        catch (const std::exception& e)
        {
            std::cout << e.what() << std::endl;
        }
    }
    

    I don’t think boost has a no-throw version of lexical cast, so we can make a true/false rather than exception version of this code by catching bad_cast‘s, as follows. Once again, this works with either boost or a custom lexical cast. (Anything that does a lexical cast and throws bad_cast):

    #include <iostream>
    #include <sstream>
    #include <string>
    
    /* Faked lexical-cast from question:
    https://stackoverflow.com/questions/1243428/convert-string-to-int-with-bool-fail-in-c/
    */
    template <typename T>
    T lexical_cast(const std::string& s)
    {
        std::stringstream ss(s);
    
        T result;
        if ((ss >> result).fail() || !(ss >> std::ws).eof())
        {
            throw std::bad_cast("Bad cast.");
        }
    
        return result;
    }
    
    
    template <typename T>
    bool parse_type(T& t)
    {
        std::string input;
        std::getline(std::cin, input);
    
        try
        {
            t = lexical_cast<T>(input);
    
            return true;
        }
        catch (const std::bad_cast& e)
        {
            return false;
        }
    }
    
    int main(void)
    {
        int i;
        if (!parse_type(i))
        {
            std::cout << "Bad cast." << std::endl;
        }
    }
    

    Now it’s back to a bool result, except we avoid code duplication by using existing lexical_cast functions.

    You can of course choose which method you would like to use.

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

Sidebar

Ask A Question

Stats

  • Questions 382k
  • Answers 382k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The only type of parameterized property you can create in… May 14, 2026 at 10:23 pm
  • Editorial Team
    Editorial Team added an answer Simple: >>> import string >>> string.ascii_letters 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' >>> import random… May 14, 2026 at 10:23 pm
  • Editorial Team
    Editorial Team added an answer As Mark pointed it looks like time zone difference for… May 14, 2026 at 10:23 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.