0

我正在使用 C 编写一个小型家庭作业程序,我遇到了一个非常不寻常的问题。我使用 Visual Studio 2012 在 C 中对此进行了编码。程序编译时没有错误,并且它还在 cmd 中运行,直到某个阶段它崩溃并出现异常。请忽略程序的逻辑,我剪掉了一些部分以专注于错误本身。我真的很感激这方面的一些帮助。谢谢你!

这是程序:

#include <stdio.h>
#include <math.h>

int main( void )
{
    int menuinput;
    int austinHour, austinMinute;
    int irishHour, irishMinute;
        printf("Insert a Number from 1-11 to select menu option: ");
        scanf_s("%d",&menuinput);
        switch(menuinput)
        {
            case 1:
            { 
                irishHour=0;
                irishMinute=0;
                austinHour=0;
                austinMinute =0; 
                printf("Enter Austin time: ");
                scanf_s("%d %d",austinHour,austinMinute);
                irishHour = (austinHour + 61);
                printf("%d %d",irishHour, austinMinute);
            }
                }

当我尝试运行程序时,错误如下:

First-chance exception at 0x62ACD745 (msvcr110d.dll) in Lab2.exe: 0xC0000005: Access violation writing location 0x00000000.

If there is a handler for this exception, the program may be safely continued.
4

2 回答 2

3

你有一个错字,你没有传递参数的地址scanf

 printf("Enter Austin time: ");
 scanf_s("%d %d",&austinHour,&austinMinute);
                 ^^          ^^

我说这是一个错字,因为您在第一次使用 scanf 时就这样做了:

scanf_s("%d",&menuinput);
于 2013-01-28T07:16:44.290 回答
0

除了 Alok Save 的回答,我想知道您是否在使用 scanf 时遇到了神秘的行为错误。我准备了一些要考虑的问题。如果您需要手册,可以在此 scanf 手册中找到答案。

  1. int x = scanf("%d %d", &foo, &bar);如果我输入“123 hello”,x 会是什么?
  2. 你会用哪个词来描述 bar 的价值?
  3. 您希望 getchar() 返回哪个字符?
  4. do { x = scanf("%d", &bar); printf("x: %d\n", x); } while (x == 0);考虑到上次转换失败后仍保留在流中的“hello”,您对这段代码有何期望?
  5. int y = scanf("%d", &foo);如果 scanf 在将任何内容放入 foo 之前遇到 EOF,您会期望 y 为正吗?在 Windows 中按 CTRL+Z 或在 Linux 中按 CTRL+D 可以将 EOF 发送到标准输入。
  6. int z = scanf("%d %d", &foo, &bar);假设 scanf 成功地为两个变量 foo 和 bar 赋值,你期望 z 是什么?
于 2013-01-28T08:00:15.720 回答