Multiply Variable by Another Variable?

15,920

Solution 1

You have multiply the euro with user given quantity qty before entered by the user. It should be as below: //euro_result = euro * qty; // <-- shift this to the position given below

//start program for user
printf("Enter a quantity: ");

//alow user to input a quantity
scanf("%d", &qty);

euro_result = euro * qty; // Euro Dollars multiplied by user input qty

printf("Euro:       %f \n", euro_result);

Thats all.

Solution 2

The bug is that the line

euro_result = euro * qty;

needs to be after qty is read-in

Solution 3

The statements in a C program are executed sequentially, and expressions are not evaluated symbolically. So you need to reorder your statements this way:

int qty;
float euro, euro_result;

euro = .6896; // store constant value in 'euro'

printf("Enter a quantity: ");

scanf("%d", &qty); // store user input in 'qty'

euro_result = euro * qty; // load values from 'euro' and 'qty',
                          // multiply them and store the result
                          // in 'euro_result'

printf("Euro:       %f \n", euro_result);

Solution 4

I suspect you want to calculate euro_result = euro * qty; only after you have gathered the value for qty.

Share:
15,920
Zach Smith
Author by

Zach Smith

I am trying to learn...everything. I miss the Korean mountains..........:(

Updated on June 14, 2022

Comments

  • Zach Smith
    Zach Smith almost 2 years

    I have a program for a C class I need to write. The program asks for a quantity, and I need to multiply that quantity by another variable which the user inputs. Basic calculator script for a c class :)

    I have it set up like this,

        int qty; //basic quantity var
    float euro, euro_result;
    
    //assign values to my float vars
    euro = .6896; //Euro Dollars
        euro_result = euro * qty; // Euro Dollars multiplied by user input qty
    
    //start program for user
    printf("Enter a quantity: ");
    
    //alow user to input a quantity
    scanf("%d", &qty);
    
    printf("Euro:       %f \n", euro_result);
    

    Why does it not work as expected?

    • Kiran Kumar
      Kiran Kumar over 14 years
      Just a comment..remember to initialize the variable as you define them. so that it becomes easier to debug. For example, instead of doing int qty; define it as int qty = 0;
  • Zach Smith
    Zach Smith over 14 years
    Ah! So put it after the user has selected the qty and it should work. DUH! I am so stupid I did not even think of that.
  • Feidex
    Feidex over 14 years
    Don't worry. It's a common beginner's mistake.
  • Zach Smith
    Zach Smith over 14 years
    a beginner? What gave it away? :)
  • Feidex
    Feidex over 14 years
    It's a common beginner's reaction to think they're stupid :-)
  • Zach Smith
    Zach Smith over 14 years
    is it ok for me to post my finished code on here to have people critique? if not is there an ideal location on the web? i am always seeking gurus to lend me their five minutes...
  • gpilotino
    gpilotino over 14 years
    yep, it's common when you come from lisp