Question
Write a templated function, named AllConvert, that takes a const reference to a string and a pointer to an int. This function should convert the
Write a templated function, named "AllConvert", that takes a const reference to a string and a pointer to an int. This function should convert the string to the templated type and return that. It should also adjust the int being pointed to to indicate the number of characters of the string that were converted.
You can assume that the string does not have any newline characters, and your function should ignore leading whitespace (like stoi does).
Test Cases
#include | |
2 | std::string s = "12"; |
3 | int pos = 99; |
4 | int result = 999; |
5 | |
6 | result = AllConvert(s, &pos); |
7 | ASSERT_EQ(result, 12); |
8 | ASSERT_EQ(pos, 2); |
9 | |
10 | |
11 | s = "-67.34"; |
12 | pos = 99; |
13 | double result_2 = 999; |
14 | |
15 | result_2 = AllConvert(s, &pos); |
16 | ASSERT_EQ(result_2, -67.34); |
17 | ASSERT_EQ(pos, 6); |
18 | |
19 | |
20 | s = "cats"; |
21 | pos = 99; |
22 | std::string result_3 = ""; |
23 | |
24 | result_3 = AllConvert(s, &pos); |
25 | ASSERT_EQ(result_3, "cats"); |
26 | ASSERT_EQ(pos, 4); |
I have been able to get the string to convert to templated type, but I am having trouble getting the number for pos everytime. I believe my trouble is with working with multiple types but please see and help me if you can?
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started