Why the number is truncated?

Jan 29, 2008 at 1:23am
I am new to C++. I tried to input a YYddd HH MM as 08028 16 00 but the number truncated as 8028160. What did I do wrong?
Here is the code:

#include <iostream> //Required for stream i/o
#include <iomanip> //Required for stream formatting
#include <string> //Required for string functions

using namespace std; //Required CS161 namespace

int main(int argc, char **argv)
{
char userHoldCharacter; //Character to hold user input
int argIndex; //Scratch index used in loop
long int checkOutDay; //User checkout day
long int checkOutHour; //User checkout hour
long int checkOutMinute; //User checkout minute
long int returnDay; //User return day
long int returnHour; //User return hour
long int returnMinute; //User return minute

for(argIndex = 0; argIndex < argc; argIndex++)
{
cout << "[\"" << argv[argIndex] << "\"] ";
}
cout << endl;

cout << endl;
cout << "Welcome to the Video Store" << endl;
cout << endl;
cout << "YY is a two-digit year (\"07\" for 2007, \"08\" for 2008, etc.)" << endl;
cout << "ddd is the day of the year. ddd is 3-digit number between 0 and 365" << endl;
cout << endl;

cout << "Please enter the checkout date and time - separated by spaces (YYddd HH MM): " << endl;
cin >> checkOutDay >> checkOutHour >> checkOutMinute;
cout << endl;

cout << "Please enter the checkin date and time - separated by spaces (YYddd HH MM): " << endl;
cin >> returnDay >> returnHour >> returnMinute;
cout << endl;

cout << "Checkout YYddd HH MM = " << checkOutDay << checkOutHour << checkOutMinute << endl;
cout << "Return YYddd HH MM = " << returnDay << returnHour << returnMinute << endl;

cout << "Press any key followed by <enter> to exit the program" << endl;
cin >> userHoldCharacter; //Wait for user input
cout << "[done]" << endl; //Print termination message
return 0; //Exit point for main

} //End of main function
Jan 29, 2008 at 11:03am
Hey. I'm not an expert but I think I know what is wrong.
Try this:

cout << "Checkout YYddd HH MM = " << checkOutDay << " " << checkOutHour << " " << checkOutMinute << endl;
cout << "Return YYddd HH MM = " << returnDay << " " << returnHour << " " << returnMinute << endl;
Jan 29, 2008 at 11:19am
The problem is that you're storing them in integers. Whilst this is the practical solution for processing them, it's not ideal for outputting them. If you input '01', and tell it to output, it will just print '1'.
Topic archived. No new replies allowed.