Author Topic: Error exporting in Power_C  (Read 1863 times)

Apocalypse

  • Autococker
  • Posts: 1463
Error exporting in Power_C
« on: September 03, 2007, 07:02:32 AM »
Hey guys recently I've been tinkering with Power_C I was making a tax calculator and I get this error
taxcalc.c(11):scanf ("%d" &Price);
********** ^131
131: | and & do not apply to floats or doubles or pointers.
---------------------------------------------------------------------------
taxcalc.c(13)scanf("%d" &Taxrate);
********** ^131
131: | and & do not apply to floats or doubles or pointers.
---------------------------------------------------------------------------
21 lines compiled
 2 Compile errors.

Here is what I put in taxcalc.c do you see anything wrong?
Code: [Select]
main()
{
/* objects */
int Price;
int Taxrate;
int Tax;

/*asking for and receiving amounts*/
printf ("Please enter the first price (no more then 10000 please) to receive the amount/n");
printf ("of tax and the total cost:/n");
scanf ("%d" &Price);
printf ("Now please enter the tax rate:");
scanf ("%d" &Taxrate);

/*calcuating tax*/
Tax = Taxrate * Price;

/*displaying results*/
printf ("The tax for this item is:");
printf ("%5.10d");
}

blaa

  • Autococker
  • Posts: 1218
Re: Error exporting in Power_C
« Reply #1 on: September 03, 2007, 08:32:27 AM »
Please enter the first price (no more then 10000 please) to receive the amount/

no more THAN 10000 please

lekky

  • Autococker
  • Posts: 2449
Re: Error exporting in Power_C
« Reply #2 on: September 03, 2007, 12:00:02 PM »
try replacing:

scanf ("%d" &Price);
scanf ("%d" &Taxrate);

with:

scanf ("%d", &Price);
scanf ("%d", &Taxrate);

(Think the syntax requires a ,)

and:

printf ("%5.10d");

with:

printf("%d", Tax);

(you want to print out the result of the calculation which is held in the Tax variable)


On a side note I have no experience with C, but you should the common convention of using camel case to name variables. eg Tax to tax, Price to price, Taxrate to taxRate.

Apocalypse

  • Autococker
  • Posts: 1463
Re: Error exporting in Power_C
« Reply #3 on: September 04, 2007, 02:29:26 PM »
Now once completed it says the tax for it is 0 :/

lekky

  • Autococker
  • Posts: 2449
Re: Error exporting in Power_C
« Reply #4 on: September 04, 2007, 03:34:02 PM »
It compiles now? Post up the way it looks now Apocolypse :P Did what I say helped?

Cobo

  • Autococker
  • Posts: 1362
Re: Error exporting in Power_C
« Reply #5 on: September 04, 2007, 03:56:27 PM »
idk how it works in Power C but I would have done something like this:

main()
{
int Price;
float Taxrate;
float Tax;

printf ("Please enter the first price (no more then 10000 please) to receive the amount/n");
printf ("of tax and the total cost:/n");

char s[32];
gets(s);
Price = atoi(s);

printf ("Now please enter the tax rate:");

char trs[32];
gets(trs);
Taxrate = atof(trs);

Tax = Taxrate * Price;

printf ("The tax for this item is:");
printf (Tax);
}

Apocalypse

  • Autococker
  • Posts: 1463
Re: Error exporting in Power_C
« Reply #6 on: September 04, 2007, 04:17:16 PM »
Please enter the first price (no more then 10000 please) to receive the amount/

no more THAN 10000 please
Missed this before but yeah it's just beacuse it would have messed up if someone just put in 9999999999 or something it won't now though so I guess I can remove that.
@lekky: Yes it does compile just for some reason it puts the tax as 0.
It says: Please enter the first price (no more then 10000 please) to receive the amount I type 100 then it says: Now please enter the Taxrate I put .1 (I wish lol) then it says the tax for this item is: 0
Power C is just another C compiler it's still in the C language.

lekky

  • Autococker
  • Posts: 2449
Re: Error exporting in Power_C
« Reply #7 on: September 04, 2007, 05:28:32 PM »
aha. Apocolypse.

int Taxrate;

int stands for integer which is a whole number. By inputting 0.1 where an integer is expected it must round down to 0, and anything mutliplied by 0 is 0 :P

Do what Cobo said and change your three variables (I would change all 3) to floating point numbers. This allows a decimal spot.

So now it would be:

float Price;
float Taxrate;
float Tax;

I think you might also need to change:
scanf ("%d", &Price);
scanf ("%d", &Taxrate);

to:
scanf ("%f", &Price);
scanf ("%f", &Taxrate);

and:
printf("%d", Tax);

to:
printf("%f", Tax);


Also Apocolypse, don't just make these changed blindly. Its important to understand why you are making the changes. If* that works and you don't unserstand why it didn't before but does now do some net research or ask :P

Apocalypse

  • Autococker
  • Posts: 1463
Re: Error exporting in Power_C
« Reply #8 on: September 04, 2007, 05:35:15 PM »
I know why %f is used to write floating while %d is used for integer. Integer is wrong because you need to be able to use decimals and you should always use floats for money not integers because you can have 1.99 for 1 dollar and 99 cents I do have a book you know thanks though lekky :)

lekky

  • Autococker
  • Posts: 2449
Re: Error exporting in Power_C
« Reply #9 on: September 04, 2007, 05:36:56 PM »
:P

I was just making sure because sometimes people do things without knowing why when learning a language. PS Let me know if it works.

Cobo

  • Autococker
  • Posts: 1362
Re: Error exporting in Power_C
« Reply #10 on: September 04, 2007, 05:46:52 PM »
Hm, I'm pretty sure %d doesnt stand for integer, its %i.
%d = double maybe?

lekky

  • Autococker
  • Posts: 2449
Re: Error exporting in Power_C
« Reply #11 on: September 04, 2007, 06:00:51 PM »
Apparently %d identifies a decimal integer, ie base 10 where as %i detects the base itself. %f identifies a double.


(Java ftw).

Apocalypse

  • Autococker
  • Posts: 1463
Re: Error exporting in Power_C
« Reply #12 on: September 05, 2007, 02:20:02 PM »
:P

I was just making sure because sometimes people do things without knowing why when learning a language. PS Let me know if it works.
It worked :)