遇到 srand 错误,想知道是否有其他方法可以改进此代码



基本上这是一个玩骰子的程序,但我对srand有问题。 它给出一个错误,指出"隐式转换丢失整数精度:"time_t"(又名"long"(到'无符号 int'" 这是为什么呢?

我还想知道是否有其他方法可以改进此代码? 目前我只知道stdio.h,我知道有一些像iostream或类似的东西。 有没有办法也向我解释这些。

这是代码。 我知道CRT安全没有警告是忽略一些错误,但我仍然想知道为什么会弹出错误?

#define _CRT_SECURE_NO_WARNINGS 
#define MAX_CHIPS 400 
#include <stdio.h> 
#include <stdlib.h> 
#include <time.h>   
void display_details(void); 
int throws_die(void); 
int main() so on
{
int die1, die2, throw_sum; 
int PointToMake; 

int Games_Played = 0; 
int Games_Won = 0;  
int Games_Lost = 0; 

int CurrentChips = MAX_CHIPS; 
int BetChips;
char PlayerName[30];   
char PlayAgain; 

display_details(); 
srand(time(NULL));
printf("Would you like to play Test your luck [y|n]?n"); 
scanf(" %c", &PlayAgain);
while ((PlayAgain == 'y') && (CurrentChips > 0))
{
if (Games_Played == 0)//shows how many games played which is obviously 0
{
printf("A new chellenger! What is your name?n");
scanf(" %s", &PlayerName);           
printf("Hi %s!n", PlayerName);     
}
Games_Played = Games_Played + 1;//each new game it will increase by 1
printf("Number of chips: %dn", CurrentChips);
printf("Place your bet:n "); 
scanf("%d", &BetChips); 

while ((BetChips < 0) || (BetChips > CurrentChips))  
{
printf("uuh ....You can only bet the chips you have.. (0-%d)!n", CurrentChips);
printf("Place your bet: n");
scanf("%d", &BetChips);
}

die1 = throws_die();
die2 = throws_die();
throw_sum = die1 + die2;
printf("You Got: %d + %d = %dn", die1, die2, throw_sum);

if ((throw_sum == 7) || (throw_sum == 12))
{
//+1 to games won
Games_Won = Games_Won + 1;
//adding to bet balance
CurrentChips = CurrentChips + BetChips;
printf("XXXXX  Winner! d(^_^)  XXXXXn");
}
else if ((throw_sum == 2) || (throw_sum == 3) || (throw_sum == 10))
{
Games_Lost = Games_Lost + 1;
CurrentChips = CurrentChips - BetChips;
printf("XXXXX  Loser! :( XXXXXn");
}
else
{
PointToMake = throw_sum;
printf("Points to make is: %dn", PointToMake);

die1 = throws_die();
die2 = throws_die();
throw_sum = die1 + die2;
printf("    |--->> Spinned: %d + %d = %dn", die1, die2, throw_sum);

while (throw_sum != PointToMake && throw_sum != 7)
{
die1 = throws_die();
die2 = throws_die();
throw_sum = die1 + die2;
printf("    |--->> Spinned: %d + %d = %dn", die1, die2, throw_sum);
}

if (throw_sum == PointToMake)
{
printf("XXXXX  Winner! (x2 the bet) XXXXXn");
//x2 added to balance
CurrentChips = CurrentChips + 2 * BetChips;
Games_Won = Games_Won + 1;
}
else
{
Games_Lost = Games_Lost + 1;
CurrentChips = CurrentChips - BetChips;
printf("XXXXX  Loser!:(  XXXXXn");
}
}

if (CurrentChips > 0)
{
printf("You now have %d chips.n", CurrentChips);
printf("============================nn");
printf("Play Again %s [y|n]? ", PlayerName);   
scanf(" %c", &PlayAgain);   
printf("============================n");
}
else  
{
printf("you're out of chips.n");
printf("XXXXX  GG TRY AGAIN NEXT TIME  XXXXXn");
}
}

if (Games_Played == 0)
{
printf("Oh well.... better luck next time!n");
}
else if (Games_Played == 1)
{
printf("%s played %d game and is cashing out with %d chips!n", PlayerName, Games_Played, CurrentChips);
printf("   |---> Games won: %dn", Games_Won);
printf("   |---> Games lost: %dn", Games_Lost);
printf("Thanks for playing, come again to test that luck of yours %s.n", PlayerName);
}
else
{
printf("%s played %d games and is cashing out with %d chips!n", PlayerName, Games_Played, CurrentChips);
printf("   |---> Games won: %dn", Games_Won);
printf("   |---> Games lost: %dn", Games_Lost);
printf("nThanks for playing, come again to test that luck of yours %s!n", PlayerName);
}
return 0;
}
void display_details(void)  
{
printf("File     :.... assignment.cn");
printf("Author   :.... n");
printf("Stud ID  :.... n");
printf("Email ID :.... n");
printf("This is my own work as defined by the n");
printf("University's Academic Misconduct Policy.n");
}
int throws_die(void)
{
return 1 + rand() % 6;  
srand

或多或少是这样定义的:

void srand(unsigned int seed);

time或多或少是这样定义的:

__int64 time(__int64 *t);

因此,您将__int64(64 位(传递到unsigned int(32 位(参数中,如果没有潜在的运行,这是不可能的。这就是警告消息告诉您的内容。

相关内容

  • 没有找到相关文章

最新更新