mardi 26 mai 2015

how to compare user input to a string for a while loop in c programming

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;
}

Aucun commentaire:

Enregistrer un commentaire