fork download
  1. #include <algorithm>
  2. #include <iostream>
  3. #include <iterator>
  4. #include <string>
  5. #include <vector>
  6.  
  7. int main()
  8. {
  9. std::vector<std::string> args;
  10. unsigned time = 5;
  11. std::string temp, message, input;
  12.  
  13. args.push_back("-m");
  14.  
  15. std::cout << "\nAlright, Please enter your message: " << std::flush;
  16. std::getline(std::cin, message);
  17. std::cout << "\n\n";
  18. args.push_back('"' + message + '"');
  19.  
  20. std::cout << "Please enter time: " << std::flush;
  21.  
  22. // Validate input until it's an actual second.
  23. bool valid = false;
  24. while (!valid){
  25. std::getline(std::cin, temp);
  26.  
  27. // Validate first five characters as digits
  28. valid = true;
  29. input = "";
  30. for (unsigned i = 0; i < temp.length() && i < 5; i++)
  31. {
  32. if (!::isdigit(temp[i]))
  33. {
  34. valid = false;
  35. }
  36. input.push_back(temp[i]);
  37. }
  38. time = atoi(input.c_str());
  39. if (valid && time < 1) {
  40. std::cout <<"Unable to use a number less than 1 seconds!" << std::endl;
  41. valid = false;
  42. }
  43. if (!valid)
  44. {
  45. std::cout << "Please enter the number of seconds." << std::endl;
  46. }
  47. }
  48. std::cout << "\n";
  49. args.push_back("-t");
  50. args.push_back(input);
  51.  
  52. std::cout << "Your " << args.size() << " args are: ";
  53. std::copy(args.begin(), args.end(), std::ostream_iterator<std::string>(std::cout, ", "));
  54. }
  55.  
  56.  
Success #stdin #stdout 0s 3036KB
stdin
This is a message. It is quite long.
This is an invalid value for "time"
So is this.
The next one will succeed.
50
stdout
Alright, Please enter your message: 

Please enter time: Please enter the number of seconds.
Please enter the number of seconds.
Please enter the number of seconds.

Your 4 args are: -m, "This is a message. It is quite long.", -t, 50,