dimanche 19 mars 2017

Getting input as mm/dd and separating it

I've been working on a project. Where you get the input as: mm/dd as an example 12/30. But hey, there's the challenging part.

I tried to use atoi and stoi... actually here's the code:

Date::Date(const char* date){
string s;
s = date;


string delimiter = "/";
auto start = s.begin();
auto finish = s.find(delimiter);
if (finish == s.npos){}
else{
    int mth = stoi(s.substr(0, finish));
    s = s.substr(finish + 1);
    month = Months[mth-1];
}

int c = atoi(s.c_str());
day = c;
}

Now, it takes the input from the user. Wheter it's 12/05 or 12 / 05 or even 12 / 05 notice the spaces and stores them into the private values month and day. It works just fine! However...

On Ubuntu while compiling it I had to use -std=c++11 because otherwise it gave the error Date.cpp:20:24: error: ‘atoi’ was not declared in this scope

Same also applies for stoi. Anyway, I wonder if I can do it in any other way which the default g++ would accept.

Where I can convert a string to a number, without using atoi nor the stoi, while ignoring spaces and the slash '/' symbol. Still making it work fine.

Any ideas is welcome, good luck!

Aucun commentaire:

Enregistrer un commentaire