Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a C program (NOT C++) that performs these steps: a. Read in the exchange.d

ID: 3557996 • Letter: W

Question

Write a C program (NOT C++) that performs these steps:

a. Read in the exchange.dat data file into a suitable internal data structure. Exchange.dat looks as follows: (note: there are more than one space between currency names and currency values)

dollar 1.00
yen 0.0078
franc 0.20
mark 0.68
pound 1.96

b. Ask the user to enter two currency names: that of the old currency being converted and of the new currency being returned.

c. Ask for a value in the original currency. d. Display the resulting value in the second currency. The easiest way to compute this value is to convert the original currency to dollars and then convert the dollars to the target currency.

Explanation / Answer

I believe we have the same class, heres my code. I compiled it and it works 100% !!

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

//Define the following:
#define FILE_NAME "exchange.dat"
#define NAME_LENGTH 10
#define MAX_TYPES 4

struct currency
{
    char name[NAME_LENGTH];
    float dollars;
}types[MAX_TYPES];

int main(void)
{
    char original[NAME_LENGTH];
    char converted[NAME_LENGTH];
    float value;
    float valueInDollars;
    float convertedValue;
    int count = 0, i;

//FILE opens by using a pointer *file , then you open a filename defined from before using fopen(filename, "r" for reading);
   FILE *file; //define the file and opens the file
    file = fopen(FILE_NAME, "r");

   if(file == NULL) //If no file exists
    {
        printf("The file %s is not found!", FILE_NAME);//printf this statement
        exit(EXIT_FAILURE); //Exit
    }

//Read the two files, and check if there are any numbers in the string.
   while(fscanf(file, "%s %f", types[count].name, &types[count].dollars) != -1)
    {
        count++;     
    }


//printf results
    printf("This program converts amounts between several currencies. ");
   printf("Convert from: "); //prompts user to input a currency
    scanf("%s", original); //Save the inputted into variable original

   printf("Convert into: "); //prompts user to input what to convert the original currency into
    scanf("%s", converted); //save into converted

   printf("How many units of type dollar? "); //prompts user to enter the amount to be converted
    scanf("%f", &value); //save into value

   int originalIndex = 0; //define the index of the original
    while(originalIndex < count)
    {
    //if the there are no strings equal to the original input, break. Else continue
        if(strcmp(types[originalIndex].name, original) == 0)
            break;
        else
            originalIndex++;
    }

   if(originalIndex == count) //If the first inputted string has no strings in the file, printf the following, and exit.
    {
        printf("%s is not a valid currency name!", original);
        exit(EXIT_FAILURE);
    }


//Do the same for the converted string
   int convertedIndex = 0;
    while(convertedIndex < count)
    {
        if(strcmp(types[convertedIndex].name, converted) == 0)
            break;
        else
            convertedIndex++;
    }

   if(convertedIndex == count) //If no strings in the second input exist in the file, printf and exit
    {
        printf("%s is not a valid currency name!", converted);
        exit(EXIT_FAILURE);
    }

//Conversions:
   valueInDollars = types[originalIndex].dollars * value;
    convertedValue = valueInDollars / types[convertedIndex].dollars;

//Printf the results
    printf("%.0f %s = %.3f %s ", value, original, convertedValue, converted);
  
    fclose(file); //close the file
    return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote