Thread: Need help creating functions for school project

  1. #1
    Registered User
    Join Date
    Nov 2015
    Posts
    1

    Need help creating functions for school project

    Code:
    #include <iomanip>
    #include <cmath>
    #include <iostream>
    #include <string>
    #include <fstream>
    const float TAX = .07, TIPS = .15, DESERT = 4.50, CHILD_RATE = .6;
    
    
    
    
    using namespace std;
    
    
    int main()
    { string name;
        float cost_adult, room_fee, deposit;
        int num_adults, num_children, num_desert;
        //input section
        cout << "What is your name? : ";
        getline(cin,name);
        cout << "How many adults will be served? : "; cin >> num_adults;
        cout << "How many children will be served? : "; cin >> num_children;
        cout << "How many pieces of dessert will you need? : "; cin >> num_desert;
        cout << "What is the cost per adult meal? : "; cin >> cost_adult;
        cout << "What is your room fee? : "; cin >> room_fee;
        cout << "What is the amouunt of deposit paid when the reservation was made? : "; cin >> deposit;
        
        //proccessing section
        float cost_child, total_cost_adult, total_cost_child, total_cost_desert, total_cost_food, taxes, total_bill, balance_due, tips;
        
        cost_child = CHILD_RATE * cost_adult;
        total_cost_adult = cost_adult * num_adults;
        total_cost_child = cost_child * num_children;
        total_cost_desert = DESERT * num_desert;
        total_cost_food = total_cost_adult + total_cost_child + total_cost_desert;
        taxes = total_cost_food * TAX;
        tips = total_cost_food * TIPS;
        total_bill = total_cost_food + taxes + tips;
        balance_due = total_bill - deposit;
        
        void output()
        {
        
        cout<< setfill('-')<<setw(80)<<"-"<<endl;
        cout<< '\n'<<setw(18);
        cout<< "Passaic County Catering & Convention Services   Final Bill"<<endl;
        cout<< '\n'<<setw(4)<<"Customer: "<<"\t\t\t\t\t\t"<<name;
        cout<< '\n'<<setw(4)<<"Number of Adults:"<<"\t\t\t\t$"<<num_adults;
        cout<< '\n'<<setw(4)<<"Number of Children:"<<"\t\t\t\t$"<<num_children;
        cout<< '\n'<<'\n'<<setw(4)<<"Cost of Meal Per Adult:"<<"\t\t\t$"<<cost_adult;
        cout<< '\n'<<setw(4)<<"Cost Of Child's Meal Per Adult:"<<setprecision (2)<<fixed<<"\t$"<<cost_child;
        cout<< '\n'<<setw(4)<<"Total Cost for Desert:"<<"\t\t\t$"<<total_cost_desert;
        cout<< '\n'<<setw(4)<<"Total Food Cost:"<<"\t\t\t\t$"<<total_cost_food;
        cout<< '\n'<<'\n'<<setw(4)<<"Plus 7.5 % Taxes:"<<"\t\t\t\t$"<<taxes;
        cout<< '\n'<<setw(4)<<"Plus 15.0 % Tips:"<<"\t\t\t\t$"<<tips;
        cout<< '\n'<<setw(4)<<"Plus Room Fee:"<<"\t\t\t\t\t$"<<room_fee;
        cout<< '\n'<<'\n'<<setw(4)<<"Total Bill:"<<"\t\t\t\t\t\t$"<<total_bill;
        cout<< '\n'<<setw(4)<<"Less Deposit:"<<"\t\t\t\t\t$"<<deposit;
        cout<< '\n'<<'\n'<<setw(4)<<"Balance Due:"<<"\t\t\t\t\t$"<<balance_due;
        cout<< '\n'<<'\n'<<setw(18)<<"Thank You For Using Passaic County Catering Services."<<endl;
        cout<< setfill('-')<<setw(80)<<"-"<<endl;
        }
    
    
        
        return 0;
    }



    That's my program so far and it works well except I want to change the group of statements under input section, processing section, and output section into functions. I tried putting making a prototype with output but I just have no clue what to do to make it into a separate functions that I can call in main.

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Since this is C++ your program should not work at all. In C++ you can't implement functions inside other functions.

    Jim

  3. #3
    Registered User
    Join Date
    Feb 2013
    Location
    Sweden
    Posts
    171
    You could skip the output() function and do this instead to make the C++ syntax correct.

    Code:
    #include <iomanip>
    #include <cmath>
    #include <iostream>
    #include <string>
    #include <fstream>
    const float TAX = .07, TIPS = .15, DESERT = 4.50, CHILD_RATE = .6;
     
     
     
     
    using namespace std;
     
     
    int main()
    { 
    	string name;
        float cost_adult, room_fee, deposit;
        int num_adults, num_children, num_desert;
        
        //input section
        cout << "What is your name? : ";
        getline(cin,name);
        cout << "How many adults will be served? : "; cin >> num_adults;
        cout << "How many children will be served? : "; cin >> num_children;
        cout << "How many pieces of dessert will you need? : "; cin >> num_desert;
        cout << "What is the cost per adult meal? : "; cin >> cost_adult;
        cout << "What is your room fee? : "; cin >> room_fee;
        cout << "What is the amouunt of deposit paid when the reservation was made? : "; cin >> deposit;
         
        //proccessing section
        float cost_child, total_cost_adult, total_cost_child, total_cost_desert, total_cost_food, taxes, total_bill, balance_due, tips;
         
        cost_child 			= CHILD_RATE * cost_adult;
        total_cost_adult 	= cost_adult * num_adults;
        total_cost_child 	= cost_child * num_children;
        total_cost_desert 	= DESERT * num_desert;
        total_cost_food  	= total_cost_adult + total_cost_child + total_cost_desert;
        taxes 				= total_cost_food * TAX;
        tips				= total_cost_food * TIPS;
        total_bill 			= total_cost_food + taxes + tips;
        balance_due 		= total_bill - deposit;
         
    
    
        cout<< setfill('-')<<setw(80)<<"-"<<endl;
        cout<< '\n'<<setw(18);
        cout<< "Passaic County Catering & Convention Services   Final Bill"<<endl;
        cout<< '\n'<<setw(4)<<"Customer: "<<"\t\t\t\t\t\t"<<name;
        cout<< '\n'<<setw(4)<<"Number of Adults:"<<"\t\t\t\t$"<<num_adults;
        cout<< '\n'<<setw(4)<<"Number of Children:"<<"\t\t\t\t$"<<num_children;
        cout<< '\n'<<'\n'<<setw(4)<<"Cost of Meal Per Adult:"<<"\t\t\t$"<<cost_adult;
        cout<< '\n'<<setw(4)<<"Cost Of Child's Meal Per Adult:"<<setprecision (2)<<fixed<<"\t$"<<cost_child;
        cout<< '\n'<<setw(4)<<"Total Cost for Desert:"<<"\t\t\t$"<<total_cost_desert;
        cout<< '\n'<<setw(4)<<"Total Food Cost:"<<"\t\t\t\t$"<<total_cost_food;
        cout<< '\n'<<'\n'<<setw(4)<<"Plus 7.5 % Taxes:"<<"\t\t\t\t$"<<taxes;
        cout<< '\n'<<setw(4)<<"Plus 15.0 % Tips:"<<"\t\t\t\t$"<<tips;
        cout<< '\n'<<setw(4)<<"Plus Room Fee:"<<"\t\t\t\t\t$"<<room_fee;
        cout<< '\n'<<'\n'<<setw(4)<<"Total Bill:"<<"\t\t\t\t\t\t$"<<total_bill;
        cout<< '\n'<<setw(4)<<"Less Deposit:"<<"\t\t\t\t\t$"<<deposit;
        cout<< '\n'<<'\n'<<setw(4)<<"Balance Due:"<<"\t\t\t\t\t$"<<balance_due;
        cout<< '\n'<<'\n'<<setw(18)<<"Thank You For Using Passaic County Catering Services."<<endl;
        cout<< setfill('-')<<setw(80)<<"-"<<endl;
     
         
        return 0;
    }
    Otherwise you have to implement a function outside of the main()-function block.
    Then you could let the output()-function do most of the tasks.

    Code:
    #include <iomanip>
    #include <cmath>
    #include <iostream>
    #include <string>
    #include <fstream>
    const float TAX = .07, TIPS = .15, DESERT = 4.50, CHILD_RATE = .6;
     
     
     
     
    using namespace std;
     
    void output(); 
     
    int main()
    {
    	output(); 
        return 0;
    }
    
    
    void output()
        {
        string name; 
        float cost_adult, room_fee, deposit;
        int num_adults, num_children, num_desert;
        
        //input section
        cout << "What is your name? : ";
        getline(cin,name);
        cout << "How many adults will be served? : "; cin >> num_adults;
        cout << "How many children will be served? : "; cin >> num_children;
        cout << "How many pieces of dessert will you need? : "; cin >> num_desert;
        cout << "What is the cost per adult meal? : "; cin >> cost_adult;
        cout << "What is your room fee? : "; cin >> room_fee;
        cout << "What is the amouunt of deposit paid when the reservation was made? : "; cin >> deposit;
        
        //proccessing section
        float cost_child, total_cost_adult, total_cost_child, total_cost_desert, total_cost_food, taxes, total_bill, balance_due, tips;
        
        cost_child 			= CHILD_RATE * cost_adult;
        total_cost_adult 	= cost_adult * num_adults;
        total_cost_child 	= cost_child * num_children;
        total_cost_desert 	= DESERT * num_desert;
        total_cost_food  	= total_cost_adult + total_cost_child + total_cost_desert;
        taxes 				= total_cost_food * TAX;
        tips				= total_cost_food * TIPS;
        total_bill 			= total_cost_food + taxes + tips;
        balance_due 		= total_bill - deposit;
        
        cout<< setfill('-')<<setw(80)<<"-"<<endl;
        cout<< '\n'<<setw(18);
        cout<< "Passaic County Catering & Convention Services   Final Bill"<<endl;
        cout<< '\n'<<setw(4)<<"Customer: "<<"\t\t\t\t\t\t"<<name;
        cout<< '\n'<<setw(4)<<"Number of Adults:"<<"\t\t\t\t$"<<num_adults;
        cout<< '\n'<<setw(4)<<"Number of Children:"<<"\t\t\t\t$"<<num_children;
        cout<< '\n'<<'\n'<<setw(4)<<"Cost of Meal Per Adult:"<<"\t\t\t$"<<cost_adult;
        cout<< '\n'<<setw(4)<<"Cost Of Child's Meal Per Adult:"<<setprecision (2)<<fixed<<"\t$"<<cost_child;
        cout<< '\n'<<setw(4)<<"Total Cost for Desert:"<<"\t\t\t$"<<total_cost_desert;
        cout<< '\n'<<setw(4)<<"Total Food Cost:"<<"\t\t\t\t$"<<total_cost_food;
        cout<< '\n'<<'\n'<<setw(4)<<"Plus 7.5 % Taxes:"<<"\t\t\t\t$"<<taxes;
        cout<< '\n'<<setw(4)<<"Plus 15.0 % Tips:"<<"\t\t\t\t$"<<tips;
        cout<< '\n'<<setw(4)<<"Plus Room Fee:"<<"\t\t\t\t\t$"<<room_fee;
        cout<< '\n'<<'\n'<<setw(4)<<"Total Bill:"<<"\t\t\t\t\t\t$"<<total_bill;
        cout<< '\n'<<setw(4)<<"Less Deposit:"<<"\t\t\t\t\t$"<<deposit;
        cout<< '\n'<<'\n'<<setw(4)<<"Balance Due:"<<"\t\t\t\t\t$"<<balance_due;
        cout<< '\n'<<'\n'<<setw(18)<<"Thank You For Using Passaic County Catering Services."<<endl;
        cout<< setfill('-')<<setw(80)<<"-"<<endl;
        }
    You could create an input and output function if you want to have the input and output in separated
    parts. It's your choice.

  4. #4
    Registered User
    Join Date
    Feb 2013
    Location
    Sweden
    Posts
    171
    Here's one idea using reference parameters of how you could make the input() and output() function at least.

    Code:
    #include <iomanip>
    #include <cmath>
    #include <iostream>
    #include <string>
    #include <fstream>
    const float TAX = .07, TIPS = .15, DESERT = 4.50, CHILD_RATE = .6;
     
     
     
     
    using namespace std;
     
    void output(string& name, float& cost_adult, float& room_fee, 
    			float& deposit, int& num_adults, int& num_children, int& num_desert); 
    void input();
     
    int main()
    {
    	input();
        return 0;
    }
    
    
    void input() {
        string name; 
        float cost_adult, room_fee, deposit;
        int num_adults, num_children, num_desert;
        
        //input section
        cout << "What is your name? : ";
        getline(cin,name);
        cout << "How many adults will be served? : "; cin >> num_adults;
        cout << "How many children will be served? : "; cin >> num_children;
        cout << "How many pieces of dessert will you need? : "; cin >> num_desert;
        cout << "What is the cost per adult meal? : "; cin >> cost_adult;
        cout << "What is your room fee? : "; cin >> room_fee;
        cout << "What is the amouunt of deposit paid when the reservation was made? : "; cin >> deposit;
        output(name, cost_adult, room_fee, deposit, num_adults, num_children, num_desert);
    }
    
    
    void output(string& name, float& cost_adult, float& room_fee, 
    			float& deposit, int& num_adults, int& num_children, int& num_desert)
        {
    
    
        
        //proccessing section
        float cost_child, total_cost_adult, total_cost_child, total_cost_desert, total_cost_food, taxes, total_bill, balance_due, tips;
        
        cost_child 			= CHILD_RATE * cost_adult;
        total_cost_adult 	= cost_adult * num_adults;
        total_cost_child 	= cost_child * num_children;
        total_cost_desert 	= DESERT * num_desert;
        total_cost_food  	= total_cost_adult + total_cost_child + total_cost_desert;
        taxes 				= total_cost_food * TAX;
        tips				= total_cost_food * TIPS;
        total_bill 			= total_cost_food + taxes + tips;
        balance_due 		= total_bill - deposit;
        
        cout<< setfill('-')<<setw(80)<<"-"<<endl;
        cout<< '\n'<<setw(18);
        cout<< "Passaic County Catering & Convention Services   Final Bill"<<endl;
        cout<< '\n'<<setw(4)<<"Customer: "<<"\t\t\t\t\t\t"<<name;
        cout<< '\n'<<setw(4)<<"Number of Adults:"<<"\t\t\t\t$"<<num_adults;
        cout<< '\n'<<setw(4)<<"Number of Children:"<<"\t\t\t\t$"<<num_children;
        cout<< '\n'<<'\n'<<setw(4)<<"Cost of Meal Per Adult:"<<"\t\t\t$"<<cost_adult;
        cout<< '\n'<<setw(4)<<"Cost Of Child's Meal Per Adult:"<<setprecision (2)<<fixed<<"\t$"<<cost_child;
        cout<< '\n'<<setw(4)<<"Total Cost for Desert:"<<"\t\t\t$"<<total_cost_desert;
        cout<< '\n'<<setw(4)<<"Total Food Cost:"<<"\t\t\t\t$"<<total_cost_food;
        cout<< '\n'<<'\n'<<setw(4)<<"Plus 7.5 % Taxes:"<<"\t\t\t\t$"<<taxes;
        cout<< '\n'<<setw(4)<<"Plus 15.0 % Tips:"<<"\t\t\t\t$"<<tips;
        cout<< '\n'<<setw(4)<<"Plus Room Fee:"<<"\t\t\t\t\t$"<<room_fee;
        cout<< '\n'<<'\n'<<setw(4)<<"Total Bill:"<<"\t\t\t\t\t\t$"<<total_bill;
        cout<< '\n'<<setw(4)<<"Less Deposit:"<<"\t\t\t\t\t$"<<deposit;
        cout<< '\n'<<'\n'<<setw(4)<<"Balance Due:"<<"\t\t\t\t\t$"<<balance_due;
        cout<< '\n'<<'\n'<<setw(18)<<"Thank You For Using Passaic County Catering Services."<<endl;
        cout<< setfill('-')<<setw(80)<<"-"<<endl;
        }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with my school project!!!
    By Maciek Grodzki in forum C Programming
    Replies: 5
    Last Post: 01-07-2014, 07:53 AM
  2. School Project Help
    By Joshua Hess in forum C++ Programming
    Replies: 9
    Last Post: 02-19-2013, 07:27 PM
  3. School project help
    By blckgoat in forum C Programming
    Replies: 8
    Last Post: 11-14-2011, 06:03 PM
  4. school project help
    By yogai in forum C Programming
    Replies: 8
    Last Post: 09-09-2004, 06:03 PM
  5. Help with School Project
    By jtouron in forum C++ Programming
    Replies: 2
    Last Post: 10-10-2003, 12:27 AM