c++ - Parse a stringstream into strings and doubles -


i writing program takes inputs on 1 line follows:

run 10.1 50.2

where "run" string , rest of line doubles. scientific notation , negative numbers can used double inputs: -5.88e-11 example (standard c++ library allows this).

here initial code tried.

string command;  double input1;  double input2;  getline(cin,input); stringstream ss(input);  ss >> command >> input1 >> input2; 

the problem approach if blank space or letter input in place of double output of stringstream 0. believe because there no null holders double in c++.

another approach tried read each input string, check string numerical , convert double. however, becomes complicated when scientific notation , negatives can entered. attempt @ this:

for (int i=0; input1[i]; i++){     if (isdigit(input1[i])){\         isdigit = true;     }else{         isdigit = false;     } } 

how can parse input string , alphanumerical doubles on same line? (while retaining negatives , scientific notation)

thanks!

using std::cin directly , checking if there's error in parsing stream.

std::string command; double arg1, arg2;  if (std::cin >> command >> arg1 >> arg2) {     std::cout << command << std::endl;     std::cout << arg1 << std::endl;     std::cout << arg2 << std::endl; } else {     // error! } 

Comments

Popular posts from this blog

javascript - Chart.js (Radar Chart) different scaleLineColor for each scaleLine -

apache - Error with PHP mail(): Multiple or malformed newlines found in additional_header -

java - Android – MapFragment overlay button shadow, just like MyLocation button -