C coding class

Posts 15 of 5 · Page 1 of 1
C coding class
So I'm taking a Intro to C coding class, and my teacher want me to do a simple code for out hw.

Code:
#include <stdio.h>

int main(void)
{
	double orLbs;
	int enerNum, yoNum;
	double orPrice, ePrice, yPrice;
	double tax = 0.065;

	orPrice = orLbs*0.95;
	ePrice = enerNum*1.5;
	yPrice = yoNum*0.75;

	printf("Lbs of Oranges? ");
	scanf("%lf", &orLbs);
	printf("Number of energy drinks? ");
	scanf("%li", &enerNum);

	printf("Item		Cost		Tax\n");
	printf("Orange		%.2f\n", orPrice);
	printf("Energy Drinks	%.2f		%.2f\n", ePrice, ePrice*tax);
}
the problem is that it's not calculating the price correctly, what is wrong with the code?


- - - Updated - - -

btw, Im righting the code and running it on a unix server
Quote Originally Posted by w4ssup View Post
So I'm taking a Intro to C coding class, and my teacher want me to do a simple code for out hw.

Code:
#include <stdio.h>

int main(void)
{
	double orLbs;
	int enerNum, yoNum;
	double orPrice, ePrice, yPrice;
	double tax = 0.065;

	orPrice = orLbs*0.95;
	ePrice = enerNum*1.5;
	yPrice = yoNum*0.75;

	printf("Lbs of Oranges? ");
	scanf("%lf", &orLbs);
	printf("Number of energy drinks? ");
	scanf("%li", &enerNum);

	printf("Item		Cost		Tax\n");
	printf("Orange		%.2f\n", orPrice);
	printf("Energy Drinks	%.2f		%.2f\n", ePrice, ePrice*tax);
}
the problem is that it's not calculating the price correctly, what is wrong with the code?


- - - Updated - - -

btw, Im righting the code and running it on a unix server
Okay first of you should initialize all your variables, it's a bad habit and can cause errors. Second you need to run the code that actually does the math after you get the values! Good luck on learning C.
Code:
#include <stdio.h>
int main(void)
{
	double orLbs = 0;
	int enerNum = 0, yoNum = 0;
	double orPrice = 0, ePrice = 0, yPrice = 0;
	double tax = 0.065;

	printf("Lbs of Oranges? ");
	scanf("%lf", &orLbs);
	printf("Number of energy drinks? ");
	scanf("%li", &enerNum);

	orPrice = orLbs*0.95;
	ePrice = enerNum*1.5;
	yPrice = yoNum*0.75;

	printf("Item		Cost		Tax\n");
	printf("Orange		%.2f\n", orPrice);
	printf("Energy Drinks	%.2f		%.2f\n", ePrice, ePrice*tax);
}
Quote Originally Posted by Bitset View Post
Okay first of you should initialize all your variables, it's a bad habit and can cause errors. Second you need to run the code that actually does the math after you get the values! Good luck on learning C.
Code:
#include <stdio.h>
int main(void)
{
	double orLbs = 0;
	int enerNum = 0, yoNum = 0;
	double orPrice = 0, ePrice = 0, yPrice = 0;
	double tax = 0.065;

	printf("Lbs of Oranges? ");
	scanf("%lf", &orLbs);
	printf("Number of energy drinks? ");
	scanf("%li", &enerNum);

	orPrice = orLbs*0.95;
	ePrice = enerNum*1.5;
	yPrice = yoNum*0.75;

	printf("Item		Cost		Tax\n");
	printf("Orange		%.2f\n", orPrice);
	printf("Energy Drinks	%.2f		%.2f\n", ePrice, ePrice*tax);
}
I think you meant shouldn't, but thanks it works now
Research on how to use a debugger; It will help you find errors as they happen.
Quote Originally Posted by Dave84311 View Post
Research on how to use a debugger; It will help you find errors as they happen.
Unix doesnt give me an error, and I tried googling "C code tester" or "c code analyzer" and they didnt work out so well. I use to have vs2010, but i deleted it lol
Posts 15 of 5 · Page 1 of 1

Post a Reply

Similar Threads

Tags for this Thread

None

Need help?