Home > Software engineering >  C Program gives funny output in VSCode but runs fine in online compiler
C Program gives funny output in VSCode but runs fine in online compiler

Time:11-28

I have received a code from a friend of mine which was giving garbage outputs and hence I tried to run it on my PC too. The same problem is happening with me and I don't know how to fix it. Here is the code to accept and display a few information of bank customers from the user and display it.

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

typedef struct Customer {
    char name[20];
    int date;
    int month;
    int year;
    int amount;
    int type_of_account;
} cus;

void display(int n);
void data_entry_of_customers(int n);

int main() {
    int i, n, choices;
    printf("\n Enter the number of customer you want to enter their data : ");
    scanf("%d", &n);
    do {
        printf("\n Enter your choice :");
        scanf("%d", &choices);
        switch (choices) {
        case 1:
            data_entry_of_customers(n);
            break;

        case 2:
            display(n);
            break;

        case 3:
            exit(0);
            break;

        default:
            {
                printf("\n Invalid Choice");
            }
        }
        i  ;
    } while (choices != 3);
    getch();
    return 0;
}

void data_entry_of_customers(int n) {
    cus c[n];
    int i;
    for (i = 0; i < n; i  ) {
        printf("\n Enter the name of the customer%d : ", i   1);
        scanf("%s", c[i].name);
        printf("\n Enter the date when the account was opened by customer : ");
        scanf("%d", &c[i].date);
        printf("\n Enter the month when the account was opened by customer : ");
        scanf("%d", &c[i].month);
        printf("\n Enter the year when the account was opened by customer : ");
        scanf("%d", &c[i].year);
        printf("\n Enter the amount disposited by the customer : ");
        scanf("%d", &c[i].amount);
    }
}

void display(int n) {
    cus c[n];
    int i;
    for (i = 0; i < n; i  ) {
        printf("\n The name of the customer%d is %s", i   1, c[i].name);
        printf("\n The date-month-year when the account was opened by customer%d is %d-%d-%d", i   1, c[i].date, c[i].month, c[i].year);
        printf("\n The amount when the account was opened by customer%d is %d", i   1, c[i].amount);
        printf("\n");
    }
}

enter image description here enter image description here

I tried to look for errors in my code initially but it looked good to me hence I proceeded with an online compiler and now I am clueless on how should I fix the IDE's problem

CodePudding user response:

The c array is defined as a local variable in both the display and the data_entry_of_customers functions. The values entered are lost when the function returns and the contents of c in the display function are indeterminate producing undefined behavior (garbage output).

The program may seem to behave as expected if, by chance, the position and contents of the c array in both functions happens to be the same.

You should more the definition of the array to the main function and pass it to data_entry_of_customers() and display().

Here is a modified version:

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

typedef struct Customer {
    char name[20];
    int date;
    int month;
    int year;
    int amount;
    int type_of_account;
} cus;

void display(int n, cus c[n]);
void data_entry_of_customers(int n, cus c[n]);

int main() {
    int n, choices;

    printf("\n Enter the number of customers : ");
    if (scanf("%d", &n) != 1)
        return 1;
    cus cus_array[n];

    for (;;) {
        printf("\n Enter your choice :");
        if (scanf("%d", &choices) != 1)
            break;

        switch (choices) {
        case 1:
            data_entry_of_customers(n, cus_array);
            break;

        case 2:
            display(n, cus_array);
            break;

        case 3:
            return 0;

        default:
            printf("\n Invalid Choice\n");
            break;
        }
    }
    return 0;
}

void data_entry_of_customers(int n, cus c[n]) {
    for (int i = 0; i < n; i  ) {
        printf("\n Enter the name of the customer%d : ", i   1);
        scanf("s", c[i].name);
        printf("\n Enter the date when the account was opened by customer : ");
        scanf("%d", &c[i].date);
        printf("\n Enter the month when the account was opened by customer : ");
        scanf("%d", &c[i].month);
        printf("\n Enter the year when the account was opened by customer : ");
        scanf("%d", &c[i].year);
        printf("\n Enter the amount deposited by the customer : ");
        scanf("%d", &c[i].amount);
    }
}

void display(int n, cus c[n]) {
    for (int i = 0; i < n; i  ) {
        printf("\n The name of the customer%d is %s", i   1, c[i].name);
        printf("\n The date-month-year when the account was opened by customer%d is %d-%d-%d", i   1, c[i].date, c[i].month, c[i].year);
        printf("\n The amount when the account was opened by customer%d is %d", i   1, c[i].amount);
        printf("\n");
    }
}

Note these remarks:

  • void display(int n, cus c[n]); defines a function that receives an int and a pointer to an array of cus structures. The prototype is equivalent to: void display(int n, cus c[]); and void display(int n, cus *c);... A possible improvement is to specify that the cus structures are not modified by the display function, thus change the prototype to void display(int n, const cus *c);

  • the return value of scanf should be tested to detect and handle invalid input. Writing separate functions to read a string and an integer would help make the code more reliable and readable.

Here is an alternate approach:

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

typedef struct Customer {
    char name[20];
    int date;
    int month;
    int year;
    int amount;
    int type_of_account;
} cus;

char *enter_string(const char *prompt, char *dest, size_t size);
int enter_int(const char *prompt, int min, int max);

void display(int n, const cus *c);
void data_entry_of_customers(int n, cus *c);

int main() {
    int n = enter_int("Enter the number of customers", 1, 1000);
    cus cus_array[n];

    for (;;) {
        switch (enter_int("Enter your choice", 1, 3)) {
        case 1:
            data_entry_of_customers(n, cus_array);
            break;

        case 2:
            display(n, cus_array);
            break;

        case 3:
            return 0;
        }
    }
}

char *enter_string(const char *prompt, char *dest, size_t size) {
    int c;
    size_t i = 0;

    printf("%s: ", prompt);
    fflush(stdout);
    while ((c = getchar()) != EOF && c != '\n') {
        if (i   1 < size) {
            dest[i  ] = (char)c;
        }
    }
    printf("\n");
    if (i == 0 && c == EOF) {
        printf("unexpected end of file\n");
        exit(1);
    }
    if (i < size)
        dest[i] = '\0';
    return dest;
}

int enter_int(const char *prompt, int min, int max) {
    int c, res, value;

    for (;;) {
        printf("%s: ", prompt);
        fflush(stdout);
        res = scanf("%d", &value);
        while ((c = getchar()) != EOF && c != '\n')
            continue;
        printf("\n");
        if (res == 1) {
            if (value >= min && value <= max)
                return value;
            printf("invalid value, must be between %d and %d\n", min, max);
        } else {
            if (c == EOF) {
                printf("unexpected end of file\n");
                exit(1);
            }
            printf("invalid input, enter a number between %d and %d\n", min, max);
        }
    }
}

void data_entry_of_customers(int n, cus *c) {
    char prompt[80];
    for (int i = 0; i < n; i  ) {
        snprintf(prompt, sizeof prompt, "Enter the name of the customer%d", i   1);
        enter_string(prompt, c[i].name, sizeof(c[i].name));
        c[i].date = enter_int("Enter the date when the account was opened by customer",
                              1, 31);
        c[i].month = enter_int("Enter the month when the account was opened by customer",
                               1, 12);
        c[i].year = enter_int("Enter the year when the account was opened by customer",
                              1900, 2100);
        c[i].amount = enter_int("Enter the amount disposited by the customer",
                                0, 1000000000);
    }
}

void display(int n, const cus *c) {
    for (int i = 0; i < n; i  ) {
        printf("The name of the customer%d is %s\n", i   1, c[i].name);
        printf("The date-month-year when the account was opened by customer%d is %d-%d-%d\n",
               i   1, c[i].date, c[i].month, c[i].year);
        printf("The amount when the account was opened by customer%d is %d\n",
               i   1, c[i].amount);
        printf("\n");
    }
}
  • Related