Created
May 7, 2018 12:51
-
-
Save jflopezfernandez/3f4c73d711f99cc605399ef99e871ff6 to your computer and use it in GitHub Desktop.
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
// MSVC 17 warns about using the `strtok` function | |
#define _CRT_SECURE_NO_WARNINGS | |
#include <iostream> | |
#include <string> | |
#include <vector> | |
template <typename T, typename Traits = char_traits<T>> | |
std::basic_ostream<T, Traits>& | |
NL(std::basic_ostream<T, Traits>& out) | |
{ | |
return out << out.widen('\n'); | |
} | |
template <typename T> | |
void PrintVector(const std::vector<T>& vector) | |
{ | |
for (auto& v : vector) | |
{ | |
std::cout << v << NL; | |
} | |
} | |
int main() | |
{ | |
std::vector<std::string> words; | |
std::string input; | |
std::cout << "Enter message: "; | |
std::getline(std::cin, input); | |
if (std::cin.fail()) | |
{ | |
std::cerr << "[Error]: Failed to read user input." << NL; | |
return EXIT_FAILURE; | |
} | |
char *singleWord = std::strtok(const_cast<char *>(input.c_str()), " "); | |
while (singleWord) | |
{ | |
words.emplace_back(singleWord); | |
singleWord = std::strtok(nullptr, " "); | |
} | |
PrintVector(words); | |
return EXIT_SUCCESS; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment