cin >> "time remaining: %02d::%02d::%02" >> hr >> mins >> secs;
Pure technically: there is no operator>>
overload accepting char const*
. From a higher point of view, this would be meaningless anyway: operator>>
is intended to modify the parameter passed to, but char const*
data is immutable (truely immutable if string literals or pointer to const array or only virtually, but still).
You most likely intended to do:
cin << "time remaining: %02d::%02d::%02"; // out put task to user
// (maybe prepend "please enter ")
cin >> hr >> mins >> secs;
cout << "time remaining: %02d::%02d::%02" << hr << mins << secs << endl;
// ^ add some whitespace?
There are some other issues:
#include <stdlib.h>
#include <string.h>
While that usually works, correct C++ headers are:
#include <cstdlib>
#include <cstring>
using namespace std
is considered bad practice. While it migt not hurt much in a source file (.cpp), don't ever do it in a header file (.h): It might lead to name clashes, and a user using (forced to use in this case) your header cannot get rid of any more.
You don't check the stream state of std::cin
after user input. User might have entered invalid data, though, such as 10y 12z 7%
– the stream will set fail
flag first time it discovers invalid input and stops reading afterwards. Before C++11, it left input variables untouched, after, it sets them to 0. In any case, you'd be operating on invalid input. You could check once after all input and possibly exit (if it does not matter where the error occured), or you check after each input (more work, but allows more fine grained error handling).
You seem to be used to printf
function family. Be aware that C++ input/output streams do not know about format strings. So
cout << "time remaining: %02d::%02d::%02" << hr << mins << secs << endl;
assuming input was 10, 12, 7, will give you exactly:
time remaining: %02d::%02d::%0210127
Unfortunately, C++ output formatting with streams gets quickly ugly. You get the (likely) desired output via
std::cout << "time remaining: " << std::setfill('0') << std::setw(2) << hr
<< "::" << std::setw(2) << mins << "::" << std::setw(2) << secs << std::endl;
Similarly the input string; you could output what's expected by a single string, just dropping the format, and then read in three integers (these must be input without the colons, they are separated by whitespace – if you want to have colons, you need to read them separately!). More user friendly would be giving some user input what is to be entered next for each value separately – similarly to what is presented in the other answer.
Finally: system("pause");
maybe convenient, but it is not the task of an executable to keep a shell window open. It only prevents that your application is usable from within a shell script. While perhaps acceptable if you are just playing around (but you risk getting used to), don't use that in productive code.