Thread: Total()

  1. #1
    janet
    Guest

    Angry Total()

    here is my sourc code.
    I have one question.
    is there anyway I can set the total(),for not allowing any purchase more than $500.00.
    Code:
    #include <stdio.h>
    #include <ctype.h>
    #include <conio.h>
    
    double getfloat(double lower);
    double total(int qty, double money);
    double show(double ticket_price,int number_of_tickets);
    
    /* ========================================================================== */
     int main()
     {
     double ticket_price;
     double number_of_tickets;
     char choice;
     
     
     clrscr();
     do
       {
    
    	 printf("\nEnter the ticket price between(5 or greater than 5):  ");
    	 ticket_price=getfloat(5);
    
    	 
    	 printf("\nEnter the number of tickets:  ");
    	 number_of_tickets=getfloat(1);
    	 
    	
    	 show(ticket_price, number_of_tickets);
    	 
    	printf("\nContinue (Y/N): ");
       while(getchar() != '\n');
       scanf("%c", &choice);
       }
    
       while (choice == 'Y' || choice == 'y');
    
    getchar();
    return 0;
         
     }
    /* ========================================================================== */
     double getfloat(double lower)
    {
    	double number;
    
    	scanf("%lf",&number);
    	while (number < lower)
    	{
    		printf("Illegal value %lf; please re-enter a value : ",
    				 number,lower);
    		scanf("%lf",&number);
    	}
    	return(number);
    }
    /* ========================================================================== */
    double total(int qty, double money)
    {
    	return qty*money;
    }
    /* ========================================================================== */
    double show(double ticket_price, int number_of_tickets)
    
    {
       double total_owed;
    
       printf("The ticket price is -->$%.2lf\n", ticket_price);
    	printf("The number of tickets is ----> %d\n", number_of_tickets);
    
       total_owed=total(ticket_price, number_of_tickets);
    
    	printf("Total owed --------> $%.2lf\n", total_owed);
    
    	return 0;
    }
    /* ========================================================================== */
    Code tags added by Kermi3

  2. #2
    Green Member Cshot's Avatar
    Join Date
    Jun 2002
    Posts
    892
    The way you designed your program, the best place to put it is in the show() function.

    If total_owed is greater than $500, just print out an invalid message and return with an error. Have your main function capture the return value for the show() function and proceed accordingly.

    One way to do it is:

    Code:
    #define NO_ERROR 0
    ...
    returnValue = show(ticket_price, number_of_tickets);
    
    if(returnValue == NO_ERROR)
    {
      printf("\nContinue (Y/N): ");
       while(getchar() != '\n');
       scanf("%c", &choice);
    }
    Try not.
    Do or do not.
    There is no try.

    - Master Yoda

  3. #3
    Lead Moderator kermi3's Avatar
    Join Date
    Aug 1998
    Posts
    2,595
    janet -
    I am posting this because you did not use code tags on this thread. In the furture please use Code Tags. They make your code MUCH easier to read and people will be much more likely to help you if you do. And they'll be happy about helping you


    For example:

    Without code tags:

    for(int i=0;i<5;i++)
    {
    cout << "No code tags are bad";
    }

    With Code Tags:
    Code:
    for(int i=0;i<5;i++)
    {
         cout << "This code is easy to read";
    }
    This is of course a basic example...more complicated code is even easier to read with code tags than without.

    I've added code tags for you this time. They can be added by putting [ code ] at the beginning of your code and [ /code ] at the end, only without the spaces. More information on code tags may be found at the link in my signature. Any further questions or ways I can help please feel free to PM me.
    Kermi3

    If you're new to the boards, welcome and reading this will help you get started.
    Information on code tags may be found here

    - Sandlot is the highest form of sport.

  4. #4
    janet
    Guest

    c programming

    thank you very much for responding me .you are great

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with day of the week program
    By Punkakitty in forum C++ Programming
    Replies: 10
    Last Post: 01-14-2009, 06:55 PM
  2. Replies: 8
    Last Post: 11-03-2008, 09:48 PM
  3. long problem
    By loko in forum C Programming
    Replies: 28
    Last Post: 07-22-2005, 09:38 AM
  4. Loops
    By Dt7 in forum C++ Programming
    Replies: 3
    Last Post: 10-23-2004, 06:11 PM
  5. Replies: 4
    Last Post: 04-22-2003, 12:52 PM