Enter numeric data using a for statement in the C language string

Asked 2 years ago, Updated 2 years ago, 65 views

I'm a beginner in C language. You want to store data in each string from a string through an array and output it.

You want to enter each branch's revenue into an array and calculate the total and average sales for the entire branch.

//
void main() {
    const char*arStore[3] = {"Gangnam branch", "Shinchon branch", "Hongdae branch"};
    const length = sizeof(arStore)/sizeof(const);
    double avg = 0.0;
    int sum  = 0;

    for (int i=0; i < length ; i++){
        printf("%s" Enter sales (unit: 10,000 won)\n", arStore[i]);
        scanf("%d",arStore + i);
        sum += arStore[i];
    }
    avg = (double)sum / length;

    printf ("average store sales: %").2lf won\n", avg);
    printf("Total sales of store: %d KRW\n", sum);

}

I'm using Xcode, but after entering sales of 3 branches, (11db) appears and the progress stops. Thread 1: EXC_BAD_ACCESS (code=EXC_I386_GPFLT) error occurs. Where did it go wrong?

c for scanf xcode

2022-09-20 20:03

1 Answers

Please refer to the code below.

#define _CRT_SECURE_NO_WARNINGS

#include <stdio.h>

int main() {
    const char*arStore[3] = { "Gangnam branch", "Shinchon branch", "Hongdae branch";
    const int length = sizeof(arStore) / sizeof(const char*);

    int sales[3];

    double avg = 0.0;
    int sum = 0;

    for (int i = 0; i < length; i++) {
        printf("%s" Enter sales (unit: 10,000 won)\n", arStore[i]);
        scanf("%d", &sales[i]);
        sum += sales[i];
    }
    avg = (double)sum / length;

    printf ("average store sales: %").2f won\n", avg);
    printf("Total sales of store: %d KRW\n", sum);

    return 0;
}


2022-09-20 20:03

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.