【问题标题】:Dice game program will not run but will not stop at 5 rolls [duplicate]骰子游戏程序不会运行,但不会在 5 次掷骰时停止 [重复]
【发布时间】:2015-07-09 00:51:02
【问题描述】:

我收到两个错误:

Error 3 error LNK2019: unresolved external symbol _WinMain@16 referenced in function ___tmainCRTStartup

和:

Error 4 error LNK1120: 1 unresolved externals

由于我无法让程序运行,我也不确定案例“A”是否会产生滚动。我不想在案例“A”中显示结果,我的直觉是 scanf 不应该在那里,但我可能错了。

这是我的代码:

#include<stdlib.h>
#include<stdio.h>
#include <time.h>
#include <ctype.h>

#define PAUSE system("pause")


int main ()
{
    int diceOne, diceTwo, diceThree;
    int currentDiceSum=0, totalDiceSum=0;
    char choice;
    int count = 0;

    srand((unsigned)time(NULL));

    diceOne      =rand()%6+1;
    diceTwo      =rand()%6+1;
    diceThree    =rand()%6+1;

    do {
        printf("\n Roll the dice, but you only get 5 rolls! You can't play forever, you know. \n");

        printf("Main Menu\n");
        printf("A.Roll the Dice\n");
        printf("B.Display the Result of Last Roll\n");
        printf("C.Quit\n");

        printf("Enter your choice:   ");
        scanf(" %c", &choice);

        choice = toupper(choice);

        switch(choice) {
            case 'A': 
                printf("Dice are rolled!'\n");

                diceOne      =rand()%6+1;
                diceTwo      =rand()%6+1;
                diceThree    =rand()%6+1;


                count ++;
                break;
            case 'B':  
                if (count = 0) {
                    printf("Please roll the dice atleast once\n");
                }  else {
                    printf("Dice 1: %d\n", diceOne);
                    printf("Dice 2: %d\n", diceTwo);
                    printf("Dice 2: %d\n", diceThree);

                    currentDiceSum = diceOne + diceTwo + diceThree;
                    printf("Dice Total: %d\n", currentDiceSum);
                    totalDiceSum+= currentDiceSum;
                }
                break;
            case 'C':
                if (count == 5)  
                    printf("Number of rolls: %d\n", count);

                printf("Total of all dice for all rolls:%d\n",totalDiceSum);
                printf("Goodbye, hope to see you again!!!\n");
                PAUSE;
                break;

            default:
                printf("Was not a valid menu choice (Please enter A,B,C\n");
                break;
        }
    } while (choice!= 'C'); 
}

【问题讨论】:

  • 请正确缩进你的代码;它不可读。该错误看起来好像缺少某些库。我不知道windows,但是:难道你正在编译一个windows-appication而不是一个控制台应用程序?
  • @Olaf:我在队列中有一个修复格式的编辑。
  • 另外,当您可能要检查 count == 0 是否是 case 'B': 时,您正在分配 count = 0
  • 哪个编译器?哪个平台?信息不足
  • 您的 scanf 会覆盖您刚刚生成的随机值。此外,您的一些 printfs 缺少换行符。

标签: c loops srand


【解决方案1】:

错误 3 错误 LNK2019:函数 ___tmainCRTStartup 中引用的未解析的外部符号 _WinMain@16

这是链接器错误消息。

我认为您已经创建了一个 GUI Windows 应用程序,因为它需要 WinMain 作为入口点。

您应该在链接器设置中将subsystem 更改为console

如果您使用的是 Visual Studio,请转到

Project properties 然后c\c++ 然后linker -&gt;system-&gt; Subsystem :CONSOLE(/SUBSYSTEM CONSOLE)

对于代码块 -

转到Project,然后转到Properties。在Build targets 选项卡下,请参阅组合框Type。对于Debug 目标,将其设置为“控制台应用程序”

另外,您的程序运行良好。它没有任何额外的错误,也不会崩溃。

【讨论】:

    猜你喜欢
    • 2012-02-29
    • 2015-12-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多