answersLogoWhite

0


Best Answer

Overloads cannot differ by return type alone. The only way to achieve this is to use output arguments. Since the implementation is exactly the same regardless of the output type, you can use a function template to generate the overloads.

#include<iostream>

#include<sstream>

template<typename T>

bool convert(std::string& s, T& value)

{

std::stringstream ss;

ss << s;

if (ss >> value)

return true;

return false;

}

int main()

{

int i;

float f;

std::string s {"3.14"};

if (convert (s, i))

std::cout << '"' << s << "" = " << i << std::endl;

if (convert (s, f))

std::cout << '"' << s << "" = " << f << std::endl;

}

Output:

"3.14" = 3

"3.14" = 3.14

User Avatar

Wiki User

9y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Write overload functions in c plus plus to convert an ascii string to an int and to convert an ascii string to float?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

Write a program convert a string from uppercase to lower case and vice-versa without using string tolower and string toupper commands in TCL?

[ string toupper $str ] or [ string tolower $str ]


Write a Program to convert decimal number into hexadecimal no without using function and string?

This is not a question.


How to write A Java program to print number of digits in a given number?

One way to do this is to convert the number to a String, then use the corresponding String method to find out the length of the String.


Write a C program to convert an upper case word to lower case and vice-versa.?

You would use the ToUpper and ToLower functions. string name = "XVengeance"; string upperName = name.ToUpper(); string lowerName = name.ToLower(); Console.WriteLine(upperName); Console.WriteLine(lowerName); Console.ReadLine(); I don't think I'm supposed to do your homework for you, but this code should get you started on making a more dynamic program.


How can I write a method that returns true if the number passed can be divided by all its digits?

Write a loop, in which you divide by each of the digits; if it's NOT divisible, set the variable "result", which you initiall set to "true", equal to "false". The tricky part is extracting the digits; one way to do this is to convert the number to a string, and use string functions. You can also repeatedly divide by 10, and use the remainder. That is, first take the remainder and test it; then divide the remaining number by 10.


Write a program in c to convert a given string to lower case?

Use the tolower() function. Example: char* a = 'X'; a = tolower(a); printf("%c", a);


How do you write programs for String Reversal Palindrome check?

Copy and reverse the string. If the reversed string is equal to the original string, the string is a palindrome, otherwise it is not. When working with strings that hold natural language phrases (including punctuation, whitespace and so on) we must remove all the non-alphanumerics and convert the remainder to a common case, such as lower-case, prior to copying and reversing the string.


How do you write a algebraic expression for twice as long as the length of the string?

You can write 2X, where X is the length of the string.


How to write a C plus plus Program to convert a string into an integer?

std::string input = ""; std::getline (std::cin, input); // get input from stdin std::stringstream ss (input); // place input in a string stream integer num = 0; if (ss &gt;&gt; num) // extract integer from string stream { // Success! } else { // Fail! }


Write a program to implement the various operations on string such as length of string concatenation reverse of a string copy of a string to another in VB?

what is string


Write a program sort a list of names in alphabetical order in java programming language?

// Let's assume we're sorting the characters in String toSort // convert the String to an array of characters char[] chars = toSort.toCharArray(); // let Java do the sorting for you Arrays.sort(chars); // recreate the original String with the newly sorted array of characters toSort = new String(chars);


How do you write a program to input an integer?

The safest way is to capture the input as a string and then convert the string to an integer. The reason is that all standard input (regardless of where it comes from) is done through character streams and it's safer to capture this input using a string rather than trying to perform conversions on the incoming data directly.