Thread: confused

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    2

    confused

    I need to add quit or exit menu option to my program. and I also need to add which will be displayed when the program exits a running total of all the converted dollar amounts entered. The highest converted dollar amount out of all the currencies entered, along with the name of that currency. everthing I have tried crashes any hlep would be great.


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
     
    
    float conversion(float, float);
    float get_dollar(void);
    void  get_menu(void);
    main(void)
    {
     
    
     
    
    /*define currency*/
    float UK              = .558005;
    float Euro            = 0.8264;
    float Canadian        = 1.32;
    float YEN             = 109.37;
    float AUD             = 1.30073;
    char  input           =  ' ';
    float amount       =  0.0;
    float total_amt    =  0.0;
    
     
    
    //display the menu options
    
    get_menu();
     
    
    /*Get input for menu option. The method used is the getch() function which
    gets the first character input only.  This is good for my menu since I am only trying
    to get the numbers 1 2, or 3.  I cant enter more then one character, and if
    I enter an invalid character the case statement below will catch this.
    */;
    
    input = getch();
    Printf("%c\n",input);
    switch(input)
    
     {
      case '1':
         /*Do conversion for british currency */
         /*Get input for dollar amount */
         amount = get_dollar();
         total_amt = conversion(amount, UK);
         /* Print out conversion amount */
         printf("\n\n %4.2f UK Pound    = %4.2f US dollars\n",amount, total_amt);
         break;
      case '2':
         /*Do conversion for Danish currency */
         amount = get_dollar();
         total_amt = conversion(amount,Euro);
          /* Print out conversion amount */
         printf("\n\n %4.2f Euro Dollar     = %4.2f US dollars\n",amount, total_amt);
         break;
      case '3':
         /*Do conversion for Candian currency */
         amount = get_dollar();
         total_amt = conversion(amount,Canadian);
         /* Print out conversion amount */  
         printf("\n\n %4.2f Canadian Dollar  = %4.2f US dollars\n",amount, total_amt);
         break;
      case '4':
         /*Do conversion for Candian currency */
         /* Print out conversion amount */  
         printf("\n\n %4.2f Japan Yen  = %4.2f US dollars\n",amount, total_amt);
         break;
      case '5':
         /*Do conversion for Candian currency */
         amount = get_dollar();
         total_amt = conversion(amount,AUD);
         /* Print out conversion amount */  
         printf("\n\n %4.2f Australian Dollar  = %4.2f US dollars\n",amount, total_amt);
         break;
      default:
        /* catch the invalid menu option and display to screen */
         printf("\n\n You have entered an invalid menu option\n");
         break;
     }
     
     
    printf("Hit any key to quit.....\n");
    getche();
    }
    
     
    
    /* Present menu options*/
    
    void get_menu()
    
    {
        printf("1.  UK Pounds\n");
        printf("2.  Euro dollar\n");
        printf("3.  Canadian Dollar\n");
        printf("4.  Japan Yen\n");
        printf("5.  Australian Dollar\n");
        printf("\n\nPlease enter the number associated with the currency:");
    }
    
     
    
    /* The function takes an input amount and the conversion rate and
       then divides the input amount by the conversion rate to get the 
       new converted amount.
    */
    float conversion(float input_amt, float curr_value)
    {
      float new_amount = 0.0;
      new_amount = input_amt / curr_value;
      return new_amount;
    }
    
     
    
    /* The function prompts for an input dollar amount.  */
    float get_dollar()
    {
       double inp_dol = 0.0;
       char inp_string[20]="";
       char *pEnd;
       
       printf("\nPlease enter a non zero dollar amount:");
       scanf("%s",inp_string);
       
       //The following code allows for validation of input. If the user enters any non numeric number or
       //a number of less than or equal to zero the code below will prompt the user again. 
       inp_dol = strtod(inp_string, &pEnd);
       while(pEnd[0] != '\0' || inp_dol <= 0.0)
       {
         printf("\nPlease enter a non zero dollar amount:");
         scanf("%s",inp_string);
         inp_dol = strtod(inp_string, &pEnd);
       }
       return inp_dol;
    }

  2. #2
    Banned
    Join Date
    Jun 2005
    Posts
    594
    int main()


    is the way to go first off,

    2nd if you want a menu to quick ask if they want to quit
    if they respond wanting to quit (check ther einput) return 0;
    will exit the progam at that time.

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >int main() is the way to go first off,
    Not quite. Try:
    Code:
    int main(void)

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Not quite. Try:
    There's nothing wrong with int main() as opposed to int main ( void ), the two are identical in this case.
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >There's nothing wrong with int main() as opposed to int main ( void ), the two are identical in this case.
    Doesn't the C Standard say?
    Code:
    int main(void)

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Doesn't the C Standard say?
    It also says "or equivalent", and in a function definition, an empty parameter list is identical to void. I agree that int main ( void ) is more consistent with the declaration scheme, but there's nothing wrong with int main() if you like it better.
    My best code is written with the delete key.

  7. #7
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Quote Originally Posted by Prelude
    in a function definition, an empty parameter list is identical to void.
    Not quite. An empty parameter list means the function accepts any number of parameters and a void parameter list means no parameters.

    You can see from this sample program:
    Code:
    itsme@itsme:~/C$ cat voidparam.c
    void func1()
    {
    }
    
    void func2(void)
    {
    }
    
    int main(void)
    {
      func1(2);
      func2(2);
    
      return 0;
    }
    Code:
    itsme@itsme:~/C$ gcc -Wall voidparam.c -o voidparam
    voidparam.c: In function `main':
    voidparam.c:12: error: too many arguments to function `func2'
    itsme@itsme:~/C$
    If you understand what you're doing, you're not learning anything.

  8. #8
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >an empty parameter list is identical to void.
    You taught me that an empty parameter list in C (as opposed to C++) means an unknown number of arguments. So right now I'm confused as to how int main() can mean the same thing as int main(void). Can you explain this better?

  9. #9
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Ok, I think I understand. Since this is a definition, not a prototype, they are identical. Obviously a function definition has to list any parameters. Otherwise you wouldn't be able to use them in your function.

  10. #10
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Not quite. An empty parameter list means the function accepts
    >any number of parameters and a void parameter list means no parameters.
    Yes, quite. In a function declaration, that is, a prototype, which is a function tag without a body, in other words:
    Code:
    int main(); /* This is a function declaration */
    An empty parameter list does indeed mean "unknown number and type of parameters". If you use a void parameter:
    Code:
    int main ( void ); /* Still a function declaration */
    Then it forces the function to understand that it takes no parameters at all, of any type. On the other hand, in a function defintion, which is also a declaration, but gives the function a body:
    Code:
    /* This is a function defintion */
    int main()
    {
      return 0;
    }
    An empty parameter list means that the function takes no arguments, regardless of the type. Just like the declaration with a parameter of void. A defintion with a parameter of void means the same thing, no arguments:
    Code:
    /* Still a function defintion */
    int main ( void )
    {
      return 0;
    }
    >You can see from this sample program:
    The only thing I see from that sample program is that you invoke undefined behavior, so the compiler can do whatever the hell it wants, including printing a diagnostic for passing too many arguments to func1, or letting it slide.

    >Can you explain this better?
    Yes. To be correct in all cases, use int main ( void ). If you know what's going on and can argue your case against people who think you don't, feel free to use int main() and be prepared to defend yourself.
    My best code is written with the delete key.

  11. #11
    Registered User
    Join Date
    Aug 2005
    Posts
    2
    thanks for the info

  12. #12
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Well then you misspoke in your original post since you mentioned a function definition but I've never seen anyone actually give a prototype for main() and the OP definitely didn't have one. So in essence, you were really talking about a declaration (or should have been.) And that's what I was writing my post in response to. Case of miscommunication I guess.

    EDIT: I know you mention that the declaration is also a definition, but if that's the case then they're still not the same since both my func1() and func2() are definitions and since they're called exactly the same way and the only difference between them is the parameter list, then the fact that one of them results in undefined behavior just further proves that the a void parameter list and an empty parameter list are not identical.
    Last edited by itsme86; 08-31-2005 at 03:47 PM.
    If you understand what you're doing, you're not learning anything.

  13. #13
    Banned
    Join Date
    Jun 2005
    Posts
    594
    the ( void ) and () wasnt my main concern when i mentioned
    that, id say the most important would be int as of right now.

    plus i do c++ mostly so i never think about ( void )
    in c.

    but reguardless when it comes to main, i dont think
    that i really matters whether you put anything in ()
    because any command line parameter you send it,
    wont make a difference right?

  14. #14
    Nonconformist Narf's Avatar
    Join Date
    Aug 2005
    Posts
    174
    So in essence, you were really talking about a declaration (or should have been.
    I can't assume to know what Prelude was really talking about, but as I understand it, he's right. I haven't seen anyone try to prototype main() in ages, so it's a stretch to say he was talking about a declaration when he was probably rightfully assuming it was a definition.
    EDIT: I know you mention that the declaration is also a definition, but if that's the case then they're still not the same since both my func1() and func2() are definitions and since they're called exactly the same way and the only difference between them is the parameter list, then the fact that one of them results in undefined behavior just further proves that the a void parameter list and an empty parameter list are not identical.
    Nah, it just proves that your implementation treats the undefined behavior differently. Both function calls are undefined because the standard says that "If a function that accepts a variable number of arguments is defined without a parameter type list that ends with the ellipsis notation, the behavior is undefined". In a function definition, f(void) and f() are the same. In a function declaration, they're not the same, and f() is deprecated.

    Sorry to interrupt like this, but people bust my balls about using int main() all the time, so I had to get really good at proving them wrong. Ya, this whole debate is probably a misunderstanding, but I've researched the topic thoroughly in the past, and I can confidently say that with the given posts Prelude is more right than you are. No offense intended, but it's hard to tactfully tell someone they're wrong.
    Just because I don't care doesn't mean I don't understand.

  15. #15
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Your quote is meaningless because we're not talking about a variable arguments function. That's in reference to functions such as printf() where the definition has ellipses (e.g. int printf(char *format, ...);) In that case you must have a parameter before the ellipses is what that quote is saying. That's not what this is about though.

    And where you have main() with the function's body that's a declaration. You're saying that void and empty are not the same in that case so you're really saying I'm more correct, not Prelude, because that's what I was saying. This whole thing is getting pretty confusing. Heh.
    If you understand what you're doing, you're not learning anything.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. New to C++ and confused by boolean and ifs and else
    By jconner in forum C++ Programming
    Replies: 10
    Last Post: 08-02-2006, 03:29 AM
  2. Confused
    By jeev2005 in forum C Programming
    Replies: 5
    Last Post: 06-01-2006, 02:04 PM
  3. Confused
    By (TNT) in forum C# Programming
    Replies: 1
    Last Post: 11-23-2005, 04:49 PM
  4. confused.. in selecting my line of deapth
    By jawwadalam in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 05-04-2003, 01:21 PM
  5. Extern Question, really confused
    By SourceCode in forum C Programming
    Replies: 10
    Last Post: 03-26-2003, 11:11 PM