writing functions


 
Thread Tools Search this Thread
Top Forums Programming writing functions
# 1  
Old 11-29-2008
writing functions

[COLOR="Navy"]I have to write a program in C++ using several functions. The program consists of ticket sales how many if you are an adult, junior, or toddler and if there are any discounts. I have the program working up to the pricing function. when i put the discount in the equation it <strike>do</strike> does not work. here is a copy of the code if this helps. this is the part that works right:

Code:
#include <iostream>
#include <string>
#include <iomanip>
#include <ctime>
#include <algorithm>
#include <stdio.h>
#include <time.h>

using namespace std;

char get_ticket_type();
double find_ticket_price(char ticket_type,int num_tickets, double junior_tick, double adult_tick, double sales);
void disp_ticket_type(char ticket_type);



int main()
{
	
	time_t t;

	time(&t); //get current date and time

	struct tm *pt = localtime(&t); //convert time_t to a structure



    //Declare Variables
	int Max_Seat_Count = 40, tot_num_tickets = 0, extra_seats = 0,toddler = 0, a_num_dis_ct = 0, disc_tick_sold = 0;
    int adult_tick_sold = 0, jun_tick_sold = 0, num_dis_ct = 0, num_tickets = 0;
	double  adult_sales = 0.0, a_total_discount_sales = 0.0, j_total_discount_sales = 0.0;
    double junior_tick =7.50, adult_tick = 11.50, grand_total = 0.0, discount = 0.25, jun_sales = 0.0,sales = 0.0, tot_ad_sales = 0.0;
	double a_discount_sales = 0.0, j_discount_sales = 0.0, tot_jun_sales = 0.0; 
    string name = " ";
	char ticket_type = ' ', dis_count = ' ';

   //Intro to Program
    cout <<setw(50) << "Welcome to Seaworld of Summerville Ticketmaster." <<endl << endl;
	cout << "We will be selling alot of tickets for the up coming shows." <<endl;
	cout <<"So lets go over a number of things you should know." << endl;
	cout << "May I please have your Name? ";
    cin >> name;
	cout << endl  << endl;
    cout << "Well, " << name  <<" Let's get started on the basics of the program.  It" << endl;
	cout <<"will be real simple to understand.  All you need to do " <<name <<" is enter how many" << endl;
	cout << "tickets the customer wants of a certian kind of ticket.  The ticket names are" << endl << endl;
	cout << "          'T' for Toddler ages 5 and under are free" << endl;
	cout <<"          'J ' for Juniors age 6 to 16 are $7.50" << endl;
	cout << "          'A' for Adults 16 and over are $11.50" <<endl << endl << endl;
	cout <<"So all you have to do is enter the number of tickest per catagory and" <<endl;
	cout << "the computer will do the rest of the work for you.  You are only aloud"<< endl;
	cout << "to sell 10 tickets per type of seat, the computer is defaulted to 10" << endl;
	cout << "so it will not let you sell anymore than 10 per type of seat." << endl << endl <<endl;
	cout << "If you need to shut down the system all you have to do is" << endl;
	cout << "type 'Q' and your out of the program.." <<endl << endl;
	cout <<"The program will also end when you  sell all the seats availble for the show." << endl << endl; 
	cout << "So Good Luck and have a Great Day!!" << endl;

	// End of program intro
	

	system("pause");
	system("cls");


	ticket_type = get_ticket_type();

	// CENTRAL WHILE LOOP
	
	while (ticket_type != 'Q')
	{
		
		cout <<"Enter how many tickets do you wish to purchase today? : ";
		cin >>num_tickets;
		cout <<endl;

	 //BEGINNING MAX TICKET
		if (num_tickets >10)
		{
			cout <<"ERROR MAXIMUM AMOUT OF TICKETS YOU ARE ALLOWED TO SELL IS '10' ";
			num_tickets = 10;

		}//END OF MAX TICKET
    
		
		sales = find_ticket_price(ticket_type, num_tickets, junior_tick, adult_tick, sales);
	// END WHILE != Q

	
		if (ticket_type == 'T')			
			 toddler += num_tickets;
		else if (ticket_type == 'J')
		{
		
		
			jun_tick_sold += num_tickets;
			jun_sales += sales;
		}
			
		
		
		else 
		 {
		
			adult_tick_sold += num_tickets ;			
			adult_sales += sales;
		 }
	
		
		
		ticket_type = get_ticket_type();
		/*dis_count = apply_discount(ticket_type, dis_count, discount_sales, discount, sales);*/
	}


	if (dis_count == 'Y' && ticket_type =='J')	
	{
		num_dis_ct += num_tickets ;
		j_total_discount_sales += j_discount_sales;
	}
	else if (dis_count == 'Y' && ticket_type == 'A')
	{
		a_num_dis_ct += num_tickets;
		a_total_discount_sales += a_discount_sales;
	 } 




	return 0;
}



// GET TICKET TYPE 

char get_ticket_type()
{
	int num_tickets = 0;
	char ticket_type = ' ';
	cout << "What type of Ticket Do you wish To purchase today? Toddler, Junior, Adult" <<endl;
	cout << setw(10) << "Enter 'T', 'J', or 'A' ticket type or 'Q' to quit: ";
	cin >> ticket_type;
	ticket_type = toupper(ticket_type);	
// WHILE CHECKING TICKET TYPE VALIDITY
while (ticket_type != 'T' &&
				ticket_type != 'J' &&
				ticket_type != 'A' &&
				ticket_type != 'Q')
		{
		cout <<"*****ERROR Wrong ticket Type Try Again*****" <<endl;
		cout  << "Enter 'T', 'J', or 'A' ticket type or 'Q' to quit Enter ticket type -->";
		cin >> ticket_type;	
		ticket_type = toupper(ticket_type);
		cout << endl;
		}//END WHILE CHECKING ERROR
	 


	return ticket_type;	
}// END GET TICKET TYPE


double find_ticket_price(char ticket_type,int num_tickets, double junior_tick, double adult_tick, double sales)
{
	/*int num_tickets = 0; 
	double junior_tick = 0.0, adult_tick = 0.0, sales = 0.0;*/
	
	 char dis_count = ' ';
	 double discount_sales = 0.0, discount = 0.25;
		
	if (ticket_type == 'T')
	{
			
	    disp_ticket_type(ticket_type);
		cout << " Amount is Free IM IN THE LOOP" << endl;
			
	}
	else if (ticket_type == 'J')
	{
		cout << fixed <<setprecision(2);
		sales = num_tickets* junior_tick;
		disp_ticket_type(ticket_type);
		cout << " Total is:IM IN THE LOOP $" <<sales << endl;
		cout << " Do you have any discount? ";
		cin >> dis_count;
		/*if (dis_count == 'Y')
		apply_discount(find_ticket_price, ticket_type, dis_count, discount_sales, discount, sales);*/
		
	}
			
		
		
	else 
	 {
		
		sales = num_tickets * adult_tick;
		
		disp_ticket_type(ticket_type);

		cout << " Total is:IM IN THE LOOP $" <<sales << endl;
		cout << " Do you have any discount? ";
		cin >> dis_count;
		/*if (dis_count == 'Y')
		apply_discount(find_ticket_price,ticket_type, dis_count, discount_sales, discount, sales);*/
	 }
	

	return (sales);
}



void disp_ticket_type(char ticket_type)
{
    ticket_type = toupper(ticket_type);
    if (ticket_type =='T')
        cout <<"Toddler";
    else if (ticket_type =='J')
        cout << "Junior";
        else
        cout<<"Adult";
}//End of Display ticket type




this part does not work right when i add it to the program and i was wondering if someone could tell me what i'm doing wrong thanks!!!

Code:
double apply_discount(double find_ticket_price,char ticket_type, char dis_count, double discount_sales, double discount, double sales)
{
	
	
	
			if (dis_count == 'Y' && ticket_type =='J')		 
			{
				
				discount_sales = sales - ( sales * discount);
				cout << "Amount with Discount is: $" << discount_sales <<endl;
			}
        
		 
			else if (dis_count == 'Y' && ticket_type == 'A')
			{
				
				
				discount_sales = sales - (sales * discount);
				cout << "Amount with Discount is: $" << discount_sales <<endl;
				
			 } 

return discount_sales;
}


Last edited by otheus; 12-05-2008 at 09:04 AM.. Reason: added [code] tags and corrected "do work"
# 2  
Old 12-05-2008
When you call apply_discount, you are trying to store the result in a character value (dis_count). So the compiler is looking for a function named "apply_discount" that returns a character, but you have one that returns a double.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to execute functions or initiate functions as command line parameters for below requirement?

I have 7 functions those need to be executed as command line inputs, I tried with below code it’s not executing function. If I run the ./script 2 then fun2 should execute , how to initiate that function I tried case and if else also, how to initiate function from command line if then... (8 Replies)
Discussion started by: saku
8 Replies

2. Programming

Writing C++ class and member functions

I have the following class and thought that when I call the set command to set a member, I always use value. Would that be fine? class ModMisfit { protected: Real dtau; Real mdacc; Real mindist; bool hw; Source** src; public: void ... (7 Replies)
Discussion started by: kristinu
7 Replies

3. Programming

Functions

Hi All, Can any one help me. I am calling in a function2 with string as parameter from function1, the function1 gives 3 values. how i get the 3 values from funciton2 to function1. i have to give a return or something. thanks in advance. (2 Replies)
Discussion started by: uday.sena.m
2 Replies

4. UNIX for Dummies Questions & Answers

== vs -eq and functions

Hey I have a question.... what is the difference between using == vs -eq when testing in WHILE loops. I use the following test that only worked with == signs.... if why do i need == and not -eq? 2. I need to re-use some code in a couple places in this script. is functions my best... (5 Replies)
Discussion started by: danieldcc
5 Replies

5. Shell Programming and Scripting

i think i need functions ?

Hi, im making a little script but need some help Code i have so far is read -p 'Bot Nickname:' ecnick read -p 'Bot Username:' ecusername read -p 'Bot Realname:' ecrealname read -p 'Your Email:' ecemail echo '' echo Your bots nickname is set to $ecnick echo Your bots username is set to... (2 Replies)
Discussion started by: Gemster
2 Replies

6. UNIX for Dummies Questions & Answers

Help with functions

Hi, I am exploring with defining functions in my BASH shell scripts. However, I am bit confused about how to pass parameters to my functions. I was under the impression that you must do something like the following: Define a function called "sample_function": function sample_function {... (3 Replies)
Discussion started by: msb65
3 Replies

7. Shell Programming and Scripting

functions

I have korn shells where I want to create a function passing $1 to a function , determine my $STAT_ENV value, set the paths and return the paths for STATSH,STATPRM,STATSQR,STATSQL,STATCTL TO BE USED IN THE UNIX SCRIPT THE CALLED THE fucnction in the first place. Can someone tell me the best... (2 Replies)
Discussion started by: TimHortons
2 Replies

8. Shell Programming and Scripting

Use of functions

Hi my shell is tcsh can I have functions in my shell scripting? Is the below shell script correct. Can I have two functions and call one of them as required. ---------- echo "functions" f1 f1 () { echo "hello" } f2 () (1 Reply)
Discussion started by: amitrajvarma
1 Replies

9. Shell Programming and Scripting

functions in

hi could anybody please suggest me how to put a function memory for particular user. say i am a user rao. want have a function foo in memory . i have done this .typed the function function in the shell it worked for the session.but next time i do login its not there . i can i have a... (6 Replies)
Discussion started by: Raom
6 Replies

10. Shell Programming and Scripting

Regarding functions

Hi, I have a function or script like this. show() { echo "Hi" } | tee -a log show This creates a logfile and prints Hi in it. Now when I try to do the same for sql like this: show() { sqlplus -s scott/tiger<<! select * from details; ! } | tee -a log show Then it gives me a... (2 Replies)
Discussion started by: sendhilmani
2 Replies
Login or Register to Ask a Question