Hello all,

I had posted earlier for help, but the post was so deep in an older thread do not think anyone will find it.

so here goes,

I am in a class to learn C and was tasked with writing a program that will convert currencies. Yes, the same old program is still being asked of students. Now I have written the program several times to complete previous requirements with no problems to date. But this last task has me stumped; I would like to get some guidance as to possible solutions to my code as I think it is correct but with a minor error that I seem to be missing.

Any help would be great, again not asking to have someone rewrite but perhaps guide me to the area of the area and possible ways to resolve the issue.

Now to the heart of the problem the program will allow a user to select the type of currency than it should allow the user to input how much to convert, I have added error checking here so as only valid inputs are excepted, if not than an error message will be displayed. Ok, right will on the second input from the user the program will assume any input to be an error, this is where I am stumped as I the code stipulates only if an input outside the range to than display an error.

With my little knowledge this is where I get confused.

Thanks for any help here.

/*The conversion rates of five currencies equivalent to one US dollar*/ 
#include <stdio.h>
#include <stdlib.h>

int main(void)
{
    int choice;
    int currency;
    float USD, UK, HK, EUR, NZ, AUS;
    char buffer[30];
    
     /********************************************
    ******Current Currency Exchange Rates*******
    *
    * British Pound(UK) 0.5408  6/15 8:50pm 
    * Hong Kong Dollar (HK) 7.7617  6/15 8:50pm 
    * Euro (EUR) 0.7914  6/15 8:50pm 
    * New Zealand (NZ) Dollar 1.6079  6/15 8:50pm 
    * Australian Dollar (AUS) 1.3556 6/15 8:50pm
    * United States Dollar (US)
    *
     **********************************************/
     
   
    USD = 1.00;
    UK = 0.5408;
    HK = 7.7617;
    EUR = 0.7914;
    NZ = 1.6079;
    AUS = 1.3556;
    
    do  // main loop runs until choice 0 is entered
    {
        /*Version and title of program and menu selection*/
        
        printf("\n\n        Welcome to Currency Conversion Utility v. 4.2.0 \n\n\n");
    
        printf("Please select the currency type you would like to convert into US dollars.\n\n");
    
        printf("1. for British Pounds \n");
        printf("2. for Hong Kong Dollars \n");
        printf("3. for Euros \n");
        printf("4. for New Zealand Dollars \n");
        printf("5. for Australian Dollars \n\n");
    
        printf("Press 0 and then return to exit the Currency Converter \n");
 
        choice = 6;  /* must be initialized to an invalid value*/ 
                     /* it's not initialized the first time, it might be in the range of 1..5*/
        do     /* 1st input loop for the choice*/
        {
            printf("\n\nEnter the number of your choice: Than hit Enter \n\n\n");
            /*reads the characters from the file associated with fp into the string pointed to.*/
 fgets(buffer, sizeof(buffer),  stdin);
         /* allows user selcetion of type of Currency to convert and Error Checking for Numeric Values only*/
            if ( ( sscanf(buffer, "%u", &choice) == 0) || choice > 5 || choice < 0 ) 
        
             {
            printf("\n\n\a                  Error please select agian  \n");
             }
        }
        while(choice < 0 || choice > 5);
     
        if ( choice > 0 ) 
         {  /* no need to ask for the amount if exit was selected*/
            
            currency = 100000;  /* must be initialized to an invalid value*/ 
            
            do    /* 2nd input loop for the amount*/
             {
       
                printf("\n\n\nEnter the amount you want to convert to U.S. Dollars: Than hit Enter \n\n");
       
                fgets(buffer, sizeof(buffer),  stdin);
                
                /* Allows input of currency amount and Error Checking for Numeric Values only*/
                if ( ( sscanf(buffer, "%d", &currency) == 0) || currency > 99999 || currency < 1 )
                 {
                    printf("\n\n\a       This is an Erroneous amount based on your input\n"); 
                    printf("       please enter a numeric value");
                 }
             }      
             
             while(currency > 99999 || currency < 0);  /* changed the condition to int values*/
        }
        
        switch(choice) 
         {
      
            case 1:
                printf("\n\n         You will have %.2f US dollars \n\n", USD/UK*currency);
                break;       
            case 2:  
                printf("\n\n         You will have %.2f US dollars \n\n", USD/HK*currency);
                break;
            case 3:
                printf("\n\n         You will have %.2f US dollars\n\n", USD/EUR*currency); 
                break;
            case 4:
                printf("\n\n         You will have %.2f US dollars \n\n", USD/NZ*currency); 
                break;
            case 5:
                printf("\n\n         You will have %.2f US dollars \n\n", USD/AUS*currency); 
                break;
            case 0:
                break;
                return 0;
        }
    
 } 
     
    while(choice!=0 && currency != 0); /* saves the second switch.*/
                                        /* the switch would not work if I had changed currency to float*/
    puts("You have entered 0 and selected to exit the program.");
    puts("Thank you for using Currency Converter.");
    puts("bye.");
    puts("press enter.");
    getchar(); 
    
    return 0;
}

Randy
"A Noob wishing to gain more knowledge";)

if ( ( sscanf(buffer, "%d", &currency) == 0) || 
       currency > 99999 || 
       currency < 1 )
{
    /////////////////////////////////////////////////////
    printf("\n\n\a       This is an Erroneous amount based on your input\n");

First I'd try placing an output statement to indicate the value of currency and buffer at the place where the /////////////////////////// is. If the values of currency and buffer aren't what you expect, then track down why not.

if ( ( sscanf(buffer, "%d", &currency) == 0) || 
       currency > 99999 || 
       currency < 1 )
{
    /////////////////////////////////////////////////////
    printf("\n\n\a       This is an Erroneous amount based on your input\n");

First I'd try placing an output statement to indicate the value of currency and buffer at the place where the /////////////////////////// is. If the values of currency and buffer aren't what you expect, then track down why not.

I actually removed the second Do statements which now allows the program to process the conditions for currency and place inthe buffer. But still have issues in that the error msg, as indicitated continues to print and there seems to be no error checking at all as I can now enter any value with a output.

here is what the code now looks like

/*The conversion rates of five currencies equivalent to one US dollar*/ 
#include <stdio.h>
#include <stdlib.h>

int main(void)
{
    int choice;
    int currency;
    float USD, UK, HK, EUR, NZ, AUS;
    char buffer1[30];
    char buffer2[90];
     /********************************************
    ******Current Currency Exchange Rates*******
    *
    * British Pound(UK) 0.5408  6/15 8:50pm 
    * Hong Kong Dollar (HK) 7.7617  6/15 8:50pm 
    * Euro (EUR) 0.7914  6/15 8:50pm 
    * New Zealand (NZ) Dollar 1.6079  6/15 8:50pm 
    * Australian Dollar (AUS) 1.3556 6/15 8:50pm
    * United States Dollar (US)
    *
     **********************************************/
     
   
    USD = 1.00;
    UK = 0.5408;
    HK = 7.7617;
    EUR = 0.7914;
    NZ = 1.6079;
    AUS = 1.3556;
    
    do  // main loop runs until choice 0 is entered
    {
        /*Version and title of program and menu selection*/
        
        printf("\n\n        Welcome to Currency Conversion Utility v. 4.2.0 \n\n\n");
    
        printf("Please select the currency type you would like to convert into US dollars.\n\n");
    
        printf("1. for British Pounds \n");
        printf("2. for Hong Kong Dollars \n");
        printf("3. for Euros \n");
        printf("4. for New Zealand Dollars \n");
        printf("5. for Australian Dollars \n\n");
    
        printf("Press 0 and then return to exit the Currency Converter \n");
 
        choice = 6;  /* must be initialized to an invalid value*/ 
                     /* it's not initialized the first time, it might be in the range of 1..5*/
        do     /* 1st input loop for the choice*/
        {
            printf("\n\nEnter the number of your choice: Than hit Enter \n\n\n");
            /*reads the characters from the file associated with fp into the string pointed to.*/
 fgets(buffer1, sizeof(buffer1),  stdin);
         /* allows user selection of type of Currency to convert and Error Checking for Numeric Values only*/
            if ( ( sscanf(buffer1, "%u", &choice) == 0) || choice > 5 || choice < 0 ) 
        
             {
            printf("\n\n\a                  Error please select again  \n");
             }
        }
        while(choice < 0 || choice > 5);
     
        if ( choice > 0 ) 
         {  /* no need to ask for the amount if exit was selected*/
            
            currency = 100000;  /* must be initialized to an invalid value*/ 
            
//            do    /* 2nd input loop for the amount*/
             {
       
                printf("\n\n\nEnter the amount you want to convert to U.S. Dollars: Than hit Enter \n\n");
       
                fgets(buffer2, sizeof(buffer2),  stdin);
                
                /* Allows input of currency amount and Error Checking for Numeric Values only*/
                
//                if ( (sscanf(buffer, "%u", &currency) == 0) || currency > 99999 || currency < 1 )
                
                scanf(buffer2, "%u", &currency);
                
                if(currency > 99999 || currency < 0 )
  
                 {
                    printf("\n\n\a       This is an Erroneous amount based on your input\n"); 
                    printf("       please enter a numeric value");
                 }
  else if(currency < 99999 || currency > 0)
  
   {
   printf("lets hope this works");
   }
             }      
             
//             while(currency > 99999 || currency < 0);  /* changed the condition to int values*/
        }
        
        switch(choice) 
         {
      
            case 1:
                printf("\n\n         You will have %.2f US dollars \n\n", USD/UK*currency);
                break;       
            case 2:  
                printf("\n\n         You will have %.2f US dollars \n\n", USD/HK*currency);
                break;
            case 3:
                printf("\n\n         You will have %.2f US dollars\n\n", USD/EUR*currency); 
                break;
            case 4:
                printf("\n\n         You will have %.2f US dollars \n\n", USD/NZ*currency); 
                break;
            case 5:
                printf("\n\n         You will have %.2f US dollars \n\n", USD/AUS*currency); 
                break;
            case 0:
                break;
                return 0;
        }
    
 } 
     
    while(choice!=0 && currency != 0); /* saves the second switch.*/
                                        /* the switch would not work if I had changed currency to float*/
    puts("You have entered 0 and selected to exit the program.");
    puts("Thank you for using Currency Converter.");
    puts("bye.");
    puts("press enter.");
    getchar(); 
    
    return 0;
}

again, all seems somewhat correct except I can not understand why no matter what the code thinks that all values for currecny are false, but again after removing the do/while the program does continue to function, well kinda.

Randy

I compiled and ran your code and it worked as expected for me no problem. What exaclty are you inputing and what exactly is the response you're getting?

Oops you posted just before me. The code runs *with* the second do loop in tact just fine for me, I literaly just pasted your first posted code straight into a main.c and compiled it, and it ran as expected first time, if I put in dodgy input I get the error message, if the inputs ok I get the conversion.

I compiled and ran your code and it worked as expected for me no problem. What exaclty are you inputing and what exactly is the response you're getting?

Oops you posted just before me. The code runs *with* the second do loop in tact just fine for me, I literaly just pasted your first posted code straight into a main.c and compiled it, and it ran as expected first time, if I put in dodgy input I get the error message, if the inputs ok I get the conversion.

Thank you for your feedback..

Randy

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.