gpt4 book ai didi

c - 在简单程序中出现段错误

转载 作者:行者123 更新时间:2023-11-30 20:12:05 25 4
gpt4 key购买 nike

我在学校假期期间一直在自学 C,最近尝试编写一个简单的计算器程序,该程序应该接受两个整数并对它们执行四个操作之一(+-*/),但是每当第一个变量已分配 我收到段错误/核心转储错误消息。我知道这与内存分配有关,并且我尝试使用指针和 malloc,尽管我怀疑我这样做不正确。

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int calculate(int numberOne, int numberTwo, int operator);

int main(){

//Declaring Variables
int numberOne, numberTwo, total, operator;
int *one, *two, *tot, *op;


//Assigning Variables
printf("Integer 1: ");
scanf("%d", numberOne);
printf("\nOperator 1[+] 2[-] 3[*] 4[/] : ");
scanf("%d", operator);
printf("Integer 2: ");
scanf("\n%d", numberTwo);


//Output Calculatoin Through Function
printf("Calculation Complete: %d is the answer", calculate(numberOne, numberTwo, operator));
}

int calculate(int numberOne, int numberTwo, int operator) {

int total = 0;
do{
switch(operator){

case 1:
total = numberOne + numberTwo;
break;

case 2:
total = numberOne - numberTwo;
break;

case 3:
total = numberOne*numberTwo;
break;

case 4:
total = numberOne/numberTwo;
break;

default:
printf("Error, Invalid Operator, Please Enter A New One: ");
scanf("%d", operator);
}
}while(total ==0);

return total;
}

最佳答案

你需要改变

scanf("%d", numberOne);

scanf("%d", &numberOne);   //%d expects a pointer to int argument

同样。

关于c - 在简单程序中出现段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37599334/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com