#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);
}
2
Contributors
2
Replies
9
Views
4 Years
Discussion Span
Last Post by Lucaci Andrew
0

Noticed that, working on fixing the bug. 10x ravenous for noticing.

Here, this should patch up quite nice:

bool isInt(string s){
    string dash="-";
    if (s[0]==dash[0]) s.erase(0, 1);
    for (int i=0;i<(int)s.size();i++)
        if (isdigit(s[i])==0) return (false);
    return (true);
}



int main(){
    string s="-2";
    int i=toInt(s);
    assert((i+1)==-1);
    cout<<i+1;
    string s1="-22-2";
    assert (isInt(s1)==false);
    return (0);
}

Edited by Lucaci Andrew

Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.