please help me to get the month only from system.
which mean the program dont need to display time , day and also year.
how to do with it?
i only know to display date.
urgent, assignment needed.
thanks alot first.
Fbody 682 Posting Maven Featured Poster
What have you tried so far? What kind of results are you currently getting? We can't even begin to help until we know what you have and what you've tried.
mrnutty 761 Senior Poster
I wrote this DateClass some time ago, see if you find a use for it. Study carefully and
you might find what you wanted.
#pragma once
#ifndef DATE_TIME_H_
#define DATE_TIME_H_
#include <ctime>
#include <string>
#include "CommonFunction.h"
using std::string;
typedef unsigned long ulong;
struct DateInfo{
ulong seconds;//current seconds
ulong minutes;//current minutes
ulong hours;//current hours
ulong year; //current year
ulong dayOfMonth; //current day of month [1-31]
ulong dayOfYear; //current day of year since januaray 1st [0-365]
ulong monthOfYear; //current month of the year [ 0 - 11 ]
string day; //current day [mon-sun]
string month; //current month [jan-dec ]
string meridiem()const{ return (_amFlag? "AM" : "PM" ); }
DateInfo(bool isAm = false) : month(), day(), year(), hours(), minutes(), seconds(), _amFlag(isAm) {
}
private:
bool _amFlag;
};
class DateTime{
public:
typedef time_t TimeType;
typedef tm TimeInfo;
private:
DateInfo dateInfo;
public:
DateTime(){ _initDate(); }
const DateInfo& info(){
return dateInfo;
}
private:
//helpers
void _initDate(){
TimeType rawTime = TimeType();
TimeInfo info = TimeInfo();
time( &rawTime );
info = *(localtime(&rawTime) );
dateInfo = DateInfo(info.tm_hour < 12 ); //check if its am or pm
dateInfo.seconds = info.tm_sec;
dateInfo.minutes = info.tm_min;
dateInfo.hours = _converToRegularTime( info.tm_hour );
dateInfo.dayOfMonth = info.tm_mday;
dateInfo.dayOfYear = info.tm_yday;
dateInfo.monthOfYear = info.tm_mon + 1; //offset so jan = 1, feb = 2 ...
dateInfo.year = _startYear() + info.tm_year;
dateInfo.day = _convertToDay( info.tm_wday );
dateInfo.month = _convertToMonth( info.tm_mon );
}
const int _startYear()const{
return 1900;
}
string _convertToDay(int dayNum)const{
string day = "INVALID";
switch( dayNum ){
case 0 : day = "SUNDAY"; break;
case 1 : day = "MONDAY"; break;
case 2 : day = "TUESDAY"; break;
case 3 : day = "WENESDAY"; break;
case 4 : day = "THURSDAY"; break;
case 5 : day = "FRIDAY"; break;
case 6 : day = "SATURDAY"; break;
}
return day;
}
string _convertToMonth(int monthNum)const{
string month = "INVALID";
switch(monthNum){
case 0 : month = "JANUARY"; break;
case 1 : month = "FEBRUARY"; break;
case 2 : month = "MARCH"; break;
case 3 : month = "APRIL"; break;
case 4 : month = "MAY"; break;
case 5 : month = "JUNE"; break;
case 6 : month = "JULY"; break;
case 7 : month = "AUGUST"; break;
case 8 : month = "SEPTEMBER"; break;
case 9 : month = "OCTOBER"; break;
case 10 : month = "NOVEMBER"; break;
case 11 : month = "DECEMBER"; break;
}
return month;
}
ulong _converToRegularTime(ulong hr)const{
return (12 + hr) % 23;
}
};
//utility functions
//format : day/month/year = dd/mm/yyyy
string convertToDateFormat(const DateInfo& info){
return convertTo<string>( info.dayOfMonth) + "/"
+ convertTo<string>( info.monthOfYear) + "/"
+ convertTo<string> ( info.year );
}
//format : hour:minutes:sec:meridiem = hh:mm:ss:meridiem
string convertToTimeFormat(const DateInfo& info){
return convertTo<string>( info.hours) + ":"
+ convertTo<string>( info.minutes) + ":"
+ convertTo<string> ( info.seconds ) + " "
+ info.meridiem();
}
#endif
gahhon 13 Junior Poster
i had tried to seach sources in internet.
but mostly is display in date.
firstPerson : is that simplest way source code? so far im just a sem 1 and year 1 diploma student, and also i learnt till nested-loop only. (:
gahhon 13 Junior Poster
by the way,
my software is Microsoft Visual C++
and i tried your source code, when compile it show me a error which is
"c:\program files\microsoft visual studio\vc98\include\eh.h(32) : fatal error C1189: #error : "eh.h is only for C++!"
Error executing cl.exe."
what it mean?
thanks anyway bro.
Edited by gahhon because: n/a
Fbody 682 Posting Maven Featured Poster
It means you got busted in an attempt to copy/paste someone else's code... The code firstPerson posted can not function independently...
Edited by Fbody because: n/a
gahhon 13 Junior Poster
It means you got busted in an attempt to copy/paste someone else's code... The code firstPerson posted can not function independently...
how to do with this.?
sorry, i'm quite new to this course.
Fbody 682 Posting Maven Featured Poster
Get a timestamp using time(), convert it to the string representation using ctime(), then tokenize the string using strtok(). If you want the month, you'll need the second token.
gahhon 13 Junior Poster
Get a timestamp using time(), convert it to the string representation using ctime(), then tokenize the string using strtok(). If you want the month, you'll need the second token.
dude.
i had try my best to follow yours.
but i still can't solve the problem.
i try to share with you my code is easy to help me (:
//To Use printf Function
#include <stdio.h>
//To Use Maths Method Calculations
#include <math.h>
//To Use Show Date
#include <time.h>
#include <stdlib.h>
//To Change The Color Of Text
#include <windows.h>
#include <iostream.h>
void main()
//Define Constant
#define EPF_rates 0.11
{
//Variable Declaration
double basic_income, gross_income, net_pay, epf, commission, allowance;
double num_total = 1, total = 0, highest = 0;
int monthly_sales, number_person, percent, num_top;
int counter = 1;
char name[31], id[6], designation[21], gender;
//User Input
printf("\n\n");
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 10); // Change Colour Of Text
printf("\t\t\t~~~ WYNE Sdn. Bhd ~~~\t\t\t\n");
printf("\n\n\n");
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 14); // Change Colour Of Text
printf("Enter number of sales person : ");
scanf("%d", &number_person);
system("cls");
if(number_person > 0 )
{
while(number_person >= counter)
{
printf("\n\n");
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 10); // Change Colour Of Text
printf("\t\t\t~~~ WYNE Sdn. Bhd ~~~\t\t\t\n");
printf("\n\n\n");
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 6); // Change Colour Of Text
printf("Sales person #%d\n", counter);
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 11); // Change Colour Of Text
printf("Enter salesperson name : \a");
fflush(stdin);
scanf("%[^\n]s", &name);
printf("Enter salesperson ID [5 characters] : \a");
scanf("%s", &id);
fflush(stdin);
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 10); //Change Colour Of Text
printf("Enter designation : \a");
scanf("%[^\n]s", &designation);
fflush(stdin);
printf("Enter gender [M/F] : \a");
scanf("%c", &gender);
fflush(stdin);
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 13); //Change Colour Of Text
printf("Basic Income : RM \a"); // To Get User Basic Income
scanf("%lf",&basic_income);
printf("Monthly Sales : RM \a"); // To Get User Monthly Sales
scanf("%d", &monthly_sales);
system("cls");
if(monthly_sales >= 50000)
{
commission = (ceil((monthly_sales * 12 / 100)*10))/10;
percent = 12;
allowance = 300;
}
else if(monthly_sales < 50000 && monthly_sales >= 25000)
{
commission = (ceil((monthly_sales * 8 / 100)*10))/10;
percent = 8;
allowance = 200;
}
else
{
commission = (ceil((monthly_sales * 5 / 100)*10))/10;
percent = 5;
allowance = 100;
}
//Process
gross_income = basic_income + allowance + commission; // To Calculate The Amount Of Gross Income
epf = EPF_rates * gross_income; // To Calculate The Amount Of EPF
net_pay = gross_income - epf; // To Calculate The Total Amount
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 10); //Change The Color Of Text
//Output
printf("\n\n");
printf("\t\t\t~~~ WYNE Sdn. Bhd ~~~\t\t\t\n");
printf("\n\n");
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 11);
printf("Sales Person %d\n", counter);
printf("\a\a\n");
printf("Name : %s Month : %B\n",name, );
printf("Staff ID: %s Designation: %s\n", id, designation);
//Change The Color Of Text
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 14);
printf("\n");
printf("+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+~~~~~~~~~~~~~~~~~+\n");
printf("| Description | Amount (RM) |\n");
printf("+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+~~~~~~~~~~~~~~~~~+\n");
printf("| Basic Income | %-3.2f |\n", basic_income);
printf("| Allowance | %-3.2f |\n", allowance);
printf("| Monthly Sales : RM %d | |\n", monthly_sales);
printf("| Commission (%d%%) | %-3.2f |\n", percent, commission);
printf("|~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+~~~~~~~~~~~~~~~~~|\n");
printf("| Gross Pay | %-3.2f |\n", gross_income);
printf("|~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+~~~~~~~~~~~~~~~~~|\n");
printf("| EPF (11%%) | %-3.2f |\n", epf);
printf("+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+~~~~~~~~~~~~~~~~~|\n");
printf("\t\t\t Net Pay | %-3.2f |\n", net_pay);
printf("\t\t\t +~~~~~~~~~~~~~~~~~+\n");
printf("\n");
printf("\n");
there is the month that i want display a string type like "JANUARY" "FEBRUARY" and so on as well.
Edited by gahhon because: n/a
gahhon 13 Junior Poster
oh.
i can already..
but i no using you gave to me code. sorry about that.
about how i solve it.
here is the code:
time_t rawtime;
struct tm * timeinfo;
char month [80];
time ( &rawtime );
timeinfo = localtime ( &rawtime );
strftime (month,80,"%B"timeinfo);
printf("Month : %s\n", month);
it works anyway (: thank you all guys so much (:
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.