0

We are trying to execute the if statements based on what the user inputs. For example if the user input Yen then we want the if statement to be executed and the conversion to yen be calculated. After the user has input how many gallons of gas the calculations are made and we are left with a total cost. We want to then let the user choose a currency that they would like to convert the total cost into. We have given the user three choices. If the user inputs something that is not any of the given choices then the user is allowed to re-enter a choice again. One the user does enter one of the 3 choices then we want the currency conversion to be executed.

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

float gas_gallons;
float cost_today_gallons;
int main()
{
    char input;
    printf("Please enter the number of gallons of gasoline: ");
    scanf("%c", &input);

    while (!isdigit(input))
    {
        printf("\nYou need to enter a digit. Please enter the number of gallons of gasoline: ");
        scanf("%c", &input);
        }
    if (isdigit(input))
    {
        printf("\nThe users input was %c", input);
        gas_gallons = input - '0';
        printf("\nAs a float it is now %f", gas_gallons);

        float carbon_dioxide_pounds = gas_gallons * 19.64;
        printf("\n%.2f gallons of gasoline produces approximately %f pounds of carbon dioxide.", gas_gallons, carbon_dioxide_pounds );

        float barrels_crude_oil = gas_gallons/19.0;
        printf("\n%.2f gallons of gasoline requires %f barrels of crude oil.", gas_gallons, barrels_crude_oil);

        cost_today_gallons = gas_gallons*2.738;
        printf("\n%.2f gallons of gasoline costs a total average of %f US dollars today.", gas_gallons, cost_today_gallons);
        }

    char currency[100];
    printf("\nChoose a currency you want to see your total cost in (Euro, Pound, or Yen): ");
    scanf("%c", &currency);
    printf("\nThe currency chosen is %c", currency);

    char Yen[100];
    char Euro[100];
    char Pound[100];

    while (strcmp(currency, Yen) != 0 || strcmp(currency, Euro) != 0 || strcmp(currency, Pound) != 0)
    {
        printf("\nYou need choose one of these currencies (Euro, Pound or Yen). Please enter one: ");
        scanf("%s", &currency);
    }
    if (strcmp(currency, Yen) == 1)
    {
        float yen_total_cost = cost_today_gallons*123.07;
        printf("\n%.2f gallons of gasoline costs a total average of %f US dollars today.", gas_gallons, yen_total_cost);
    }
    if (strcmp(currency, Euro) == 1)
    {
        float euro_total_cost = cost_today_gallons*0.92;
        printf("\n%.2f gallons of gasoline costs a total average of %f US dollars today.", gas_gallons, euro_total_cost);
    }
    if (strcmp(currency, Pound) == 1)
    {
        float pound_total_cost = cost_today_gallons*0.65;
        printf("\n%.2f gallons of gasoline costs a total average of %f US dollars today.", gas_gallons, pound_total_cost);
        }
    return 0;
}
5
  • Simplify your life if possible and provide a numerical menu which will allow them to enter an integer that you can switch on. Commented May 27, 2015 at 2:11
  • Enable compiler warnings and you will find a few issues, also it's not completely clear what you want to do, and asking it multiple times is not going to help you, it's probably going to be closed again. Commented May 27, 2015 at 2:12
  • my problem is that when the user inputs an answer to the question "Choose a currency you want to see your total cost in (Euro, Pound, or Yen):", I want it to take that input and execute the corresponding code. If the input is Euro, then i want the if statement for euro to be executed. Commented May 27, 2015 at 2:19
  • 1
    But you are not explaining why isn't that happening, and the warnings thing, it's really important, your code has errors. Commented May 27, 2015 at 2:21
  • You've described what you want to happen. But you have not described what is actually happening that is incorrect and you have not asked a specific (or even any) question. Commented May 27, 2015 at 2:23

1 Answer 1

2

Some problems I can see in your code:

  1. You should use scanf("%s", str) to read a string, see scanf()
  2. Also, strcmp() returns 0 when its parameters are equal.

  3. And char[] Yen, Euro, Pound in your code are not initialized.

Sign up to request clarification or add additional context in comments.

2 Comments

When I get prompted to choose a currency, I typed in a currency and it just keeps repeating that I need to choose a currency so it's not executing any of the if statements even though I typed in the currency that was one of the given choices.
@NicolRamirez Do you initialize char[] Yen, Euro, Pound correctly? And check the last 3 strcmp(), strcmp() returns 0 when its parameters are equal. If it still does not work, you can try print currency to check if the input is correct.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.