C 为什么在输入范围内的数字时,代码会随机抽取和给出数字?

C 为什么在输入范围内的数字时,代码会随机抽取和给出数字?,c,calculator,C,Calculator,我正在用简单的C语言制作一个普通的基本计算器。我用double来存储数字,我输入9和81作为数字,但代码会取一些随机数并使用它们来计算答案。代码如下: #include <stdio.h> int main(void){ double i,l,j; char k; loop: printf("Enter integer 1 : "); scanf("%i",&i); printf("Enter integer 2 : "); scanf("%

我正在用简单的C语言制作一个普通的基本计算器。我用double来存储数字,我输入9和81作为数字,但代码会取一些随机数并使用它们来计算答案。代码如下:

#include <stdio.h>

int main(void){
double i,l,j;
char k;
    loop: printf("Enter integer 1 : ");
    scanf("%i",&i);
    printf("Enter integer 2 : ");
    scanf("%i%*c",&j);
    printf("Enter code for operation  p = +, m = -, s = *, 4 = d  : ");
    scanf("%c%*c",&k);
    switch(k){
        case 'p' : l = i + j; printf("%d + %d = %d\n",&i,&j,&l);
            break;
        case 'm' : l = i - j; printf("%d - %d = %d\n",&i,&j,&l);
            break;
        case 's' : l = i * j; printf("%d * %d = %d\n",&i,&j,&l);
            break;
        case 'd' : l = i / j; printf("%d / %d = %d\n",&i,&j,&l);
    }
    exit: printf("enter \"r\" to continue or \"x\" to exit.....\n");
    char exit_v;
scanf("%c" , &exit_v);
    switch(exit_v){
      case 'r':goto loop;
        break;
      case 'x':return 0;
        break;
     default: { printf("Enter valid arguments..." );
                goto exit;}

        }
}
当我在范围内输入数字时,为什么它会以随机整数作为输入

对代码的修复(编辑由注释通知):

#包括
内部主(空)
{
双i,l,j;
chark;
循环:
printf(“输入整数1:”;//使用%lf读取双精度
scanf(“%lf”、&i);
printf(“输入整数2:”);
scanf(“%lf”、&j);
printf(“输入操作p=+,m=-,s=*,4=d:”)的代码;
scanf(“%c”,k);//您不需要使用&k来读取单个字符
开关(k)
{
案例“p”:
l=i+j;
printf(“%lf+%lf=%lf\n”,i,j,l);//不读取地址,直接获取值
//其次,您使用%d读取了一个与变量不兼容的double,
//使用%lf读取/写入双精度
打破
案例“m”:
l=i-j;
printf(“%lf-%lf=%lf\n”,i,j,l);
打破
案例s:
l=i*j;
printf(“%lf*%lf=%lf\n”,i,j,l);
打破
案例“d”:
l=i/j;
printf(“%lf/%lf=%lf\n”,i,j,l);
}
出口:
printf(“输入\“r\”继续,或输入\“x\”退出…\n”);
char exit_v;
scanf(“%c”,退出);
开关(出口v)
{
案例“r”:
后藤环;
打破
案例“x”:
返回0;
打破
违约:
{
printf(“输入有效参数…”);
转到出口;
}
}

作为编程技巧,请记住下次在编码系统中使用
goto
语句是一种不好的做法。

您希望这一行做什么?'scanf(“%i%*c”,&j)“;”:
i匹配一个可选的有符号整数;
。如果要读取整数,请将变量声明为int而不是double:
int i,l,j;
@nivpeled获取数字并丢弃换行符显然,您需要为
double
变量使用
double
格式说明符。您使用的是
int
格式pecifer.所以要么更改它,要么切换到
int
变量。而
printf
参数应该是实际值,而不是指针。打开编译器警告并注意它们。为什么在这里发布编译器已经告诉您有bug的代码?
Enter integer 1 : 9
Enter integer 2 : 81
Enter code for operation  p = +, m = -, s = *, 4 = d  : p
-511875960 + -511875976 = -511875968
enter "r" to continue or "x" to exit.....
x

[Process completed - press Enter]
#include <stdio.h>

int main(void)
{
    double i, l, j;
    char k;
loop:
    printf("Enter integer 1 : "); // USE %lf FOR READING DOUBLES
    scanf("%lf", &i);
    printf("Enter integer 2 : ");
    scanf("%lf", &j);
    printf("Enter code for operation  p = +, m = -, s = *, 4 = d  : ");
    scanf("%c", k); // You don't need to use &k to read a single char
    switch (k)
    {
    case 'p':
        l = i + j;
        printf("%lf + %lf = %lf\n", i, j, l); // don't read addresses, get values directly
        // secondly you've used %d to read a double, that's incompatible with the variables,
        // use %lf to read/write doubles
        break;
    case 'm':
        l = i - j;
        printf("%lf - %lf = %lf\n", i, j, l);
        break;
    case 's':
        l = i * j;
        printf("%lf * %lf = %lf\n", i, j, l);
        break;
    case 'd':
        l = i / j;
        printf("%lf / %lf = %lf\n", i, j, l);
    }
exit:
    printf("enter \"r\" to continue or \"x\" to exit.....\n");
    char exit_v;
    scanf("%c", exit_v);
    switch (exit_v)
    {
    case 'r':
        goto loop;
        break;
    case 'x':
        return 0;
        break;
    default:
    {
        printf("Enter valid arguments...");
        goto exit;
    }
}