两数相加
#include<stdio.h>
int main()
{
int firstNumber, secondNumber, sumofTwoNumbers;
printf_s("请输入两个整数");
scanf_s("%d %d", &firstNumber, &secondNumber);
sumofTwoNumbers = firstNumber + secondNumber;
printf_s("%d+%d=%d", firstNumber, secondNumber, sumofTwoNumbers);
scanf_s("");
return 0;
}
这样写没问题,加了char类型出错,存在C++ Stack around the variable 'Operatorstr' was corrupted
#include<stdio.h>
int main()
{
int firstNumber, secondNumber; // 第一个数,第二个数
char Operatorstr; // 运算符
int sumofTwoNumbers; // 和
int differenceofTwoNumbers; // 差
int productofTwoNumbers; // 乘积
int quotient, remainder; // 商,余数
printf_s("2 2 \n");
scanf_s("%d %d", &firstNumber, &secondNumber);
printf_s("+-*/ ");
scanf_s("%s", &Operatorstr,4);
switch (Operatorstr) {
case '+':
sumofTwoNumbers = firstNumber + secondNumber;
printf_s("%d + %d=%d\n", firstNumber, secondNumber, sumofTwoNumbers);
break;
case '-':
differenceofTwoNumbers = firstNumber - secondNumber;
printf_s("%d - %d=%d\n", firstNumber, secondNumber, differenceofTwoNumbers);
break;
case '*':
productofTwoNumbers = firstNumber * secondNumber;
printf_s("%d * %d=%d\n", firstNumber, secondNumber, productofTwoNumbers);
break;
case '/':
quotient = firstNumber / secondNumber;
remainder = firstNumber % secondNumber;
printf_s("%d / %d=%d······%d\n", firstNumber, secondNumber, quotient, remainder);
break;
default:
printf_s("default");
}
scanf_s("");
return 0;
}
作者:꧁执笔小白꧂