Answer:
Written in C++
#include <iostream>
#include <sstream>
using namespace std;
int main() {
string num;
cout<<"Enter a number: ";
cin>>num;
stringstream sstream(num);
int convertnum = 0;
sstream >> convertnum;
cout << "Output: " << convertnum;
}
Explanation:
Without using atoi() or stoi(), we can make use of a string stream and this is explained as follows:
This line declares a string variable num
string num;
This line prompts user for input
cout<<"Enter a number: ";
This line gets user input
cin>>num;
This line declares a string stream variable, sstream
stringstream sstream(num);
This line declares and initializes the output variable, convertnum to 0
int convertnum = 0;
The stream is passed into the output variable, convertnum
sstream >> convertnum;
This line displays the output
cout << "Output: " << convertnum;
<em>Note that: if user enters a non integer character such as (alphabet, etc), only the integer part will be convert.</em>
<em>For instance,</em>
<em>40a will be outputted as 40</em>