1

The core idea of this is just to have a array of integers and change the value of them based on teminal inputs. This is my very first project and everything seems to be in order but it does not work, i cant seem to get any idea of what has gone wrong. I think it might have something to do with the array but i'm not sure. When i build it and then run it i get the the outputs from line 10, 11 and 12 but then when i input a number it just says: Process returned -1073741819 (0xC0000005)

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



int main()
{
    int account[1000];
    // Tell you the options
    printf("Check balance : 3\r\n");
    printf("Add cash : 2\r\n");
    printf("Remove cash : 1\r\n");
    // Creates a variable for the loop
    int q;
    // Creates a "infinte loop"
    for(q=0;q<100000;++q){
        printf("Select Action\r\n");
        int acted;
        int am;
        int acc;
        scanf("%s", acted);
        printf("Enter account number\r\n");
        scanf("%s", acc);
        // Checks what option you chose.
        if (acted == 1) {
           printf("Amount of cash to remove");
           scanf("%s", am);
           account[acc]=(account[acc]-am);
        } else if(acted == 2) {
           printf("Amount of cash to add");
           scanf("%s", am);
           account[acc]=(account[acc]+am);
        } else if(acted == 3) {
           printf("Account Number: ",acc);
           printf("Balance of account: ",account[acc]);
        };

        }
}
  • 1
    `scanf("%s", acted)` that is incorrect. Second param needs to be `char *` whereas what you have given is not even a pointer. Some of the other calls have similar error. Suggest reviewing basic scanf docs and examples. – kaylum Jul 23 '22 at 12:51
  • Your code has lots of issues, but the reason for the bad return value is the lack of a return statement. Therefore the value on top of the stack is returned. `return 0` at the end of your function – DownloadPizza Jul 23 '22 at 13:07
  • @DownloadPizza: When program execution reaches the terminating `}` of `main`, zero is returned, per C 2018 5.1.2.2.3 1. – Eric Postpischil Jul 23 '22 at 13:33

3 Answers3

3

Use the scanf function as follows.

scanf("%d", &acted);

Use the link below to learn more.

scanf function

Change you code as below.

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

int main()
{
    int account[1000];
    // Tell you the options
    printf("Check balance : 3\r\n");
    printf("Add cash : 2\r\n");
    printf("Remove cash : 1\r\n");
    // Creates a variable for the loop
    int q;
    // Creates a "infinte loop"
    for(q=0;q<100000;++q){
        printf("Select Action\r\n");
        int acted;
        int am;
        int ACC;
        scanf("%d", &acted);
        printf("Enter account number\r\n");
        scanf("%d", &acc);
        // Checks what option you chose.
        if (acted == 1) {
           printf("Amount of cash to remove\n");
           scanf("%d", &am);
           account[acc]=(account[acc]-am);
        } else if(acted == 2) {
           printf("Amount of cash to add\n");
           scanf("%d", &am);
           account[acc]=(account[acc]+am);
        } else if(acted == 3) {
           printf("Account Number: %d\n",ACC);
           printf("Balance of account: %d\n",account[ACC]);
        }

    }
}
2

Notice that the variables you have defined inside the loop are integers:

int acted;
int am;
int acc;

So, when using the scanf() function, you need to indicate that you are receiving an integer. Right now, using "%s" you are indicating a string. To receive an integer, you must use "%d" or "%i". Also, you were missing an & before the second argument. For example:

scanf("%d", &acted);

The value you where getting before is a random value stored in memory, because your int variables were not initialized, meaning your program had not assigned any value to the variable and therefore when you print the value, it shows that random number "-1073741819".

Enrique
  • 61
  • 1
  • 6
1

The working version of this code is one that was supplied by Tharindu Lakshan with some edits to fix some errors that made it not work. The main thing i edited in Tharindu's code was defining the array's element and setting them to 0. The error was not really an error and was just a random number the computer outputed since the elements in the arrays had no value.

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

int main()
{
    int account[10] = { 0 };
    // Tell you the options
    printf("Check balance : 3\r\n");
    printf("Add cash : 2\r\n");
    printf("Remove cash : 1\r\n");
    // Creates a variable for the loop
    int q;
    // Creates a "infinte loop"
    for(q=0;q<100000;++q){
        printf("Select Action\r\n");
        int acted;
        int am;
        int acc;
        scanf("%d", &acted);
        printf("Enter account number\r\n");
        scanf("%d", &acc);
        // Checks what option you chose.
        if (acted == 1) {
           printf("Amount of cash to remove\n");
           scanf("%d", &am);
           account[acc]=(account[acc]-am);
        } else if(acted == 2) {
           printf("Amount of cash to add\n");
           scanf("%d", &am);
           account[acc]=(account[acc]+am);
        } else if(acted == 3) {
           printf("Account Number: %d\n",acc);
           printf("Balance of account: %d\n",account[acc]);
        }
    }
    return 0;
}