简体   繁体   English

C程序抽奖

[英]C program lottery

I have homework with the following instruction:我有以下说明的作业:

This program requires the student to write a C program that will generate one year of 3-digit lottery numbers between 000 and 999. When the program begins the user will be asked to buy one year of lottery tickets.该程序要求学生编写一个 C 程序,该程序将生成 000 到 999 之间的 3 位数彩票号码为一年。当程序开始时,将要求用户购买一年的彩票。 It cost $1.00 for each ticket.每张票的价格为 1.00 美元。 The user will provide his or her favorite number.用户将提供他或她最喜欢的号码。 Each ticket purchased with have the exact same number that was provided from the user.购买的每张票都有与用户提供的完全相同的号码。 The program will output the number of times the user's ticket matched the daily number from the lottery.该程序将 output 用户的彩票匹配的次数与每天的彩票数量相匹配。 Each time the user hits the lottery he or she wins $500.00.每次用户中奖时,他或她赢得 500.00 美元。 The final output will be the number of times the user hit the lottery during the past year.最终的 output 将是用户在过去一年中中奖的次数。 The program will calculate and display the amount of money the user has after one-year passes.该程序将计算并显示一年后用户拥有的金额。

I feel like I'm missing something or program not written correctly Thanks in advance Here is my code我觉得我遗漏了一些东西或程序写得不正确提前谢谢这是我的代码

#include <stdlib.h>
int main()
{
    int fav_number;
    int i;
    int total_wins = 0;
    printf("Please buy one year of lottery It just costs $1 a day!!!!\n");
    printf("Enter your favourite number\n");
    scanf_s("%d", &fav_number);
    for (i = 0; i < 365; i++) {
        int random_number = (rand() % 1000);
        if (random_number == fav_number) {
            total_wins += 1;
        }
    }
    printf("Total number of wins in the past year = %d \n", total_wins);
    printf("Money he had after one year passes = $%d \n", total_wins * 500-365);
}

Everythings looks fine except that you need to include stdio.h as it is needed for scanf and printf functions to execute一切看起来都很好,除了你需要包含 stdio.h 因为 scanf 和 printf 函数需要它来执行

I'm assuming that you are wondering why your program is not compiling.我假设您想知道为什么您的程序没有编译。

This is because you have not included stdio.h as a header.这是因为您没有将 stdio.h 作为 header 包含在内。 The functions printf (and scanf_s since you are using a Microsoft compiler) are defined in this header.函数 printf(和 scanf_s,因为您使用的是 Microsoft 编译器)在此 header 中定义。 You just need to include it.你只需要包括它。

In other words, add #include <stdio.h> .换句话说,添加#include <stdio.h>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM