New Programmer in College

This is a discussion on New Programmer in College within the C Programming forums, part of the General Programming Boards category; Im taking a class with a book called C by Dissection, Pretty good book so far no complaints. I am ...

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    6

    New Programmer in College

    Im taking a class with a book called C by Dissection, Pretty good book so far no complaints. I am just having problems with a lil bit of the code and understanding the the if statements. Basically what I need it to say is :

    ************************************************** ************************************************** ****

    Adjust your program to allow for cases where the user either breaks even or loses money. If the user breaks even, print out the following statement:

    You have broken even from selling goldfish.

    If the user loses money, print out a statement of the following format:

    Your loss from selling goldfish is $XX.XX.

    ************************************************** ************************************************** ****


    Here is a sample of my code. Any suggestions would be greatly appreciated as I am new to the programming world and am VERY LOST:

    Code:
    #include <stdio.h>
    
    int main(void)
    
    
    		
    {
    
    	float length1,width1,height1,area,volume,total,sum1,sum2,finalsum,revenue,profit;
    	int fishsum;
    	#define PRICE1 .02
    	#define PRICE2 .005
    	
    		
    	
    		printf("\nWhat is the length of your goldfish tank in inches? ");
    		scanf("%f",&length1);
    		printf("\nWhat is the width of your goldfish tank in inches? ");
    		scanf("%f",&width1);
    		printf("\nWhat is the height of your goldfish tank in inches? ");
    		scanf("%f",&height1);
    
    
    	area = (length1  * height1 * 2) + (height1 * width1 * 2) + (length1 * width1);
    	volume = (length1  * width1  * height1);
    	fishsum = volume / 250;
    	sum1 = PRICE1 * area;
    	sum2 = PRICE2 * volume;
    	finalsum = sum1 + sum2;
    	revenue = fishsum * 5;
    	profit = revenue - finalsum;
    	
    			
    	if (profit > 0)
    	{
    		printf("\nYour loss from selling goldfish is $%.2f. \n", profit); 
    	}
    	else
    	{
    		if (profit < 0)
    		{
    			printf("\nYour profit is $%.2f. \n", profit); 
    	
    	}
    	}
    	return 0;
    }
    Last edited by bronson37; 09-24-2004 at 01:19 AM.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    1) You'll want to read the forum Announcements. They'll cover how to use [code] tags.
    2) When using floating point numbers, you must make all of your comparisons floating point numbers also. As such, you want something like this instead:
    Code:
    if( foo < 0.0 )
        ...do whatever...
    3) When stating a problem, don't just post code, tell us:
    a) what it does that it shouldn't
    b) what it doesn't that it should
    c) any errors or warnings you get
    d) something else I'm forgetting but have posted recently :hic:



    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Sep 2004
    Posts
    6
    Yeah sorry I caught that code thing once I had already posted! Its fixed now!

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Edit your program, and wherever you have:

    .x make it 0.x
    x make it x.0

    And then see if that helps. It'll also help if you state what it's supposed to give you for a given set of input. For example, if the input is as follows:
    1
    3
    5

    and the expected output is:
    10

    but you get:
    14

    Then say as much. It helps in debugging.

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Sep 2004
    Posts
    6
    When I run the program my input is:
    6
    6
    6

    My output should read:
    Your loss from selling goldfish is $4.68.

    When I input these numbers I do get my desired output.
    However, my next input is supposed to be :
    24
    12
    16

    And desired output should read:
    Your profit from selling golfish is $38.16

    My output reads:
    Your profit from selling goldfish is $-38.16

    So, basically it's the negative sign in the profit thats giving me trouble.

    Sorry for the previous postings, but I am new here. Thanks for showing me the ropes.

    Bronz

  6. #6
    ---
    Join Date
    May 2004
    Posts
    1,379
    you have your if statements wrong
    Code:
    if (profit > 0)
    	{
    		printf("\nYour loss from selling goldfish is $%.2f. \n", profit); 
    	}
    	else
    	{
    		if (profit < 0)
    		{
    			printf("\nYour profit is $%.2f. \n", profit); 
    	
    	}
    	}
    should be
    Code:
    if (profit < 0)
    {
            printf("\nYour loss from selling goldfish is $%.2f. \n", profit);
    }
    else if (profit > 0)
    {
            printf("\nYour profit is $%.2f. \n", profit);
    }
    
    /****
    else
    {
            printf("\nYou broke even!");
    }
    ****/
    apart from getting the '<' and '>' operators around the wrong way, you werent using 'else if' properly.
    Last edited by sand_man; 09-24-2004 at 08:14 AM.

  7. #7
    Registered User
    Join Date
    Sep 2004
    Posts
    6
    thank the help. I appreciate it.

    bronz

  8. #8
    n00b
    Join Date
    Sep 2004
    Posts
    3
    I'm having a similar problem with the same program. Strange that we have the exact same homework. You wouldn't be chance be attending ucf would you?

    Anyway here is what my code looks like.

    Code:
    #include <stdio.h>
    int main( )
    {       
    	/* I used the int function to define totalfishsale because we cant have a fraction or 
    	   .xx of a fish.*/  
    	int totalfishsale;
    	
    	/* First I defined the variables via the double function length, width and height as l, w, and h. 
    	   I also defined priceperinch, maintenance, profit, fishprice, and cost here. */
    	double l, w, h, cost, priceperinch = 0.02, maintenance = 0.005, profit, fishprice = 5.00;
    	
    	/* The first print and scan functions prompt the user for the length of the fish tank in inches*/
    	printf("What is the length of your fish tank in inches?\n");
    	scanf("%lf", &l);
    	
    	/* The second print and scan functions prompt the user for the width of the fish tank in inches */
    	printf("What is the width of your fish tank in inches?\n");
    	scanf("%lf", &w);
    	
    	/* The third print and scan functions prompt the user for the height of the fish tank in inches */
    	printf("What is the height of your fish tank in inches?\n");
    	scanf("%lf", &h);
    		
    	/* This Equation calculates the cost of maintenance and construction of the fish tank */	
    		cost = ( ( (l * w) + (h * l * 2) + (w * h * 2) )  * priceperinch ) + ( ( l * w * h ) * maintenance );
    	
    	/* This equation calculates the total profit for the amount of fish sold at the fishprice */	
    		totalfishsale = ( ( l * w * h )/250.0 ) * fishprice;
    		
    	/* This equation subtracts the cost of production from the total profit for the amount of fish sold */		
    		profit = totalfishsale - cost;
    		
    		if ( profit < 0.0 )
    		{	
    			printf("\n Your loss from selling fish is $%.2f\n", profit);
    		} 	
    		else if ( profit > 0.0 ) 
    		{
    			printf("\n Your profit from sellling fish is $%.2f\n", profit); 
    		}
    		else
    		{	
    		 	printf("\n You break even from selling fish.\n");
    		}
    			
    
    }
    Now when i input the variable 6, 6, and 6 instead of getting 4.68 I'm gettting -0.68. I've looked it over a number of times and compared my code to bronson's but i can't seem to figure out what's wrong. I'm thinking maybe it's a format issue or something? I don't know I'm still really new to the whole coding business since I've only ever done html before.

    -matt

  9. #9
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote Originally Posted by lysol
    I'm having a similar problem with the same program.
    Now when i input the variable 6, 6, and 6 instead of getting 4.68 I'm gettting -0.68. I've looked it over a number of times and compared my code to bronson's but i can't seem to figure out what's wrong. I'm thinking maybe it's a format issue or something? I don't know I'm still really new to the whole coding business since I've only ever done html before.

    -matt
    So, the first thing is to write some code. You've done that.
    Next compile it and eliminate errors, notice warnings, etc. You've done that.

    Now run with a test case for which you know the answers.

    OK so far? Time for more testing.

    Not OK? Time for debugging.

    Go over the code. You've done that (I assume). Can't see anything wrong? What's the next step?

    PUT IN SOME prinf() statements at strategic locations:

    After the program has acquired input from the user, print out the values that the program is going to use (so you can see if there was any format errors, etc.)
    So, put the following just before the cost calculation.

    Code:
      printf("You entered: l = %f, w = %f, h = %f\n", l, w, h);

    If you have complicated calculations you might want to break them down and print out partial results.

    In this case, just put the following after the cost calculation:
    Code:
        printf("cost = %f\n", cost);
    and put the following after the totalfishsale calclulation:
    Code:
        printf("totalfishsale = %f\n", totalfishsale);
    Get the idea?

    The idea is for you to learn about progamming, and this means for you to learn something about debugging. The most powerful debugging tool: your brain. The second most powerful debugging tool: printf() (or cout << for you c++ fans). This wonderful forum certainly appears on my list of debugging tools, but not in the top 2.

    Regards,

    Dave

  10. #10
    n00b
    Join Date
    Sep 2004
    Posts
    3
    Thankyou sooooooo so so so so much. Printf statements saved my life. I figured it out, i guess there was something in there wrong with my equation,
    Code:
    	totalfishsale = ( ( l * w * h )/250.0 ) * fishprice;
    I'm guessing it was a float/double to integer conversion problem? I fixed it by dividing the equation into two parts and declaring a new variable:

    Code:
    /* This equation calculates the total profit for the amount of fish sold*/	
    		totalfishsale = ( ( l * w * h )/250.0 );
    		
    	/* This equation calculates revenue by multiplying totalfishsale by fishprice*/
            revenue = totalfishsale * fishprice;
            
    	/* This equation subtracts the cost of production from the total profit for the amount of fish sold */		
    		profit = revenue - cost;
    Now i just have one more problem, i'm getting -4.68 as the output for my loss calculation, is there a way i can drop the negative sign without throwing off the if statement since it's assumed that 4.68 is the loss?

  11. #11
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    Code:
    printf("\n Your loss from selling fish is $%.2f\n", profit * -1.0);

  12. #12
    n00b
    Join Date
    Sep 2004
    Posts
    3
    ah thankyou much. I was throwing positive and negative signs all over the place haha.

  13. #13
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,162
    Or you can just do -profit instead of profit * -1.0.
    If you understand what you're doing, you're not learning anything.

  14. #14
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    Quote Originally Posted by Dave Evans
    The most powerful debugging tool: your brain. The second most powerful debugging tool: printf() (or cout << for you c++ fans).
    gdb has to have a place in there somewhere too

  15. #15
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Although I'm glad the problem has been solved, I have one thing to point out:
    >>you werent using 'else if' properly.
    It was used properly; else if(...){} is the same as else{if(...){}}, except in the first example the redundant {} around the if(){} were dropped since if(){} counts as a single block of code. But the original code was perfectly valid.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Should I go to a state college and be 20k in debt, or a private college and be 90k in
    By Terrance in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 10-10-2003, 08:22 PM
  2. University versus college, which should I do?
    By Terrance11 in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 07-02-2003, 11:19 PM
  3. Which college should I go to?
    By Terrance in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 05-13-2003, 10:06 PM
  4. College or No College :: Future
    By kuphryn in forum A Brief History of Cprogramming.com
    Replies: 13
    Last Post: 09-25-2002, 03:48 PM
  5. University College
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 11-30-2001, 01:55 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21