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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T02:40:30+00:00 2026-06-18T02:40:30+00:00

Consider the useful code by Zaita posted at cplusplus.com , in particular the part

  • 0

Consider the useful code by Zaita posted at cplusplus.com, in particular the part which gets numbers safely, modified to be a function in my case:

int get_number()
{
   /**
    * cplusplus.com/forum/articles/6046
    * gets number from input stream
    **/

   string input = "";
   int number = 0;

   while (true)
   {
      getline(cin, input);
      stringstream checks(input);
      if (checks >> number)
         return number;
      cout << "Please enter a valid number\n";
   }
}

Now, my question is this: Can I remove the int on the first line of the function definition for get_number(), and declare it at the top of my code with all the types I might want to return such as doing some declarations like this:

double get_number();
int get_number();
long get_number();
unsigned short get_number();
...
...

And somehow get it to do different returns depending on the variable I want to store the return from the function with? Currently I simply writing multiple definitions of essentially the same function while changing the name to get_someType

I am hoping I can do something like declare with this sort of syntax:

int get_number(int);
double get_double(double);
...
...

And my desire would be to do something like:

int x;
x = get_number(int);

I am sure this will NOT work however! Because it would be impossible to define the function’s source code with parameters with no names…

  • 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-18T02:40:31+00:00Added an answer on June 18, 2026 at 2:40 am

    You’re looking for function templates!

    This code is buggy (please don’t use cplusplus.com as a reference), since it doesn’t check that input from std::cin is received.

    A templated example would look something like this (untested):

    #include <string>
    #include <sstream>
    #include <iostream>
    #include <stdexcept>
    
    template <typename T>
    T get_number()
    {
        std::string input = "";
        T number; // Don't initialise to zero
        bool done = false;
    
        while (!done)
        {
            if (std::getline(std::cin, input)) // check for success
            {
                std::istringstream checks(input);
                if (checks >> number)
                {
                    done = true;
                }
                else
                {
                    std::cout << "Please enter a valid number\n";
                }
            }
            else
            {
                throw std::runtime_error("No input was received\n");
            }
        }
        return number;
    }
    
    int main()
    {
        try
        {
            int i = get_number<int>();
            float f = get_number<float>();
            double d = get_number<double>();
            char b = get_number<char>();
        }
        catch (const std::runtime_error& e)
        {
            std::cerr << e.what() << std::endl;
        }
    }
    

    To incorporate my comment below into this answer… In a class template or function template, typename can be used as an alternative to class to declare templated types. I prefer typename because we’re dealing with POD-types and not classes.

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

Sidebar

Related Questions

Consider the following code: template <class x1, class x2 = int*> struct CoreTemplate {
Consider the following code: public interface A { public A another(); } public interface
Consider this code: enum { ERR_START, ERR_CANNOTOPENFILE, ERR_CANNOTCONNECT, ERR_CANNOTCONNECTWITH, ERR_CANNOTGETHOSTNAME, ERR_CANNOTSEND, }; char* ERR_MESSAGE[]
Consider the following snippet of code: foreach (var setting in RequiredSettings) { try {
Consider this code snippet:- Whatever mock = EasyMock.createMock( Whatever.class ); mock.doSomething(); EasyMock.expectLastCall(); // <-
Consider the following code: #include <iostream> struct test { void public_test() { [this]() {
Consider the following code: [Test] public void WidgetTest() { foreach (Widget widget in widgets)
Consider the custom error function: function customError($errNumber, $errString, $errFile, $errLine, $errContext) The $errContext provides
Consider the following VB code: Dim fooBar As String fooBar = Foo Bar Dim
Consider some code that can throw a checked exception (an exception of type Exception

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.