I am trying to get this to work and need some help please
I have this if statement that checks for conversion of a currency
Code:if (ok == 1) { amount_to_convert = atof(line); //convert the amount to a float rate_EURO_USD = euro * amount_to_convert; //calculate the converted amount printf("%.2f Euro is %.2f U.S. Dollars\n", amount_to_convert, rate_EURO_USD); } //print error message else { printf("Incorrect Value Given.\n"); }
and i need to place this within a function if okay = 0
Here is the whole program, I need to add a function
Code:#include <stdio.h> #include <stdlib.h> #include <system.h> /* Start of currency program here */ int main(void) { /* setup data type defintions and initialize variables */ char line[256]; //retains the line of user input int i; //variabile used in the for loop below int ok = 0; //is 1 if the input value is correct and 0 otherwise float amount_to_convert; //retains the amount to be converted float rate_EURO_USD; //retains the conversion amount /* render values of the Euro to the screen */ printf("\n\t\t%.2f Euro is %1.6f U.S. Dollars", us_dollar, euro); //loop until a correct value is entered while (ok == 0) { //print the info for the user to know what he has to do (enter the amount of euros) printf("\n\nEnter the amount of Euro you want to convert to U.S.Dollars\n"); fflush(stdin); //flush the input buffer gets(line); //reads a line from the user ok = 1; //value is assumed correct //for each character of the input line (strlen returns the length of the line) for (i = 0; i < strlen(line); i++) { //if it is the first or last character(digit) if (i == 0 || i == strlen(line) - 1) { //if the value is not between 0 and 9 error if (line[i] < '0' || line[i] > '9') { ok = 0; //value is incorrect break; //exit the for loop } } //the other characters else { //if the value is not between 0 and 9 and not the decimal point if ((line[i] < '0' || line[i] > '9') && line[i] != '.') { ok = 0; //value is incorrect break; //exit the for loop } } } //if the value is correct if (ok == 1) { amount_to_convert = atof(line); //convert the amount to a float rate_EURO_USD = euro * amount_to_convert; //calculate the converted amount printf("%.2f Euro is %.2f U.S. Dollars\n", amount_to_convert, rate_EURO_USD); } //print error message else { printf("Incorrect Value Given.\n"); } } /* do not allow DOS window to close until key is pressed by user */ fflush(stdin); printf("\n\nPress any key to terminate this program..."); getch(); return 0; }
can someone help me out please, I am a newbie
Thanks,
BOB Perkins