Devilnp123 wrote: |
---|
if(*choice == 'time') |
First of all, you're comparing only the first character of "
choice" and nothing else. Second, '
time' has an identity crisis; it doesn't know if it's a string or a character constant. In C++, there are two ways to represent characters: a string and character constant. A character constant is a single character which is enclosed in single quotes. A string is an array of characters with a null-character at the end and are enclosed in double-quotes.
Please, "
std::string" is around for a reason.
1 2 3 4 5 6 7 8 9 10
|
#include <string>
int main( )
{
std::string Input_;
std::cin >> Input_;
if(Input_ == "Time") // "Time" is a string; 'Time' is not.
// ...
}
|
See here for more: http://www.cplusplus.com/reference/string/string/
Now back to the original code. When "
time" is entered as input, "
time" is broken down into individual characters. When you extract data from the input stream and attempt to store the data in a "
char", only one character from the input stream is extract because "
char" can only hold one character at a time. After an extraction, "
ime" will still remain in the input stream. Any subsequent extraction operations with "
choice" will use those remaining characters as their input. Here's an example:
This is the current input stream's state:
Input Stream: T, I, M, E, New-Line.
1 2 3 4 5
|
int main( )
{
char Input_(0);
std::cin >> Input_;
}
|
'T' will be removed from the input stream and placed in "
Input_". This leaves the input stream in this state:
Input Stream: I, M, E, New-Line. Each extraction operation with "
Input_" will extract one character from the input stream until there's not data left. If the input stream already contains data before a user enters more data, the data that's already inside the stream will be used automatically as input.
See here: http://www.cplusplus.com/doc/tutorial/basic_io/
Wazzak