Thread: Float/double compiler error and TONS of questions!

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    11

    Talking Float/double compiler error and TONS of questions!

    Looks like I'll be asking tons of questions in this forum from now Hope I don't make anyone irritated with constant questions.

    I'm making my second program for the beginner's C class.. Just that I haven't been able to compile it.

    Code:
    #include <stdio.h>
    
    #define DISCOUNT 0.00525f;
    
    FILE *fileout;
    
    void main (void)
    {
    
    int
      item_one,
      item_two,
      item_three,
      subtotal,
      disc_amt,
      grantotal;
    
    fileout = fopen ("A:REPORT2.RPT","w");
    
    printf ("\n\nPlease enter the amount of the first item: $");
    scanf ("%d", &item_one);
    printf ("\n\nPlease enter the amount of the second item: $");
    scanf ("%d", &item_two);
    printf ("\n\nPlease enter the amount of the third item: $:");
    scanf ("%d", &item_three);
    
    printf ("\n\nYou have entered these three prices:");
    printf ("\n\n	First Item: $%d", item_one);
    printf ("\n\n	First Item: $%d", item_two);
    printf ("\n\n	First Item: $%d", item_three);
    printf ("\n\n	Calculating costs...");
    printf ("\n\n	Receipt created at A:REPORT2.RPT");
    
    subtotal = item_one + item_two + item_three;
    disc_amt = subtotal * DISCOUNT;
    grantotal = subtotal - disc_amt;
    
    fprintf (fileout, "\n\nCost of first item:		$%d", item_one);
    fprintf (fileout, "\n\nCost of second item:		$%d", item_two);
    fprintf (fileout, "\n\nCost of third item:		$%d", item_three);
    fprintf (fileout, "\n				-----------");
    fprintf (fileout, "\nTotal Cost				$%d", subtotal);
    fprintf (fileout, "\nDiscount			       -$%d", disc_amt);
    fprintf (fileout, "\n				-----------");
    fprintf (fileout, "\nGrand Total			$%d", grantotal);
    
    }
    Well that's what I have so far.
    I get the compiling error at the line "disc_amt = subtotal * DISCOUNT;"
    Something like "cannot convert from float/double".
    No idea what it means!

    Please keep in mind that I'm writing this off a template... the template has just a few lines of coding. To be specific,
    #include stuff, FILE * fileout;, void main (void), and fileout = fopen ("A:REPORT2.RPT","w"); are the only things in the template.

    From that I have to make everything else in scratch.

    Now on to my hideous questions:

    1. I've yet to learn what alot of these things are... can anyone tell me what exactly things like
    "FILE *fileout;"
    "void main (void)"
    "fileout = fopen ("A:REPORT2.RPT","w");"
    "int" does?

    I could pretty much tell what fileout= fopen ... is but i'm not sure what the "w" is for.
    As for "int", I'm just assuming I need this to include whatever variables I'm going to put in scanf and formulas.

    2. About fprintf, I assume it pretty much works like printf but why would I need a ([B]fileout[\B], "bleh");?

    Thanks for reading all this and for having patience with this complete newbie business I have here I appreciate it.

  2. #2
    Unregistered
    Guest
    "FILE *fileout" is declaring a pointer to a file, meaning that it is a variable that contains the "memory address" of the output file.

    void main(void): the first void means that the "main" function of the program does not return a value to the operating system... the second void means that the "main" function of the program does not accept any arguments (input) from the command line (or anywhere else) when it is called.

    "w" means you are opening the file to WRITE to it, as opposed to read, Etc.

    "int" means you are defining all of those variables to be integers. This can have some strange mathematical consequences since data past a decimal can get truncated.

    fprintf prints out to a file rather than stdout (your display)... so, you need to tell it where the file is (using the file pointer, above).

    I recommend checking out the tutorials on this site and elsewhere... a lot of these questions may be answered better there.

  3. #3
    Registered User Engineer's Avatar
    Join Date
    Oct 2001
    Posts
    125
    As far as your compiler error goes try changing this:

    #define DISCOUNT 0.00525f;

    to this:

    #define DISCOUNT 0.00525

    "disc_amt" should be declared as a float or double.

    You also have to add this at the end of your program:

    fclose(fileout);

    This will close the file that you have opened.

    Hope this helped, Cheers.
    1 rule of the Samurai Code: if you have nothing to say, don't say anything at all!

  4. #4
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    you should also check that the fopen() succeeded by testing the file pointer for NULL.If NULL the file was not successfully opened.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    18
    You are more or less correct on what "int" does. If you are going to use a variable(s) in your program, you need to tell the program what you are going to call them so it can look for them as it executes. There are several different types of variables that you can use and "int" is only one of them.


    Unless you are expecting input to be entered in whole dollar amounts, you might want to change your variables to floats instead of int's.

    int = integer, which is a whole number value (2, 45, -256, etc)

    whereas

    float = floating point, which is a decimal number (1.5, 45.67, $29.99, etc)

    There are also: double, char, and char string[]. I will leave it to you to research these though, as I find it's easier to learn if you work with the information rather then just look at what "we" spit out

    Hope this helps,

    Guideon72

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    11
    I appreciate this guys!

    All I ask is for a small nudge in the right direction... I firmly believe that the best way is to learn from my mistakes and find out new things by researching... with a little bit of guidance.

    Thanks for the inputs, I've a better idea of what to do now.

Popular pages Recent additions subscribe to a feed