Simple conversion between string and int.
Simple string-to-int and int-to-string conversion
#include <iostream>
#include <sstream>
#include <string>
#include <assert.h>
#include <cctype>
using namespace std;
string toStd(int i){
stringstream toStd;
toStd<<i;
return (toStd.str());
}
bool isInt(string s){
for (int i=0;i<(int)s.size();i++)
if (isdigit(s[i])==0) return (false);
return (true);
}
int toInt(string s){
stringstream toInt;
int ret;
if (isInt(s)){
toInt<<s;
toInt>>ret;
return (ret);
}
else return (-1);
}
int main(){
string c="2312", b;
int i=1123, j;
assert ((b=toStd(i))=="1123");
assert ((j=toInt(c))==2312);
return (0);
}
Lucaci Andrew 140 Za s|n
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.