Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to write a small and easy program: PLEASE HELP 3

Status
Not open for further replies.

silentAssassin

Programmer
Mar 12, 2001
12
US
Ok, I am currently in a beginners C++ Class. I need help writing the following practice problem:

Write a loop that calls a funciton to generate a year between 1895 and 2010. Test the year for being a leap year and print an appropriate statement to the screen. The loop should run at least five times.


I do not even know where to start. Don't worry, this problem is for practice only. I would just like to see the script so I can know what I am doing when we do some other problem for a grade.
 
OK, here's the algorithm to test to see if a year is a leap year or not.

- If a year is evenly divisible by 4, then it is leap.
- If a year is evenly divisible by 100, then it is not leap.
- If a year is evenly divisible by 400, then it is leap.

So, 1896 was leap. 1900 was not leap. 2000 was leap.

The rest of your assignment should be a simple loop. Look up the for(;;) function in your manual.

Hope this gets you started.

Chip H.
 
Yea. Thanks for your help. I am having trouble writing the loop. I am not that familiar with C++ Code. I knew about the leap year and how to find it. I just can't write the code for the loop. Can anyone please help me. Thanks again Chiph, I guess I should have been more specific.
 
> I guess I should have been more specific

Your on the right track! Hopefully no one here will write your code for you since it is a class assignment. If you are having trouble with specific code, post the code in your question and describe the problem you are having. Also post any compiler errors if that is the problem.

Good luck
-pete
 
First I want to clear up the class thing. I am taking this class on my own time. I already have a BS in Computer Science and several Certifications in Networking. This is not really a class, I just bought the book and am trying to work through it. I am glad and also hope noone "gives" me the code. I am attaching what I have, Please don't laugh too hard...LOL If anyone can either rewrite or tell me what to do from here I would be grateful. I cannot think of anything else to do to get this to work. I am just trying to learn the basics of C++ because I want to write a backup program for my daily tasks at work; however I do want to go thru this book till the end so I am trying my hardest to keep with it.

The following is my code:
___________________________________________________________

#include <iostream>
#include <cstdlib>
using std

int main()
{
// Declare Variables

int num = 5;
int run = 5;


// Perform Loop

while (run > 0)
{
while (year > 1895 && year < 2010)
srand ()

}
run = num - 1;

__________________________________________________________

I am using Microsoft Visual Studio to compile and work with these programs.
 
few comments... w/o giving all the code ... plus i'm away from my compiler so I can't

1) Do a for loop like
for(i=0; i<5; i++){ //to get the 5 reps

2) if i remember(i have a poor memory) srand() inits/seeds the random# generator, use rand() to get a random# ... comment out random# init for debugging(srand?... you'd thing i'd remember that)

3) rand() returns an int between 0 & 32767(?) so use the modulus op (%) to get an integer value between 1895 & 2010
something like
yr = (rand() % (2010 - 1895 +1)) + 1895

then test yr via a function call maybe like...

boolean IsLeapYear(int yr){

4) this is very a very &quot;c&quot; & not really a &quot;C++&quot; approach but...
 
Ok, I am kindof confused now. I took Rafe's advice but I don't know how to implement the whole Leap Year thing. So far all I have is posted below. I don't even care if you give me the answer anymore. I just want to make this program work so I can move on. Or instead of giving me the answer please at least help me with the loop. The loop part is what is throwing me for a &quot;loop&quot;. I don't understand how to do one and the example in my book is too vague. The following is what I have so far and I am posting the errors at the bottom.
________________________________________________________


#include <iostream>
#include <cstdlib>
using std;

int main()
{
// Declare Variables

int num = 5;
int run = 5;
int year = 0;

// Perform Loop

while (run > 0)
{
while (year > 1895 && year < 2010)
{
year = (rand() % (2010-1895 + 1 )) +1895;

}
run = num - 1;
cout << year << endl;
}

return 0;
}

____________________________________________________________
ERRORS:
____________________________________________________________

SampleTut3.cpp(6) : error C2873: 'std' : symbol cannot be used in a using-declaration

SampleTut3.cpp(26) : error C2065: 'cout' : undeclared identifier

SampleTut3.cpp(26) : error C2065: 'endl' : undeclared identifier

SampleTut3.cpp(26) : warning C4552: '<<' : operator has no effect; expected operator with side-effect
Error executing cl.exe.

SampleTut3.exe - 3 error(s), 1 warning(s)
___________________________________________________________
 
try losing the name space line &quot;using std&quot; i'm not sure but it looks evil.

change the while loop to a for loop, well that just me & not required actually.

the inner while loop serves no purpose.

The you have to add code that determines leap year based upon earlier posts.

 
I have no Idea what you just said. I do not know what to do as far as writing. can you please post the code for the loop. I have no idea what a for loop is. all I know is my book has a while loop for an example. ???? I am more confused than ever.
 
Will someone help me? I am so confused now. Can anyone please help me with this loop.
 
> using std;

should be

using namspace std;


That should clear up your compiler errors

Good luck
-pete
 
ok I have come a long way since my last post. Now all I am have problems with is the fact that It is not finding out if a year is truly a leap year and it is not producing REAL Random numbers, instead it produces the same numbers every time. Can someone please just post the correct way to fix these problems or describe in detail what to do. The following is the code I have thus far.
____________________________________________________________

#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
// Declare Variables
int run = 5;
int year ;
// Perform Loop
while (run > 0)
{
year = (rand() % (2010-1895 + 1 )) +1895;
if (year / 4 == 0)
{
cout << year << &quot; This year is a Leap Year!&quot; << endl;
}
else
{
cout << year << &quot; This is not a leap Year!&quot; << endl;
}
run = run - 1;
}
return 0;
}
 
Now add the srand() function before your while loop.

that's the one that &quot;seeds&quot; your random# so that it'll be &quot;pseudo-random&quot; each time... if i remember it uses the cpu timer as a seed(?) so it starts from a different spot & looks different each time. you only need to seed once!

change the &quot;year / 4&quot; to &quot;year % 4&quot; because you want the remainder of division not division itself.

then go back to the 1st post from &quot;chiph&quot; and make more changes based upon his suggestions.
 
As rafe stated earlier use 'srand()' to seed the random generator. Look at the API in whatever docs you have, there is usually example code using the system time to seed the generator.

Make sure you use assocative operators in your expressions so that they are evaluated in the order you desire.

( year / 4 == 0 ) may not do what you expect

((year / 4) == 0)

Good luck
-pete
 
OKAY Thanks guys I knew there was another way to do division I just forgot about the '%' That helped a lot. How do I use the srand ??? I have no idea where to put it or how to put it in. I don't understand what the seed should be.. you guys have been extremely helpful and I almost have it. It is at least giving me numbers now and if they are Leap year or not But they are still not random . I would like to thank everyone that has helped me. Now can someone finally finish this project up with where do I place the srand and what should I put in the () ?

This is what I have now:

___________________________________________

#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
// Declare Variables
int run = 5;
int year ;
// Perform Loop

while (run > 0)
{
year = (rand() % (2010-1895 + 1 )) +1895;

if (year % 4 == 0 || year % 400 == 0)
{
cout << year << &quot; This year is a Leap Year!&quot; << endl;
}
else
{
cout << year << &quot; This is not a leap Year!&quot; << endl;
}
run = run - 1;
}
return 0;
}
 
i'm away from my compiler & source now but if i remeber correctly all you need to do is put

srand();

as a line to itself at the top of the program somewhere. no arguments & it doesn't return anything. the rand() function as you noticed returns a sequence of numbers. the seed just starts that sequence at a different place.
 
I tried that before and it didn't work. I tried it again as you can see and it still gives me an error. The following is where I put it in and the error I got. ____________________________________

// Declare Variables
int run = 5;
int year ;
// Perform Loop
srand ();

while (run > 0)

{
_______________________________________________
IT gives me the ERROR
__________________________________________________
Test.cpp
Sample.cpp(13) : error C2660: 'srand' : function does not take 0 parameters
Error executing cl.exe.
________________________________________________________
Any suggestions???
 
I tried that before and it didn't work. I tried it again as you can see and it still gives me an error. The following is where I put it in and the error I got. ____________________________________

// Declare Variables
int run = 5;
int year ;
// Perform Loop
srand ();

while (run > 0)

{
_______________________________________________
IT gives me the ERROR
__________________________________________________
Sample.cpp
Sample.cpp(13) : error C2660: 'srand' : function does not take 0 parameters
Error executing cl.exe.
________________________________________________________
Any suggestions???
 
i'm back at my computer & you're right the call i used in my programs is...

srand((unsigned)time(NULL));

you may want to include the following

#include <time.h>

over & out
 
Well That did it. Thanks everybody. The following is the completed code and I would like to invite anyone or anybody to find an error or anything with it. If there are any bugs with it please let me know. I would also like to thank EVERYONE who helped me. Now after three days of working on this problem I can finally move on. Thanks a bunch.

__________________________________________________________
#include <iostream>
#include <cstdlib>
#include <time.h>

using namespace std;

int main()
{
// Declare Variables
int run = 5;
int year = 0;
// Perform Loop
srand((unsigned)time(NULL));

while (run > 0)
{
year = (rand() % (2010-1895 + 1 )) +1895;

if (year % 4 == 0 || year % 400 == 0)
{
cout << year << &quot; This year is a Leap Year!&quot; << endl;
}
else
{
cout << year << &quot; This year is not a leap Year!&quot; << endl;
}
run = run - 1;
}
return 0;
}
________________________________________________________
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top