Skip to content

Instantly share code, notes, and snippets.

@Caellian
Last active March 18, 2019 00:58
Show Gist options
  • Select an option

  • Save Caellian/d7f914bf3b607a96f7def0f5e849b684 to your computer and use it in GitHub Desktop.

Select an option

Save Caellian/d7f914bf3b607a96f7def0f5e849b684 to your computer and use it in GitHub Desktop.
Fixed number input.
#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