Go Back   Computer Forums > General Computing > Programming
Join Computer forums Today

Thread Tools Search this Thread Display Modes
Old 08-24-2005, 07:33 PM   #1
Amorssang's Avatar
Join Date: Apr 2005
Posts: 358
Send a message via AIM to Amorssang Send a message via MSN to Amorssang
Default Random?

Alright, so I'm not to familar with c++ and I was wondering how I could get a random "Month" date for this....

#include <iostream>
using namespace std;

int main ()
int num1, num2, total;

cout << "Enter Your Birth Year: ";
cin >> num1;
cout << "Now Enter The Current Year: ";
cin >> num2;
total = num1 + num2;
cout << "Your Doom Date is September 5th Year " << total;

return 0;

What I want to do is make the "September" change randomly everytime you enter a new set of numbers. So it would change around....how would I do this? I'm sure it's possible...

Amorssang is offline   Reply With Quote
Old 08-25-2005, 04:49 PM   #2
Baseband Member
Griz803's Avatar
Join Date: Jun 2004
Posts: 89
Default Re: Random?

You'd need to set up a way to interpret the numbers 1 to 12 as month names, use the srand(time(NULL)) statement in main, called ONCE to seed the random number generator. Then use ((rand()%11)+1) [I think that will generate 1 to 12!?] to get the number and replace a string variable with the resulting month name for display. I beleive srand and rand are included in cstdlib and you will need time or time.h for the time call used to seed the random generator.
/*What does not kill us, makes us drink far too much caffeine*/
Griz803 is offline   Reply With Quote

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

All times are GMT -5. The time now is 10:30 PM.

Powered by vBulletin® Version 3.8.8 Beta 4
Copyright ©2000 - 2016, vBulletin Solutions, Inc.
Search Engine Friendly URLs by vBSEO 3.6.0