help....

Need help on the code for a program I need to write. Display date in 8 digit format. Need code for month, day, year and date. Must use setw function for proper spacing.

I'm really up against it here. Am in a C++ class but never had any programming in the past. So far we've learned about stuff like the basic structure of a program, cout/cin, iostream.h, commenting the code for so non programmers can understand, identifiers, constructors, objects, system defined-programmer defined, attributes, return 0, int main. Just learning about setw and adding,subtracting, mulitplication and division. About all I know but not totally sure how to put it all together.

Thanks.

Jason
closed account (zb0S216C)
What code do you have so far? We'll start to help you once you provide your current code.

Wazzak
If your on windows you can include <windows.h>

Here's a snippet from a code I have written:

1
2
3
4
5
6
7
8
9
    SYSTEMTIME st, lt;
	::std::ostringstream timeStampStream;

	GetLocalTime(&lt);
	timeStampStream.str("");
	timeStampStream <<lt.wYear<<"-"<< ::std::setfill('0')<< ::std::setw(2)<<lt.wMonth<<"-"<<::std::setw(2)<<lt.wDay
	  <<" "<< ::std::setw (2)<<lt.wHour<<":"<<::std::setw(2)<<lt.wMinute<<":"<<::std::setw(2)<<lt.wSecond<<"."
	  <<::std::setw(3)<<lt.wMilliseconds<<"0";
	timeStampString = timeStampStream.str();


It should give a timestamp in the form:
YYYY-MM-DD HH:MM:SS.xxxx
Where:
YYYY = the year in full
MM = the month (a number between 1-12)
DD = the day of the month (a number between 1-31)
HH = hours in 24-hour format (a number between 0-23)
MM = minutes (a number between 0-59)
SS = seconds (a number between 0-59)
xxxx = a fraction of a second to 4 decimal places

Let me know if you have any problems with it, I haven't really put much thought into this answer.
closed account (zb0S216C)
TheMeerkat, how will he/she learn if you give him/her the code?

Wazzak
Last edited on
Yea true, I just had that already here and just copied and pasted it without thinking too much. There should still be enough to keep jaypa busy. I'll try not to make too much of a habit of telling people the answers though.
Here's the code I have sofar.....

// TYPE YOUR NAME
// A BRIEF PROGRAM DESCRIPTION FOR CHAPTER 2, HOMEWORK 2

// COMMENT THE PREPROCESSOR STATEMENT
#include <iostream.h>

int main( )
{
// COMMENT THE OBJECT (ABOVE THE CODE)
int date;
// COMMENT THE OBJECT (ABOVE THE CODE)
int month;
// COMMENT THE OBJECT (ABOVE THE CODE)
int day;
// COMMENT THE OBJECT (ABOVE THE CODE)
int year;

// OUTPUT A MESSAGE (cout) AND THEN INPUT THE OBJECT (cin)
cout << "Enter the date (format YYYYMMDD): ";
cin >> date;

// COMMENT THE CALCULATION
month = ____________________________________________;
// COMMENT THE CALCULATION
day = ______________________________________________;
// COMMENT THE CALCULATION
year = _____________________________________________;

// OUTPUT THE INFORMATION
cout << month << " " << day << " " << year << endl;

return 0;
}

Topic archived. No new replies allowed.