Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <sstream>
- #include <list>
- bool isSeperator(char c);
- bool isNumber(char c);
- int main()
- {
- std::string input; // Input that gets used in each part of the main function.
- std::string buffer; // A buffer meant to help process the input.
- std::cout << "Please enter a bunch of numbers seperated with commas." << std::endl;
- std::getline(std::cin, input);
- // Remove non seperators or numbers
- buffer = input;
- input = "";
- for( char c : buffer )
- {
- if( isNumber(c) or isSeperator(c) )
- {
- input.push_back(c);
- }
- }
- // Create list with numbers in string format.
- std::list<std::string> str_numbers;
- buffer = "";
- for( char c : input )
- {
- if( isNumber(c) )
- buffer.push_back(c);
- if( isSeperator(c) )
- {
- if(buffer.size() > 0)
- {
- str_numbers.push_back(buffer);
- buffer = "";
- }
- }
- }
- // Convert string numbers into int numbers.
- std::list<int> int_numbers;
- for( std::string str : str_numbers )
- {
- int num;
- std::istringstream ss(str);
- ss >> num;
- int_numbers.push_back(num);
- }
- // Print the numbers
- for( int num : int_numbers )
- std::cout << num << std::endl;
- }
- bool isSeperator(char c)
- {
- if(c == ',')
- return true;
- if(c == ';')
- return true;
- if(c == '&')
- return true;
- return false;
- }
- bool isNumber(char c)
- {
- return (c>='0' && c<='9');
- }
Advertisement
Add Comment
Please, Sign In to add comment