Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 132,126 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,019 people online right now. Registration is fast and FREE... Join Now!




Guessing number game

 
Reply to this topicStart new topic

Guessing number game, user enter the numbers to guess between

cyril27cherian
post 4 Oct, 2008 - 09:54 AM
Post #1


New D.I.C Head

*
Joined: 4 Oct, 2008
Posts: 4

I'm writing a program where
1. ask the user to enter two integers
2. the computer will randomly select a number between those two integers (inclusive)
3. the computer will then ask the user to guess the number it selected then:
a.say if it's too high or too low if it's wrong
b.print that the guess is correct and say the number or guesses it took if it was right

I need help starting out the program in how the computer will select a random number between the numbers the user inputs

My teacher told me to first do an if statement with if(n1 > n2) and if it's true :
temp = n1;
n1 = n2;
n2 = temp;

and if it's false pick a random number

This is supposed to check if the user didn't input the same number right?

CODE

#include <cstdlib>
#include <iostream>
#include <ctime>


using namespace std;

int main()
{
    int n1;
    int n2;
    int count = 0;
    int temp;
    
    cout << "Please enter two integers: " << endl;
    cin  >> n1 >> n2;
    cout << endl;
    
    if (n1 > n2)
    {  
       temp = n1;
       n1   = n2;
       n2   = temp;
    }



This post has been edited by cyril27cherian: 4 Oct, 2008 - 09:58 AM
User is offlineProfile CardPM

Go to the top of the page

Martyr2
post 4 Oct, 2008 - 12:03 PM
Post #2


Programming Theoretician

Group Icon
Joined: 18 Apr, 2007
Posts: 5,012



Thanked 171 times

Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions


Well so far so good, you just need to add an else if to test if they are equal, otherwise go on to selecting a random number using the two numbers. smile.gif
User is offlineProfile CardPM

Go to the top of the page

UG Cyber
post 4 Oct, 2008 - 05:28 PM
Post #3


D.I.C Head

**
Joined: 24 Jul, 2008
Posts: 184



Thanked 3 times
My Contributions


Well there is a rand()% function. use like this
CODE

int RandomNumber;
int HighestNum = 100;
RandomNumber = rand()%100;


But the downside is that the compilier has set numbers that it uses so the numbers will always be the same icon_down.gif .
If you multi-thread it you can prevent this.
like this (Bad habit by the way)
cpp

#include <iostream>
#include <windows.h>
using namespace std;

int RandomNumber;
int SetNumber;
int Highest, Lowest;

DWORD WINAPI SetRandom(LPVOID)
{
while (true)
{
RandomNumber = rand()%Highest;
}
}

int main(void)
{
int Number;
cout << "Highest Number: ";
cin >> Highest;
CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)&SetRandom, 0, 0, NULL); //Start generating
cout << endl << "Lowest: ";
cin >> Lowest;

while (true)
{
Number = Highest;
if (Number > Lowest)
{
break;
}
}
}


I wrote that in browser so it may not work.....But basicly it will start generating the number when you pick the highest but it wont pick the number until the user picks the lowest number allowed. You shouldnt get the same numbers over and over agen.

Hope this helped
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/21/08 11:29AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month