Jump to content

Number generator

- - - - -

  • Please log in to reply
4 replies to this topic

#1
Braydon Vaughan

Braydon Vaughan

    Newbie

  • Members
  • PipPip
  • 15 posts
I wrote this little matching game

#include <iostream>
#include <Windows.h>
#include <conio.h>

int main()
{
std::cout<<"Good day, would you like to play a guessing game?\n";
Sleep(2000);
std::cout<<"I want you to guess a number from 1 to 20.\n";
Sleep(2000);
std::cout<<"See how many times it takes you to get the correct number\n";
Sleep(2000);
std::cout<<"This is generated by the computer.\n";
Sleep(2000);
std::cout<<"The computer will not track your losses, so dont cheat!\n";
Sleep(2000);
std::cout<<"Go ahead and type in a number.\n";
Sleep(1000);


int r;
std::cin>> r;
std::cout << "You entered: " << r << std::endl << std::endl;
std::cout<<"FAIL, try again.\n";

while (r != 17)
{
std::cin>> r;
std::cout << "You entered: " << r << std::endl << std::endl;


if (r != 17)
std::cout<<"Tsk tsk, this is not the correct number try again!\n";

else
std::cout<<"Good job! You won, this time.\n";
Sleep(2000);
}
return 0;
}


Is there any simple function i can make that will change the number to be guessed each time a new game begins?

#2
AKMafia001

AKMafia001

    Programmer

  • Members
  • PipPipPipPip
  • 119 posts
Yes! you can use the rand() function to generate random numbers. e.g. rand()%6 or rand()%100 etc.
It would generate more random if you use it with the system clock.
I think i'm able to write a code for printing "Hello, World!". Proud of that!

#3
Braydon Vaughan

Braydon Vaughan

    Newbie

  • Members
  • PipPip
  • 15 posts
Any way I can keep the numbers between 1-20

#4
AKMafia001

AKMafia001

    Programmer

  • Members
  • PipPipPipPip
  • 119 posts

Braydon Vaughan said:

Any way I can keep the numbers between 1-20

Of course yes! Just use the basic maths. Use the operator % to restrict it.
Have a look at the example below,

#include<iostream.h>

#include<stdlib.h> For rand() & srand()

#include<time.h> For Time objects


using std::cout;

using std::endl;


int main()

{


time_t t; // Time type variable


time(&t); // get the time


srand((unsigned int) t); // convert the time to unsigned int


cout << rand()%20 << endl;  // have a look at the two outputs

cout << rand()%20 << endl;


return 0;

}


The srand() is used to seed the rand() with the initial value. It provides the rand() different initial values through every execution so it may generate different numbers.

There is a hint in this program which will help you to reduce redundancy. Even it can be more efficent. Find it out!;)

Hope it Helped!
I think i'm able to write a code for printing "Hello, World!". Proud of that!

#5
Braydon Vaughan

Braydon Vaughan

    Newbie

  • Members
  • PipPip
  • 15 posts
Thanks for the help. I am really at the moment making small programs in order to better myself at C++.

:thumbup1:




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users