Thread: While statement Help

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

    While statement Help

    Im having trouble using the while statement for a fishtank program.

    Here is what I have so far

    Code:
    
    #include <stdio.h>
    
    int main()
    
    		
    {
    
    	float length1,width1,height1,area,volume,total,sum1,sum2,finalsum,revenue,profit;
    	int fishsum,num = 1;
    	#define PRICE1 .02
    	#define PRICE2 .005
    	
    	printf ("\nWould you like to calculate your fishtank profit (1=yes, 0=No)?");
     	 scanf ("%d",&num );
    
    while ( num !=0) 
    {
    		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 * -1);
    }
    else if (profit > 0)
    {
            printf("\nYour profit is $%.2f. \n", profit);
    }
    
    
    else
    {
            printf("\nYou broke even!");
    }}
    
    printf("\nThank you for using the fish tank program, goodbye!");
    
    }

    After typing in the number 1, the program proceeds to ask me for the length, width, and height, and outputs the correct answer.

    However, After giving me the output, the program asks me for the length again. What I want the program to do is to ask me if I would like to calculate your fish tank profit. Then I have the option to type 1 for yes and to repeat the process or 0 to end the program.

    Please help

    Thanks Bronz

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    do
        display prompt
        get input
        if input == go go go
            do fishtank stuff
    while input != stop stop stop
    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Jun 2004
    Posts
    722
    Very simple...
    Do this.
    Code:
    while(1){
    	printf ("\nWould you like to calculate your fishtank profit (1=yes, 0=No)?");
    	scanf ("%d",&num );
    	if(num==0) break;
    	/*...rest of cicle...*/
    }

  4. #4
    Registered User
    Join Date
    Sep 2004
    Posts
    6
    Thanks a bunch guys. That solved everything and everything works great now.

    Thanks,
    Bronz

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Usefulness of the "else if" statement
    By gn17 in forum C Programming
    Replies: 7
    Last Post: 08-12-2007, 05:19 AM
  2. Meaning of this statement?
    By @nthony in forum C Programming
    Replies: 7
    Last Post: 07-16-2006, 02:57 AM
  3. If Else statement problem
    By doofusboy in forum C Programming
    Replies: 2
    Last Post: 11-09-2005, 07:18 AM
  4. if/break statement
    By Apropos in forum C++ Programming
    Replies: 7
    Last Post: 02-22-2005, 02:33 PM
  5. Uh-oh! I am having a major switch problem!
    By goodn in forum C Programming
    Replies: 4
    Last Post: 11-01-2001, 04:49 PM