首页 > 解决方案 > 如何在我的 C 程序中纠正这个错误?

问题描述

在这个程序中,我首先尝试编写我的简单计算器方程。然后,我会得到我的结果。当我编写这个程序时,我收到了两个警告:

  1. usertext 使用未初始化的内存printf("%c", usertxt[i]);
  2. scanf_s: 没有足够的参数传递给格式字符串 wherescanf_s("%c", &myoperator);
#include<stdio.h>

#define     SIZE        50

int i = 0;

int main() {

char usertxt[SIZE], myoperator;
printf("addition='+',subtraction='-',multiplication='*',division='/'\n");

int x,myarray[SIZE];
printf("How many numbers should be entered? ");
scanf_s("%d", &x);

    for ( i = 0; i < x; i++) {
        scanf_s("%c", &myoperator);
        if (myoperator == '\0') {
            break;
        switch (myoperator) {
        case '+':printf("Addition operation\n");
            printf("  Enter your number: ");
            scanf_s("%d", &myarray[i]);
            usertxt[i] = printf("%d%c", myarray[i], myoperator);
            break;
        case '-':
            printf("Subtraction operation\n");
            printf("Enter your numbers: ");
            scanf_s("%d", &myarray[i]);
            usertxt[i] = printf("%d%c", myarray[i], myoperator);
            break;
        case '*':
            printf("Multiplication operation\n");
            printf("Enter your numbers: ");
            scanf_s("%d", &myarray[i]);
            usertxt[i] = printf("%d%c", myarray[i], myoperator);
            break;
        case '/':
            printf("Division operation\n");
            printf("Enter your numbers: ");
            scanf_s("%d", &myarray[i]);
            usertxt[i] = printf("%d%c", myarray[i], myoperator);
            break;
        };
    }
    
}
    for (int m = 0; m < i; m++) {
        printf("%c", usertxt[i]);
    }

标签: c

解决方案


尝试使用 scanf 而不是 scanf_S


推荐阅读