Author Topic: I thing I found a bug  (Read 4609 times)

dtrich

  • Guest
I thing I found a bug
« on: September 19, 2009, 05:02:14 pm »
I wrote this simple program in C:

/*
* Exercise 2-3.                                                       *
* You’re selling a product that’s available in two versions:          *
* type 1 is a standard version priced at $3.50, and                   *
* type 2 is a deluxe version priced at $5.50.                         *
* Write a program using only what you’ve learned up to now            *
* that prompts for the user to enter the product type and a quantity, *
* and then calculates and outputs the price for the quantity entered. */

#include <stdio.h>

int main(void)
{

    short int q=0;     /* quantity */
    short int x=0;     /* selection switch */

    //prompt for version and quantity
    printf("\nPlease, give the type 1:normal, 2:deluxe: ");
    scanf("%d",&x);

    printf("\nPlease, give the quantity: ");
    scanf("%d",&q);

    //calculations - printout
    printf("\nThe Total price is: %0.2f\n", (x*2+1.5)*q);

    return 0;
}

when I exchange the lines:
    
    short int q=0;     /* quantity */
    short int x=0;     /* selection switch */

each-other (declaring first the x and after the q) the result is wrong.

I use the  Code::Blocks 8.02 built Feb27 2008 wx2.8.7(windows version)

Offline danselmi

  • Developer
  • Almost regular
  • *****
  • Posts: 206
Re: I thing I found a bug
« Reply #1 on: September 19, 2009, 05:20:58 pm »
What compiler do you use? Mine gave me a warning about your code! It is a good hint about the error.

Offline Jenna

  • Administrator
  • Lives here!
  • *****
  • Posts: 7252
Re: I thing I found a bug
« Reply #2 on: September 19, 2009, 05:45:12 pm »
Your problem is a programming error, not a compiler nor a C::B bug.

danselmi's hint can help you.

Read the documentation of scanf.

Unrelated to C::B, thus violating our forums rules.

Topic locked !!