I have got a problem about rand(); function in C language. My teacher gave me a assignment about finding the random generated number. ı used rand(); function for generating a random number but my program always generating the same number (42). How can I solve this problem?
#include "stdlib.h"
#include <stdio.h>
#include <dos.h>
#include <windows.h>
#include <locale.h>
int main()
{
int rnd, number=0; //Value definitions.
setlocale(LC_ALL, "Turkish"); //Without this line, line 48 is working incorrectly.
printf("\t\t\t\t-----------------------------------------\n");
printf("\t\t\t\t| |\n");
printf("\t\t\t\t| Welcome to Guess Number Game |\n"); //Introduction.
printf("\t\t\t\t| |\n");
printf("\t\t\t\t-----------------------------------------\n");
printf("Game is starting...\n");
printf("3\n");
Sleep(1000);
printf("2\n");
Sleep(1000); //Game start countdown.
printf("1");
Sleep(1000);
system("cls");
rnd = 1 + rand() % 1000; //Random number is created.
printf("Let's try to guess the number that in my mind :)\n\n");
for (int i = 1; number != rnd; i++)
{
printf("What is your guess?\n");
printf("Number: "); //"For structure" is defined for finding the attempts.
scanf_s("%d", &number);
printf("\n");
if (number < rnd)
{
printf("The secret number is greater than your guess. Try again...\n");
}
else if (number > rnd)
{
printf("The secret number is smaller than your guess. Try again...\n");
}
else
{
printf("Congratulations!!! You know the number at your %d’th guess", i);
}
}
system("PAUSE");
return 0;
}
Aucun commentaire:
Enregistrer un commentaire