Thread: Need help with variables

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

    Question Need help with variables

    I need some help with my variables in C(the basic one, not that new fangled plus stuff) <grin>. I have successfully defined two variables and would like to have a third that is the product of multiplying the first two:

    float rate;
    float hours;
    float total = rate * hours

    The program compiles and runs fine, except that it never gives me "total", all I get is 0.00 in the output.

    Does anyone have any suggestions?

    Thanks,
    Guideon72

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    164
    I don't know how your output code looks like!
    // Gliptic

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    18

    Question

    Ok, here's the whole thing...it's not pretty but it works(mostly)...

    #include <stdio.h>

    main()
    {
    char name[10];
    float rate;
    float hours;
    float tax;
    float total=rate*hours;

    printf("What is your first name?\n");
    scanf(" %s", name);
    printf("Enter your hourly rate\n");
    printf("%c", '$');
    scanf(" %f", &rate);
    printf("Enter your hours worked\n");
    scanf(" %f", &hours);
    printf("Enter your tax rate in decimal format\n");
    scanf(" %f", &tax);
    ----->printf("Your total gross wage for this time period is %.2f dollars\n", total);
    printf("%s,you entered values of %.2f dollars per hour, %.1f hours worked and %.3f in taxes.\n", name, rate, hours, tax);
    return 0;
    }

    The line that isn't working is the one that I've added the arrow next to. Everything else is working fine.

    Thanks!

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    > main()

    Would be better written:

    int main( void )

    Incidently, what you wrote is exactly the same thing, as it defaults to this with what you wrote, however it just looks better this way.

    > printf("Enter your hourly rate\n");
    > printf("%c", '$');

    This can be written:

    printf("Enter your hourly rate:\n$");

    Or you can split it up. There is no need to do this:

    > printf("%c", '$');

    When you can just do this:

    printf("$");

    > printf("Your total gross wage for this time period is %.2f
    > dollars\n", total);

    This line is correct. Your problem is that you are calculating
    the "total" before those variables are entered. Before you
    put this line, do this:

    > total=rate*hours;

    See, when you did this originally, it basicly put zero in there. (Or
    some randon values, since you didn't initialize your variables first.

    When you do this, you are not setting up a formulae. You are
    simply telling the compiler to take the current values in the
    variables and multiply them, and stick that sum in 'total'.

    From then on, you can do whatever you want to 'total', or to
    either of those variables, and it isn't going to apply that bit of
    math to them again. This is a one-time-deal.

    Quzah.

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    18

    Question

    Thanks for the tips! My only question is where in the code does the total=rate*hours go? That is what I was attempting to do up above, but it doesn't work there. Should it actually be located down in the main function somewhere?


    Thanks!

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You use it when you want that information calculated.
    To do so, you first have to get values for "rate" and "hours".
    Once you do so, you now have to make sure that you have
    computed the "total" before displaying it.

    Therefore, you put compute the total:

    a) after you have 'rate' and 'hours'
    b) before you need to use 'total'

    Remember: always give a variable a value before you need to use it.

    Most programming problems can be solved if you break the problem down enough into small enough steps.

    Quzah.

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    18
    I thought that because rate and hours are determined first that having the total in the declarations at the beginning would automatically assign the values that the user entered, and therefore calculate from there:

    char name[10];
    float rate;
    float hours;
    float tax;
    ----> float total=rate*hours;

    printf("Enter your hourly rate\n$");
    scanf(" %f", &rate);
    printf("Enter your hours worked\n");
    scanf(" %f", &hours);
    printf("Enter your tax rate in decimal format\n");
    scanf(" %f", &tax);
    ---->printf("Your total gross wage for this time period is %.2f dollars\n", total);

    Am I putting that in the wrong place or using the wrong syntax?

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You've got the basics right, but you're understanding it wrong.
    Let's examine the code a bit:

    > char name[10];
    > float rate;
    > float hours;
    > float tax;

    Here, you are "declaring" variables. All this does is tell the compiler to make a variable called "name" that is of type "char", and is an array of 10 total characters. (And for "rate", "hours", "tax", you're telling the compiler to make three variables with those names, that are of type "float".

    What this does is set aside a block of memory for those variables.

    You can also declare variables, and assign them a value in one line:

    > float total=rate*hours;

    This is what you are doing here. However, what value does "total" get? Well, it gets rate*hours.

    This means:

    "Whatever the value of 'rate' is, multiply that by whatever the value of 'hours' is, _AT THIS POINT IN TIME_, and put this value in the variable called "total".

    Later on, if I did:

    total = 1;

    I've just set the value of total to 1. Any value that was there before is gone.

    Now, one more thing to look at:

    float myVariable;

    Here, I've declared a variable. What value does it have?

    It could be anything at this point in time. Its value will be whatever the value of that piece of memory was before I called it. This could be anything. I haven't given any value to "myVariable", so I am unsure what is in it.

    This is why you always want to assign values to your variables before you use them.

    You should do something like this:
    Code:
       char name[10]; 
    
       /*
       Notice, you can declare multiple variables
       of the same type, on the same line, like I
       did, by seperating them by a comma.
       */
       float rate, hours, tax, total;
    
       printf("Enter your hourly rate\n$"); 
       scanf(" %f", &rate); 
       printf("Enter your hours worked\n"); 
       scanf(" %f", &hours); 
       printf("Enter your tax rate in decimal format\n"); 
       scanf(" %f", &tax); 
    
       /*
       Now, calculate the total before we display it.
       */
       total = rate * time;
    
       /*
       Now we can display it.
       */
       printf("Your total gross wage for this time period is %.2f dollars\n", total);
    Quzah.

  9. #9
    Registered User
    Join Date
    Oct 2001
    Posts
    18

    Smile

    OHHHHHHHHHHHHHHH!!! Ok, now I get it. It was the part about the actual variable as listed in the beginning being, in effect, dynamic.

    So, if I wanted to have another function call rate and hours, I could do that as well, but I would have to add/subtract/divde them again after they were defined for that instance. Correct?

  10. #10
    Registered User
    Join Date
    Oct 2001
    Posts
    18

    Talking

    By the way, I just wanted to say thanks again for your help Quzah!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. basic question about global variables
    By radeberger in forum C++ Programming
    Replies: 0
    Last Post: 04-06-2009, 12:54 AM
  2. Best way to avoid using global variables
    By Canadian0469 in forum C++ Programming
    Replies: 7
    Last Post: 12-18-2008, 12:02 PM
  3. Replies: 15
    Last Post: 09-30-2008, 02:12 AM
  4. esbo's data sharing example
    By esbo in forum C Programming
    Replies: 49
    Last Post: 01-08-2008, 11:07 PM
  5. Global Variables
    By Taka in forum C Programming
    Replies: 34
    Last Post: 11-02-2007, 03:25 AM