C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old September 26th, 2006, 02:10 PM
henno2000 henno2000 is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Sep 2006
Posts: 59 henno2000 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 12 h 52 m 10 sec
Reputation Power: 3
Help with the rand function

Hi, I am trying to write a program that generates a random number between 1 & 100 and I have 10 chances to guess what the number is before it tells me.

I think I can write all of it, with the exception of the rand function (the initialising of it), from there all you have to do is compare the unknown value with your guess (correct me if I am wrong)

I am using C

If possible could anyone give me any pointers please?

Thanks

Reply With Quote
  #2  
Old September 26th, 2006, 04:44 PM
Paul820 Paul820 is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2006
Location: United Kingdom
Posts: 346 Paul820 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 Days 14 h 14 m 58 sec
Reputation Power: 3
Here's how to get a random number:

Code:
// generate a random number
#include <iostream>
#include <windows.h>

int main()
{
	int randomNum = rand();
	srand( GetTickCount() );
	randomNum = rand() % 100;  // from 0 to 99 - one less than the value you enter so if you put 20 it will be 0 to 19

	std::cout << "Random number is " << randomNum;

	return 0;
}


Then just compare randomNum with your guess.

Reply With Quote
  #3  
Old September 26th, 2006, 06:06 PM
henno2000 henno2000 is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Sep 2006
Posts: 59 henno2000 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 12 h 52 m 10 sec
Reputation Power: 3
Thanks, is this basically the same for C?

I have been trying to use just the following commands

#include <stdio.h>
#include <stdlib.h>
rand()
srand

Is this possible with a similar structure?

Thanks again

Reply With Quote
  #4  
Old September 26th, 2006, 07:18 PM
Paul820 Paul820 is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2006
Location: United Kingdom
Posts: 346 Paul820 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 Days 14 h 14 m 58 sec
Reputation Power: 3
You can use stdio.h and stdlib.h, but you also need windows.h for the GetTickCount()

srand( GetTickCount() );
int randomNum = rand() % 100;

Then just compare it. Without using windows.h it won't work.

Reply With Quote
  #5  
Old September 26th, 2006, 07:25 PM
ubergeek ubergeek is offline
Contributing User
Dev Articles Novice (500 - 999 posts)
 
Join Date: Jan 2005
Posts: 600 ubergeek User rank is Private First Class (20 - 50 Reputation Level)ubergeek User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 2 Days 22 h 40 m 27 sec
Reputation Power: 4
Send a message via AIM to ubergeek
If you don't want to use windows.h, include time.h instead and replace GetTickCount() with clock().

Reply With Quote
  #6  
Old September 27th, 2006, 01:59 AM
henno2000 henno2000 is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Sep 2006
Posts: 59 henno2000 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 12 h 52 m 10 sec
Reputation Power: 3
Thanks, just wrote it in C, changing the bits that apply (ie - cout to printf etc)

but get the error .eh is for C++

Which part does this refer to? I tried double clicking on the error and it takes me to a fuction list

Thanks

Reply With Quote
  #7  
Old September 27th, 2006, 01:45 PM
henno2000 henno2000 is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Sep 2006
Posts: 59 henno2000 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 12 h 52 m 10 sec
Reputation Power: 3
Nearly cracked it, just one final thing I noticed when I was debugging, if I put 2 wrong answers in, then enter the right answer on the third go, it will tell me I have had too many goes and kick me out, but if I increase the count condition by one, it keeps telling me that my third answer is always correct, even if it isn't.

I have already tried moving the increment value to outside the loop, but obviously after the first loop, the value will not change.

Could someone have a look and see if it can be rectified or suggest any ideas?

Code:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

// Guess a number 1-6 with 3 tries

void main()
{
	int a, ans;
	int count = 0;

	srand((unsigned) time (NULL));
	
	ans = ((rand()%6)+1);
	printf("%d\n", ans);
	printf("Three chances to to guess a number between 1 - 6: ");
	scanf("%d", &a);
		
	while ((a != ans) && (count < 2))
	{
		count++;
		printf("Wrong! Please try again: Number of goes so far (%d): ", count +1);
		scanf("%d", &a);

	}

	if (count >=2 )
	{
		printf("Too many goes. End of game. The number was %d\n", ans);
	}
	else
	{
		printf("Correct!\n");
		printf("You had %d goes.\n", count + 1);
	}
}

Reply With Quote
  #8  
Old September 27th, 2006, 06:20 PM
ubergeek ubergeek is offline
Contributing User
Dev Articles Novice (500 - 999 posts)
 
Join Date: Jan 2005
Posts: 600 ubergeek User rank is Private First Class (20 - 50 Reputation Level)ubergeek User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 2 Days 22 h 40 m 27 sec
Reputation Power: 4
Send a message via AIM to ubergeek
First gripe: main() is, by definition and requirement by the standard, always an int function, declared int main() or int main(int argc, char **argv), NOT void main(). And return an int at the end of main() to tell the OS whether your program succeeded (as a general rule 0 means success and 1 means failure).

Now, about the counting answers, this is what the program says:
keep taking answers, as long as they're wrong and the count stays less than two. Then, when the count is equal to two, yell about too many tries.
-->If you change that to >=3, then after the while loop which exited when count==2, it sees that count < 3, so it goes to the else, which says correct, no matter what a was.

The way I fixed this was to declare another int variable called correct. (I would have used bool, but C doesn't have a bool type.) After each scanf, i set
Code:
correct = (a == ans);

Then I changed the first loop from (a != ans) to (!correct) and the if statement from (count >= 2) to (!correct).

Reply With Quote
  #9  
Old September 28th, 2006, 02:00 AM
henno2000 henno2000 is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Sep 2006
Posts: 59 henno2000 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 12 h 52 m 10 sec
Reputation Power: 3
Thanks mate, now I see that you need anoother check statement before you enter the loop, instead of just checking within the loop

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > Help with the rand function


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump



 Free IT White Papers!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

Request Your Free Technology Downloads!
 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

Request Your Free Technology Downloads!
 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

Request Your Free Technology Downloads!
 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

Request Your Free Technology Downloads!
 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

Request Your Free Technology Downloads!
 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2009 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway
Stay green...Green IT