View Full Version: here is a lil program i made

C++ Learning Community > C++ Creations > here is a lil program i made

Pages: [1] 2

Title: here is a lil program i made


buzz - March 4, 2004 10:51 PM (GMT)
here is a lil program that i made in like 2 min.
its an age finder!

CODE

// age finder
#include <iostream.h>

int main()
{
int a, b;
cout << "Enter year of birth: ";
cin >> a;

b = 2004 - a;
cout << "You are " << b << " or you are going to turn " << b << " this year!";

return 0;
}

TheHawgMaster - March 4, 2004 11:02 PM (GMT)
I guess we've all made one of these at some point... :lol: Anyway the only thing that's even sorta wrong with it is
CODE
#include <iostream>
using std::cout;
using std:: cin;
Would be better than what you have.

buzz - March 4, 2004 11:05 PM (GMT)
ok ill do that cept il just use
CODE


#include <iostream>
using namespace std;



that should work just fine eh

KTC - March 4, 2004 11:24 PM (GMT)
Yeah.

Sam Fisher vs Solid Snake - March 5, 2004 10:35 PM (GMT)
i agree

buzz - March 5, 2004 10:37 PM (GMT)
thats good u all agree lol

Sam Fisher vs Solid Snake - March 5, 2004 10:52 PM (GMT)
well im glad u thinks so j/k
:wacko:

buzz - March 5, 2004 11:15 PM (GMT)
i have a problem w/ my .EXE file on my age finder. every time i push enter after i enter my birth date, the window closes. isnt ther something i can put into the code to make it pause, like a different library instead of iostream?

KTC - March 6, 2004 12:03 AM (GMT)
Read the bloody FAQ !!!

Sam Fisher vs Solid Snake - March 6, 2004 01:33 AM (GMT)
QUOTE
i have a problem w/ my .EXE file on my age finder. every time i push enter after i enter my birth date, the window closes. isnt ther something i can put into the code to make it pause, like a different library instead of iostream?



system("pause");
return 0;

there do that

buzz - March 6, 2004 06:51 AM (GMT)
yup lol i figured that out like an hour after it wasnt workin. at first i thought it was like
#include <conio.h>
_getch();
or something like that
lol thanx newaz

absolute n00b - March 6, 2004 10:36 PM (GMT)
Well here's point from a n00b also:)
I think hard coded stuff is bad, i'm thinkin of that year 2004, that way u'r program won't work next year, so i would like some of the C++ experts to somehow extract system time into aditional variable so this program works allways, next year and everyother year for that matter. I'm i making any sense? :)

something maybe with

#include <ctime>
time (year)

Dunno if i'm making any sense this was just an idea....


Sam Fisher vs Solid Snake - March 6, 2004 11:33 PM (GMT)
in a bit i will sned a post to u explaining time, u wanty current time right? And u want it to constantly update?

Sam Fisher vs Solid Snake - March 6, 2004 11:49 PM (GMT)
CODE
//Clock.cpp
#include <iostream>
#include <ctime>

//main() function
int main()
{
std::time_t now = std::time(0);
std::cout << std::asctime(std::gmtime(&now));

system("pause");
return 0;
}


this takes the current time it loaded on and displays it in gmtime, i will make a better one (that updates) soon

buzz - March 7, 2004 12:18 AM (GMT)
hey good idea!!

absolute n00b - March 7, 2004 12:52 AM (GMT)
Well if i'm not mistaken thats a prog for a GMT time, but what he needs here is current year (extracted in a similar way from the system maybe?)
How to do that?
(even thou it wasn't a bad thing to learn a clock either :)

buzz - March 7, 2004 01:00 AM (GMT)
hey what if you could make the time and once it hits a certain date it changes the code to b = 2005 - a and keeps doing that

absolute n00b - March 7, 2004 01:10 AM (GMT)
QUOTE (buzz @ Mar 7 2004, 01:00 AM)
hey what if you could make the time and once it hits a certain date it changes the code to b = 2005 - a and keeps doing that

Well that is the point of all of this. But i never worked in C++ (just last few days).
So i just know that u must do following in order that to be nice little GuessAge program:
1.) extract year from a sistem somehow (somebody will write this i'm sure, here people RULE!)
2.) store a year into the variable (let's say we called it currentYear)
3.) put variable into your formula
b = currentYear - a

That way year would be allways read from system and would allways be properly updated.
Somebody more expirienced plz act soon!
I hope i'm not mistaken.

Sam Fisher vs Solid Snake - March 7, 2004 01:23 AM (GMT)
edit: oops bad post sry

Sam Fisher vs Solid Snake - March 7, 2004 01:25 AM (GMT)
i didnt quite figure out what u said earlier sry

buzz - March 7, 2004 01:35 AM (GMT)
or we could just do this right
CODE
// age finder
#include <iostream.h>
using namespace std;
int main()
{
int a, b,c;
cout << "Enter current year: ";
cin >> a;
cout << "Enter year of birth: ";
cin >> b;
c = b - a;
cout << "You are " << c << " or you are going to turn " << c << " this year!";

return 0;
}

Sam Fisher vs Solid Snake - March 7, 2004 05:04 PM (GMT)
uh an error in that is iostream.h dont use that since not all comilers understand it, just put <iostream>

absolute n00b - March 7, 2004 06:24 PM (GMT)
OK Snake :)
My question is this: by executing u'r gmt program i get this result:

CODE

Sun Mar 07 18:14:57 2004


That's nice, but i need to extract ONLY year from that (2004 at the end of prog result) and store it into variable currentYear?

Can u show me how to do that?
thx

Sam Fisher vs Solid Snake - March 7, 2004 07:38 PM (GMT)
here it is 12:35 and im going to be gone quite awhile in a few minutes so ill try and make u one when i get back ok, so u just want the year?, no time, no date just year?

absolute n00b - March 7, 2004 09:21 PM (GMT)
Well, in this particular case year is more than enough :)
BUT, if u'r willing i'm ready to learn and listen how to take out all these values separately from gmt time:
Sun Mar 07 18:14:57 2004 (day separately, date, clock, year of course everytihng separately and how to store them into separate variables)
Do u get me? I want to know how to extract any part of the gmt time?
Year for starters.
Thx man.

Sam Fisher vs Solid Snake - March 7, 2004 09:24 PM (GMT)
i will work on it, i would give a week at max to figure it out


this is what i have so far

CODE
//Clock.cpp
//Written by Andrew Suter-Morris
//A.K.A. Sam Fisher vs. Solid Snake
//Email:atsmorris@sbbco.com
//AIM:SeraphimFieryOne

//Includes
#include <iostream> //Input/Output stream
#include <iomanip> //Input/Output manipulation
#include <ctime> //Time include

//Date Strucute
struct Date
{
int month, day, year; //
void DisplayAllSeperately(); //Date Function Display Method
};

//DisplayAllSeperately()'s member functions
void Date::DisplayAllSeperately()
{
static char *monthdisplay[] =
{
 "January","Feburary","March","April","May","June",
 "July","August","September","October","November",
 "December"
};

std::cout << monthdisplay[month] << ' ' << day << ", " << year;
}

//Time structure
struct Time
{
int hour, minute, second;
void DisplayAllSeperately(); //Time Function Display Method
};

//DisplayAllSeperately's member functions
void Time::DisplayAllSeperately()
{
std::cout.fill('0');
std::cout << (hour>12?hour-12:(hour==0?12:hour)) << ':' //Hour Display
    << std::setw(2) << minute << ':'              //Minute Display
    << std::setw(2) << second                     // Second Display
    << (hour < 12 ? "am" : "pm");                 //Check whether its p.m. or a.m.
}

//Main() function
int main()
{
//Get current time from OS
std::time_t curtime = time(0);
std::tm tim = * std::localtime(&curtime);

//Define Date & Time Structures
Time now;
Date today;

//Initialize the structures
now.hour = tim.tm_hour;
now.minute = tim.tm_min;
now.second = tim.tm_sec;
today.month = tim.tm_mon;
today.day = tim.tm_mday;
today.year = tim.tm_year+1900;

//Display All Seperately
std::cout << "At the tone it will be ";
now.DisplayAllSeperately();
std::cout << " on ";
today.DisplayAllSeperately();
std::cout << '\a' << std::endl;

system("pause");
return 0;
}



but im getting there, im looking for ways to extract all of it seperatly, i am really close, im thinking of inserting the time/date into the function then display it seperately. ALMOST DONE

KTC - March 7, 2004 10:29 PM (GMT)
emmm guys ... what's the problem with just extracting the year from gmtime() ?
CODE
#include <iostream>
#include <ctime>

int main()
{
   time_t rawtime;
   tm *ptm;

   time( &rawtime );

   ptm = gmtime( &rawtime );

   std::cout << (ptm->tm_year)+1900 << '\n';

   return 0;
}


Sam Fisher vs Solid Snake - March 7, 2004 10:32 PM (GMT)
nothing anyways i figured it out, its a bit longer about 85 lines of code



Here is the console clock finished, the time wont change unless u keep opening it, this was just meant to recieve the time it opened the app, it displays the date too, the only reason it has that
3
7
2004

look is because it shows how to seperate the date and time, it wasnt hard

CODE

//Clock.cpp
//Written by Andrew Suter-Morris
//A.K.A. Sam Fisher vs. Solid Snake
//Email:atsmorris@sbbco.com
//AIM:SeraphimFieryOne

//Includes
#include <iostream> //Input/Output stream
#include <iomanip> //Input/Output manipulation
#include <ctime> //Time include

//Date Strucute
struct Date
{
int month, day, year; //Date ints
void DisplayAllSeperately(); //Date Function Display Method
};

//DisplayAllSeperately()'s member functions
void Date::DisplayAllSeperately()
{
static char *monthdisplay[] =
{
 "January","Feburary","March","April","May","June",
 "July","August","September","October","November",
 "December"
};

std::cout << monthdisplay[month] << ' ';
   std::cout << day << ", ";
std::cout << year;
}

//Time structure
struct Time
{
int hour, minute, second; //Time ints
void DisplayAllSeperately(); //Time Function Display Method
};

//DisplayAllSeperately's member functions
void Time::DisplayAllSeperately()
{
std::cout.fill('0');
std::cout << (hour>12?hour-12:(hour==0?12:hour)) << ':' //Hour Display
    << std::setw(2) << minute << ':'              //Minute Display
    << std::setw(2) << second                     // Second Display
    << (hour < 12 ? "am" : "pm");                 //Check whether its p.m. or a.m.
}

//Main() function
int main()
{
//Get current time from OS
std::time_t curtime = time(0);
std::tm tim = * std::localtime(&curtime);

//Define Date & Time Structures
Time now;
Date today;

//Initialize the structures
now.hour = tim.tm_hour;
now.minute = tim.tm_min;
now.second = tim.tm_sec;
today.month = tim.tm_mon;
today.day = tim.tm_mday;
today.year = tim.tm_year+1900;

//New ints
int HOUR = tim.tm_hour;
int MINUTE = tim.tm_min;
int SECOND = tim.tm_sec;
int MONTH = tim.tm_mon+1;
int DAY = tim.tm_mday;
int YEAR = tim.tm_year+1900;

//Display Time
std::cout << "At the tone it will be ";
std::cout << std::endl;
now.DisplayAllSeperately();
std::cout << std::endl;
// Display Month on one line, Day on another, and year on the last
std::cout << MONTH << std::endl;
std::cout << DAY << std::endl;
std::cout << YEAR << std::endl;
std::cout << '\a' << std::endl;

system("pause");
return 0;
}






buzz - March 7, 2004 10:38 PM (GMT)
coool!!!!!

absolute n00b - March 8, 2004 08:18 PM (GMT)
Concidering all that is written here, this is the way i choose:

CODE

#include <iostream>
#include <ctime>
using namespace std;

int main()
{
time_t rawtime;
tm *ptm;
               time( &rawtime );
               ptm = gmtime( &rawtime );

int currentYear = 0;
currentYear = (ptm->tm_year)+1900;
 
int a = 0, b = 0;
cout << "Enter year of birth: ";
cin >> a;

b = currentYear - a;
cout << "You are " << b << " or you are going to turn " << b << " this year!" << endl;

return 0;
}


Maybe this way it's a bit more efficient 'cause u don't have to type in years twice and current year is allways updated from the GMT clock.

Sam Fisher vs Solid Snake - March 9, 2004 02:00 PM (GMT)
thats cool, the reson minen is so long is im gonna make a win32 app for it and actually have a clock/date

buzz - March 10, 2004 04:14 AM (GMT)
thats pretty cool cept there is no reason for the time/clock

Sam Fisher vs Solid Snake - March 10, 2004 01:44 PM (GMT)
for u maybe but my app's gonna have both

ih8censorship - March 10, 2004 03:00 PM (GMT)
yeah well mine is gonna be connected to a speaking robot---a GIRL robot ^_^

wait what are we talking about?:lol: ;)

C-Man - March 10, 2004 03:54 PM (GMT)
:D :D :D

buzz - March 10, 2004 09:11 PM (GMT)
lol an age finder. he wants the time and date.

micster - March 13, 2004 09:34 PM (GMT)
Hi im new here. Earlyer on someone said about executing and it comes on and off. if PAUSE isnt avalible to use then open the exe in a command prompt it wont close it when it finishes

Micster

Days using C= 1

C-Man - March 14, 2004 08:57 AM (GMT)
Type EXIT

micster - March 14, 2004 11:17 AM (GMT)
i know <_<

micster

days using c= 2

FrozenKnight - March 14, 2004 09:28 PM (GMT)
i ws just wondering why we need a program to find something we already know. unless you want to know how many seconds / minutes / hours / days you have lived. i have no need to know how meny years i have lived.




* Hosted for free by InvisionFree