Thread: Need help with function

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    5

    Need help with function

    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
    Last edited by old_student; 02-16-2009 at 03:10 PM.

  2. #2
    Registered User
    Join Date
    Feb 2009
    Posts
    5
    more....

    I tired this

    Code:
    if (ok == 1)
            {
                int total_tansaction_amount
                {
                	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");
            }
    but int total_transaction_amount is not compiling!! any help would be appreciated

    thanks in advance!
    Bob

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You have probably been told by your teacher that this is what you should do:
    Code:
            fflush(stdin); //flush the input buffer
            gets(line); //reads a line from the user
    However, fflush(stdin) is undefined since stdin is a input file, and fflush, in the standard document, is only defined for output files. So it may well work on your current system, but on other systems, it may not, and aside from "not working", the C library that comes with the compiler is interely free to crash your application if you do such a thing - because that's one of the options in "undefined behaviour" - it may do lots of other things too, including, but not limited to, nothing, write garbage to the screen.

    gets is probably one of the worst functions in the C library - it is absolutely unsafe. If someone enters a string longer than your buffer, it will crash your program. Now, line is fairly long, but 256 characters only take a few seconds to type in if you lean on the 'a' key (about 17 seconds if you use the normal 1/15th second delay between repeats that DOS/Windows chooses if you don't change it). Try it and see what happens if you get it wrong...

    As to making a function, what have you done so far - or are you expecting someone to do it for you?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Registered User
    Join Date
    Feb 2009
    Posts
    5
    Quote Originally Posted by matsp View Post
    You have probably been told by your teacher that this is what you should do:
    Code:
            fflush(stdin); //flush the input buffer
            gets(line); //reads a line from the user
    However, fflush(stdin) is undefined since stdin is a input file, and fflush, in the standard document, is only defined for output files. So it may well work on your current system, but on other systems, it may not, and aside from "not working", the C library that comes with the compiler is interely free to crash your application if you do such a thing - because that's one of the options in "undefined behaviour" - it may do lots of other things too, including, but not limited to, nothing, write garbage to the screen.

    gets is probably one of the worst functions in the C library - it is absolutely unsafe. If someone enters a string longer than your buffer, it will crash your program. Now, line is fairly long, but 256 characters only take a few seconds to type in if you lean on the 'a' key (about 17 seconds if you use the normal 1/15th second delay between repeats that DOS/Windows chooses if you don't change it). Try it and see what happens if you get it wrong...

    As to making a function, what have you done so far - or are you expecting someone to do it for you?

    --
    Mats
    Thank you Mats for replying to my post so fast and thanks for the ideas.

    Sir I am not looking for anyone to do my homework for me at all, I am not a programmer and I thought that I could get some help.

    Honestly I am lost and do not understand how to add a function to that area. I am just looking for some guidance that is all. I am not trying to anger anyone sir

    Thanks,
    Bob

    ps this is what I have tried so far

    from this:

    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");
            }
    to this

    Code:
    if (ok == 1)
            {
                int total_tansaction_amount
                {
                	*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 the int total_tansaction_amount is not compiling, i do not know how to do this that is why I am asking for some guidance or help
    Last edited by old_student; 02-16-2009 at 03:07 PM.

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    You should be warned that there are some potential problems with using floating point numbers with money -- since money has a fixed number of decimal places, it is not actually a floating point (and there are no fixed point numbers in C!). This had real consequences in real world accounting programs in the past, I have heard. This is one thread from cboard which hints at the problem. There are better ones I couldn't find.

    I don't have time to look at your code beyond that, and I know you have these other pressing problems on your mind, but you must not ignore what I am trying to tell you. I know one of the moderators (tabstop) can explain it in more detail -- it has to do with the fact that all numbers are stored in binary...
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #6
    Registered User
    Join Date
    Feb 2009
    Posts
    5
    No sir, I understand what you are saying and I thank you kindly for the tips, I really do

  7. #7
    Registered User
    Join Date
    Feb 2009
    Posts
    4
    you need to put the function total transaction amount outside of int main(){} and call to it from within the main function so it would look sort of like this.

    Code:
    int total_transaction_amount(int a){
    /*where int a is the value you want to manipulate,here it would probably be the amount of euros to create, you can add more values seperated by a comma*/
    
    //in the body you would write what you need the function to do using the variable a(or whatever you declared in the 'int a' spot. so something like
    
    amount = a * conversion
    
    //or whatever
    
    return(amount);
    }
    
    //then when you want to call to it you can just say
    
    whatever_variable = total_transaction_amount(another var);

    Hope that helps.

  8. #8
    Registered User
    Join Date
    Feb 2009
    Posts
    5
    Thank you sir, that gives me a great idea on what I need to do

    I will try that and let you know

    I appreciate it


    Thanks,
    Bob

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  3. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  4. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  5. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 06:44 PM