c++ - How to convert a string to a series of integers? -
making rpg , want currency represented in platinum, gold, silver , copper. unfortunately, professor wants currency stored string (i.e. string class, not cstrings). example -- 0.1.23.15 0 platinum, 1 gold, 23 silver , 15 copper.
i know big idea of how implement this. example -- use strtok (i.e. believe works on cstrings) or other c++ function accomplish this?
here's 1 solution:
#include <iostream> #include <sstream> #include <vector> using namespace std; int main() { string str="0.1.23.15",temp; stringstream s(str); vector<int> v; while(getline(s,temp,'.')) { v.push_back(stoi(temp)); } for(int i: v) cout << << endl;//c++11 style //for(int i=0; i<v.size(); i++) cout << v[i] << endl; //old school :d system("pause"); return 0; }
Comments
Post a Comment