Last active
March 18, 2019 00:58
-
-
Save Caellian/d7f914bf3b607a96f7def0f5e849b684 to your computer and use it in GitHub Desktop.
Fixed number input.
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
| #include <iostream> | |
| #include <string> | |
| #include <sstream> | |
| template <class T> | |
| void readInto(T *var, const std::string &inputMessage, const std::string &errorMessage) { | |
| std::string input; | |
| while (true) { | |
| std::cout << inputMessage; | |
| getline(std::cin, input); | |
| std::stringstream s(input); | |
| if (s >> *var) return; | |
| std::cout << errorMessage << std::endl; | |
| } | |
| } | |
| int main(int argc, char const *argv[]) { | |
| std::string unos = ""; | |
| int unos2 = 0; | |
| while (unos != "bye") { | |
| readInto(&unos2, "Unesite broj: ", "WUT?"); | |
| std::cout << "Uneseno je '" << unos2 << "'." << std::endl; | |
| readInto(&unos, "Unesite tekst: ", "Smak svijeta!"); | |
| std::cout << "Uneseno je '" << unos << "'." << std::endl; | |
| } | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment