Last active
November 1, 2016 10:50
-
-
Save jacknlliu/a714447d60e8ea90bfc0150eebba8e0c to your computer and use it in GitHub Desktop.
Input a double array from commandline
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * This is an example input a double number array from commandline | |
| * input: [ d1 d2 d3 ] | |
| * ouput: d1 d2 d3 | |
| */ | |
| #include <iostream> | |
| #include <string> | |
| #include <sstream> | |
| using namespace std; | |
| int main() | |
| { | |
| std::string line; | |
| cout<<"Please input, using space as separator:"<<endl; | |
| std::getline(std::cin, line); // read a line from std::cin into line | |
| const std::string input = line; | |
| // create an input string stream to read from the string | |
| std::istringstream stm(input) ; | |
| // perform formatted input from the string stream | |
| std::string str1, str2; | |
| double d1, d2, d3; | |
| // split string to some pattern(formatted), then print | |
| if( stm >> str1 >> d1 >> d2 >> d3 >> str2 ) | |
| { | |
| std::cout << d1 << '\n' << d2 << '\n' | |
| << d3 << '\n' ; | |
| } | |
| return 0; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * This is a general example to input a double number and strings array from commandline | |
| * input: str d1 i1 d2 d3 i2 | |
| */ | |
| #include <iostream> | |
| #include <string> | |
| #include <sstream> | |
| using namespace std; | |
| int main() | |
| { | |
| std::string line; | |
| cout<<"Please input:"<<endl; | |
| std::getline(std::cin, line); // read a line from std::cin into line | |
| const std::string input = line; | |
| // create an input string stream to read from the string | |
| std::istringstream stm(input) ; | |
| // perform formatted input from the string stream | |
| std::string str ; | |
| double d1, d2, d3; | |
| int i1, i2; | |
| // split string to some pattern(formatted), then print | |
| if( stm >> str >> d1 >> i1 >> d2 >> d3 >> i2 ) | |
| { | |
| std::cout << std::fixed << str << '\n' << d1 << '\n' << i1 << '\n' | |
| << d2 << '\n' << d3 << '\n' << i2 << '\n' ; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment