为什么这段代码不起作用?

时间:2015-10-06 15:05:34

标签: c exit

这个问题与SPOJ教程问题有关:

你的程序是使用蛮力方法来找到生命,宇宙和一切的答案。更确切地说......从输入到输出重写小数字。读取数字42后停止处理输入。输入的所有数字都是一位或两位数的整数。

我想运行程序而不使用if -else语句,但程序不起作用。有人可以告诉我,我做错了什么?

(defun open-outside-emacs (orig-fun size op-type filename)
  (if (not (string-equal op-type "open"))
      (apply orig-fun size op-type filename)
    (call-process "/usr/bin/xdg-open" nil 0 nil (expand-file-name filename))
    (error "opened externally")))
(advice-add 'abort-if-file-too-large :around #'open-outside-emacs)

1 个答案:

答案 0 :(得分:1)

这是获得所需结果的快捷方式。

由于您希望用户输入数字(由多个字符组成),因此您需要使用scanf()而不是getchar()

scanf()文档http://www.cplusplus.com/reference/cstdio/scanf/

#include <stdio.h>
#include <stdlib.h>
int main()
{
  int i = 0;

  while (i != 42)
  { 
    scanf("%d", &i);
    printf("You have entered : %d\n", i);
  }

  printf("You have successfuly entered 42!\n");
  exit(0);  
}

希望这有帮助。

相关问题