Crash C program when running

Asked

Viewed 55 times

1

Once I enter the variable data originalMoney the prompt crash.

Follow the code (it’s incomplete, I only ran the test with the first case)

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

int main()
{
    float coinsValue, notesValue, totalMoney;
    int switchValue;
    char originalMoney, convertedMoney;

    printf("Enter original money type\n");
    scanf("%c",&originalMoney);

    printf("Enter the type of money you want to convert\n");
    scanf("%c",&convertedMoney);

    if (strcmp(originalMoney, "REAL") == 0 && strcmp(convertedMoney, "DOLLAR") == 0){
        switchValue = 1;
    } else if (strcmp(originalMoney, "REAL") == 0 && strcmp(convertedMoney, "LIBRAS")){
        switchValue = 2;
    } else if (strcmp(originalMoney, "DOLLAR") == 0 && strcmp(convertedMoney, "REAL")){
        switchValue = 3;
    } else if (strcmp(originalMoney, "DOLLAR") == 0 && strcmp(convertedMoney, "LIBRAS")){
        switchValue = 4;
    } else if (strcmp(originalMoney, "LIBRAS") == 0 && strcmp(convertedMoney, "REAL")){
        switchValue = 5;
    } else if (strcmp(originalMoney, "LIBRAS") == 0 && strcmp(convertedMoney, "DOLLAR")){
        switchValue = 6;
    } else {
        printf("You have chosen an invalid conversion option!");
    }

    switch (switchValue) {
    case 1:
        printf("Entering case 1...\n");

        printf("Enter coins value in R$: \n");
        scanf("%f",coinsValue);

        printf("Enter notes value in R$: \n");
        scanf("%f",notesValue);

        totalMoney = coinsValue + notesValue;
        totalMoney = totalMoney * 4.01;

        printf("The value in reais converted to dollar is: %.2f",totalMoney);
        break;
    case 2:
        printf("Entering case 2...\n");
        break;
    case 3:
        printf("Entering case 3...\n");
        break;
    case 4:
        printf("Entering case 4...\n");
        break;
    case 5:
        printf("Entering case 5...\n");
        break;
    case 6:
        printf("Entering case 6...\n");
        break;
    }
    return 0;
}
  • Problem is that you are trying to put a string in a single character, it will never work in C

  • I get it, thank you!

  • Did the answer solve your question? Do you think you can accept it? See [tour] if you don’t know how you do it. This would help a lot to indicate that the solution was useful to you. You can also vote on any question or answer you find useful on the entire site

1 answer

1


Are you declaring originalMoney and convertedMoney as char, so it has a 1 byte space reserved in each. Then you try for several bytes in it, this corrupts the memory and gives error. What you probably wanted to do was declare a string, or as in C, a array of char. And then have them read how string, something like that:

char originalMoney[10];
scanf("%s9", &originalMoney);

I put in the Github for future reference.

  • Thanks for the help!

  • @Pedroguilherme see in the [tour] the best way to say thank you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.