CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    May 2015
    Posts
    3

    Help Pleasse! Beginning C++ program (can't debug)

    Objectives
    To learn to code, compile and run a program containing three or more functions. This is a catering company.
    Assignment
    Plan and code a program utilizing one file for input and one file for output to solve the following problem:
    A. For adults, the deluxe meals will cost $25.80 per person and the standard meals will cost $21.75 per person, dessert included. Children's meals will cost 60 percent of adult meals. Everyone within a given party must be served the same meal type.
    B. All customers will be charged the same rate for tip and tax, currently 18 percent (applied only to the cost of the food).
    C. A surcharge, currently 7 percent, is added to the total bill if the catering is to be done on the weekend (Friday, Saturday, or Sunday).
    D. To induce customers to pay promptly, a discount is offered if payment is made within ten days.
    E. This discount depends on the amount of the total bill. If the bill is less than $100.00, the discount is 1.5 percent; if the bill is at least $100.00 but less than $400.00, the discount is 2.5 percent; if the bill is $400.00 or more, the discount is 3.5 percent.
    Input
    Number of adults, number of children, meal type (D, S) character indicating whether or not date is a weekend (Y/N), amount of any deposit. Create the data file below in text editor or Notepad.
    Output
    Output an itemized bill listing the number of adults, children, cost for adult meals, cost for children's meals, total food cost, surcharge (if appropriate), tax and tip, total cost of the party, deposit (if any), total balance due, amount of discount if bill is paid within ten days. Output error data to a separate error file.

    It does what it asks but my teacher said it doesn't work. I think the problems is my functions but I don't know how to fix it since I don't know the problem. Help please.
    lab6.cpp

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: Help Pleasse! Beginning C++ program (can't debug)

    Well in order to be able to provide some guidance, you'll need to post your code so that we can have a look at it.

    When posting code, please use code tags. Go Advanced, select the pasted formatted code and click '#'.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #3
    Join Date
    Jul 2013
    Posts
    576

    Re: Help Pleasse! Beginning C++ program (can't debug)

    Quote Originally Posted by asal_2 View Post
    It does what it asks but my teacher said it doesn't work.
    In an educational situation "working" may not be good enougth. Maybe the code should be of a certain quality reflecting what you have been taught.

    Why not ask the teacher to be more specific about what's the problem with your effort.

  4. #4
    Join Date
    May 2015
    Posts
    3

    Re: Help Pleasse! Beginning C++ program (can't debug)

    So this is what I've written so far. And thanks i didn't know how to do this so i made an attachment the first time.
    So it works fine until it get to the errors which it does send to the error file but then it doesn't actually do the ones that are correct. Therefore i think the Booleans aren't resetting but i don't know how to make them reset after each row.
    Code:
    #include <iomanip>
    #include <iostream>
    #include <fstream>
    #include <string>
    using namespace std;
    
    const double dmeal = 25.80;
    const double smeal = 21.75;
    const double tax = 0.18;
    const double sur = 0.07;
    
    void ReadData(int &NumAdults, int &NumKids, char &MealType, char &Weekend, double &Deposit, bool &ANumError, bool &KNumError, bool &MealError, bool &WeekendError, bool &DepositError);
    void CalcSubTotal(int NumAdults, int NumKids, char MealType, double &SubTotal, double &TaxTip, double &Acost, double &Kcost, double &Total, double tax, double smeal, double dmeal);
    void CalcSurcharge(char Weekend, double TaxTip, double &Surcharge, double Total, double &Balance, double Deposit, double sur, double &PostTotal);
    double Calcdiscount(double &PostTotal, double &Discount);
    void Output(int NumAdults, int NumKids, double Acost, double Kcost, double SubTotal, double TaxTip, double Surcharge, double PostTotal, double Deposit, double Balance, double Discount);
    
    ifstream infile;
    ofstream outfile;
    ofstream errorfile;
    
    int main()
    {
    
    	int NumAdults, NumKids;
    	double Acost, Kcost, SubTotal, Deposit, TaxTip, Surcharge, Total, Discount, Balance, PostTotal;
    	char MealType, Weekend;
    	bool ANumError = 0, KNumError = 0, MealError = 0, WeekendError = 0, DepositError = 0;
    
    	Acost = 0; Kcost = 0; SubTotal = 0; Deposit = 0; TaxTip = 0; Surcharge = 0; Total = 0; Discount = 0; Balance = 0; PostTotal = 0;
    
    
    	infile.open("C:\\Users\\Ale\\Desktop\\InFile.txt");                        //"C:\\Users\\student\\Desktop\\InFile.txt"
    	if (!infile)
    	{
    		cout << "File can not be open." << endl;
    		system("pause");
    		exit(1);
    	}
    
    
    	outfile.open("C:\\Users\\Ale\\Desktop\\OutFile.txt");                    //"C:\\Users\\student\\Desktop\\OutFile.txt"
    	errorfile.open("C:\\Users\\Ale\\Desktop\\ErrorFile.txt");            //"C:\\Users\\student\\Desktop\\ErrorFile.txt"
    
    	while (!infile.eof())
    	{
    		ReadData(NumAdults, NumKids, MealType, Weekend, Deposit, ANumError, KNumError, MealError, WeekendError, DepositError);
    
    		if ((ANumError == 0) && (KNumError == 0) && (MealError == 0) && (WeekendError == 0) && (DepositError == 0))
    		{
    			CalcSubTotal(NumAdults, NumKids, MealType, SubTotal, TaxTip, Acost, Kcost, Total, tax, smeal, dmeal);
    			CalcSurcharge(Weekend, TaxTip, Surcharge, Total, Balance, Deposit, sur, PostTotal);
    			Calcdiscount(PostTotal, Discount);
    			Output(NumAdults, NumKids, Acost, Kcost, SubTotal, TaxTip, Surcharge, PostTotal, Deposit, Balance, Discount);
    		}
    
    
    	}
    
    
    	infile.close();
    	outfile.close();
    	errorfile.close();
    	return 0;
    
    }
    
    void ReadData(int &NumAdults, int &NumKids, char &MealType, char &Weekend, double &Deposit, bool &ANumError, bool &KNumError, bool &MealError, bool &WeekendError, bool &DepositError)
    {
    	infile >> NumAdults >> NumKids >> MealType >> Weekend >> Deposit;
    
    	if (NumAdults < 0)
    	{
    		errorfile << "Number of adults can't be negative." << endl << NumAdults << " " << NumKids << " " << MealType << " " << Weekend << " " << Deposit << endl;
    		ANumError = 1;
    	}
    	if (NumKids < 0)
    	{
    		errorfile << "Number of children cant be negative." << endl << NumAdults << " " << NumKids << " " << MealType << " " << Weekend << " " << Deposit << endl;
    		KNumError = 1;
    	}
    	if ((MealType != 'S') && (MealType != 'D'))
    	{
    		errorfile << "Invalid character for meal type." << endl << NumAdults << " " << NumKids << " " << MealType << " " << Weekend << " " << Deposit << endl;
    		MealError = 1;
    	}
    	if ((Weekend != 'N') && (Weekend != 'Y'))
    	{
    		errorfile << "Invalid character for weekend." << endl << NumAdults << " " << NumKids << " " << MealType << " " << Weekend << " " << Deposit << endl;
    		WeekendError = 1;
    	}
    	if (Deposit < 0)
    	{
    		errorfile << "Deposit can't be negative." << endl << NumAdults << " " << NumKids << " " << MealType << " " << Weekend << " " << Deposit << endl;
    		DepositError = 1;
    	}
    }
    
    void CalcSubTotal(int NumAdults, int NumKids, char MealType, double &SubTotal, double &TaxTip, double &Acost, double &Kcost, double &Total, double tax, double smeal, double dmeal)
    {
    	if (MealType == 'S')
    	{
    		Acost = smeal * NumAdults;
    		Kcost = (smeal * 0.60) * NumKids;
    
    	}
    	if (MealType == 'D')
    	{
    		Acost = dmeal * NumAdults;
    		Kcost = (dmeal * 0.60) * NumKids;
    
    	}
    	SubTotal = Acost + Kcost;
    	TaxTip = SubTotal * tax;
    	Total = SubTotal + TaxTip;
    }//calcSubTotal
    
    void CalcSurcharge(char Weekend, double TaxTip, double &Surcharge, double Total, double &Balance, double Deposit, double sur, double &PostTotal)
    {
    	if (Weekend == 'Y')
    	{
    		Surcharge = Total * sur;
    		PostTotal = Total + Surcharge;
    	}
    	else
    	{
    		Surcharge = 0;
    		PostTotal = Total + Surcharge;
    	}
    	Balance = PostTotal - Deposit;
    }//calcSurcharge
    
    double Calcdiscount(double &PostTotal, double &Discount)
    {
    	if (PostTotal >= 400.00)
    	{
    		Discount = PostTotal * 0.035;
    	}
    	else if ((PostTotal < 400.00) && (PostTotal >= 100.00))
    	{
    		Discount = PostTotal * 0.025;
    	}
    	else if (PostTotal < 100.00)
    	{
    		Discount = PostTotal * 0.015;
    	}
    	return Discount;
    }//calcdiscount
    
    
    
    void Output(int NumAdults, int NumKids, double Acost, double Kcost, double SubTotal, double TaxTip, double Surcharge, double PostTotal, double Deposit, double Balance, double Discount)
    {
    	outfile << fixed << showpoint << setprecision(2) << setw(10);
    	outfile << "Number of Adults: \t" << setw(10) << NumAdults << endl;
    	outfile << "Number of Children: \t" << setw(10) << NumKids << endl;
    	outfile << "Cost of Adult Meal: \t" << setw(10) << Acost << endl;
    	outfile << "Cost of Child Meal: \t" << setw(10) << Kcost << endl;
    	outfile << "Total Food Charge: \t" << setw(10) << SubTotal << endl;
    	outfile << "Tax and Tip: \t\t" << setw(10) << TaxTip << endl;
    	outfile << "Surcharge: \t\t" << setw(10) << Surcharge << endl;
    	outfile << "Total Party Cost: \t" << setw(10) << PostTotal << endl;
    	outfile << "Deposit: \t\t" << setw(10) << Deposit << endl;
    	outfile << "Total Balance Due: \t" << setw(10) << Balance << endl;
    	outfile << "Discount: \t\t" << setw(10) << Discount << endl
    		<< "Get these savings by paying the bill within 10 days." << endl << "Don't miss out on this chance to save." << endl << endl;
    }

  5. #5
    Join Date
    May 2015
    Posts
    3

    Re: Help Pleasse! Beginning C++ program (can't debug)

    This is the data file info given. Sorry i thought it might be helpful.
    10 0 S Y 100.00
    27 3 D Y 57.50
    125 17 D N 0.00
    4 0 S N 25.00
    0 25 S Y 23.75
    250 43 D N 500.00
    0 0 D N 0.0
    10 0 R Y 10.00
    17 3 D R 15.00
    5 0 D Y 275.00
    -3 10 D Y 20.00
    14 -1 S N 30.00
    20 3 D Y -10.00

  6. #6
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: Help Pleasse! Beginning C++ program (can't debug)

    You should post the code rather than providing it as an attachment. Like this

    Code:
    #include <iomanip>
    #include <iostream>
    #include <fstream>
    #include <string>
    using namespace std;
    
    const double dmeal = 25.80;
    const double smeal = 21.75;
    const double tax = 0.18;
    const double sur = 0.07;
    
    void ReadData(int &NumAdults, int &NumKids, char &MealType, char &Weekend, double &Deposit, bool &ANumError, bool &KNumError, bool &MealError, bool &WeekendError, bool &DepositError);
    void CalcSubTotal(int NumAdults, int NumKids, char MealType, double &SubTotal, double &TaxTip, double &Acost, double &Kcost, double &Total, double tax, double smeal, double dmeal);
    void CalcSurcharge(char Weekend, double TaxTip, double &Surcharge, double Total, double &Balance, double Deposit, double sur, double &PostTotal);
    void Calcdiscount(double &PostTotal, double &Discount);
    void Output(int NumAdults, int NumKids, double Acost, double Kcost, double SubTotal, double TaxTip, double Surcharge, double PostTotal, double Deposit, double Balance, double Discount);
    
    ifstream infile;
    ofstream outfile;
    ofstream errorfile;
    
    int main()
    {
    
    	int NumAdults, NumKids;
    	double Acost, Kcost, SubTotal, Deposit, TaxTip, Surcharge, Total, Discount, Balance, PostTotal;
    	char MealType, Weekend;
    	bool ANumError = 0, KNumError = 0, MealError = 0, WeekendError = 0, DepositError = 0;
    
    	Acost = 0; Kcost = 0; SubTotal = 0; Deposit = 0; TaxTip = 0; Surcharge = 0; Total = 0; Discount = 0; Balance = 0; PostTotal = 0;
    
    
    	infile.open("C:\\Users\\Ale\\Desktop\\InFile.txt");                        //"C:\\Users\\student\\Desktop\\InFile.txt"
    	if (!infile)
    	{
    		cout << "File can not be open." << endl;
    		system("pause");
    		exit(1);
    	}
    
    
    	outfile.open("C:\\Users\\Ale\\Desktop\\OutFile.txt");                    //"C:\\Users\\student\\Desktop\\OutFile.txt"
    	errorfile.open("C:\\Users\\Ale\\Desktop\\ErrorFile.txt");            //"C:\\Users\\student\\Desktop\\ErrorFile.txt"
    
    	while (!infile.eof())
    	{
    		ReadData(NumAdults, NumKids, MealType, Weekend, Deposit, ANumError, KNumError, MealError, WeekendError, DepositError);
    
    		if ((ANumError == 0) && (KNumError == 0) && (MealError == 0) && (WeekendError == 0) && (DepositError == 0))
    		{
    			CalcSubTotal(NumAdults, NumKids, MealType, SubTotal, TaxTip, Acost, Kcost, Total, tax, smeal, dmeal);
    			CalcSurcharge(Weekend, TaxTip, Surcharge, Total, Balance, Deposit, sur, PostTotal);
    			Calcdiscount(PostTotal, Discount);
    			Output(NumAdults, NumKids, Acost, Kcost, SubTotal, TaxTip, Surcharge, PostTotal, Deposit, Balance, Discount);
    		}
    
    
    	}
    
    
    	infile.close();
    	outfile.close();
    	errorfile.close();
    	return 0;
    
    }
    
    void ReadData(int &NumAdults, int &NumKids, char &MealType, char &Weekend, double &Deposit, bool &ANumError, bool &KNumError, bool &MealError, bool &WeekendError, bool &DepositError)
    {
    	infile >> NumAdults >> NumKids >> MealType >> Weekend >> Deposit;
    
    	if (NumAdults < 0)
    	{
    		errorfile << "Number of adults can't be negative." << endl << NumAdults << " " << NumKids << " " << MealType << " " << Weekend << " " << Deposit << endl;
    		ANumError = 1;
    	}
    	if (NumKids < 0)
    	{
    		errorfile << "Number of children cant be negative." << endl << NumAdults << " " << NumKids << " " << MealType << " " << Weekend << " " << Deposit << endl;
    		KNumError = 1;
    	}
    	if ((MealType != 'S') && (MealType != 'D'))
    	{
    		errorfile << "Invalid character for meal type." << endl << NumAdults << " " << NumKids << " " << MealType << " " << Weekend << " " << Deposit << endl;
    		MealError = 1;
    	}
    	if ((Weekend != 'N') && (Weekend != 'Y'))
    	{
    		errorfile << "Invalid character for weekend." << endl << NumAdults << " " << NumKids << " " << MealType << " " << Weekend << " " << Deposit << endl;
    		WeekendError = 1;
    	}
    	if (Deposit < 0)
    	{
    		errorfile << "Deposit can't be negative." << endl << NumAdults << " " << NumKids << " " << MealType << " " << Weekend << " " << Deposit << endl;
    		DepositError = 1;
    	}
    }
    
    void CalcSubTotal(int NumAdults, int NumKids, char MealType, double &SubTotal, double &TaxTip, double &Acost, double &Kcost, double &Total, double tax, double smeal, double dmeal)
    {
    	if (MealType == 'S')
    	{
    		Acost = smeal * NumAdults;
    		Kcost = (smeal * 0.60) * NumKids;
    
    	}
    	if (MealType == 'D')
    	{
    		Acost = dmeal * NumAdults;
    		Kcost = (dmeal * 0.60) * NumKids;
    
    	}
    	SubTotal = Acost + Kcost;
    	TaxTip = SubTotal * tax;
    	Total = SubTotal + TaxTip;
    }//calcSubTotal
    
    void CalcSurcharge(char Weekend, double TaxTip, double &Surcharge, double Total, double &Balance, double Deposit, double sur, double &PostTotal)
    {
    	if (Weekend == 'Y')
    	{
    		Surcharge = Total * sur;
    		PostTotal = Total + Surcharge;
    	}
    	else
    	{
    		Surcharge = 0;
    		PostTotal = Total + Surcharge;
    	}
    	Balance = PostTotal - Deposit;
    }//calcSurcharge
    
    void Calcdiscount(double &PostTotal, double &Discount)
    {
    	if (PostTotal >= 400.00)
    	{
    		Discount = PostTotal * 0.035;
    	}
    	else if ((PostTotal < 400.00) && (PostTotal >= 100.00))
    	{
    		Discount = PostTotal * 0.025;
    	}
    	else if (PostTotal < 100.00)
    	{
    		Discount = PostTotal * 0.015;
    	}
    }//calcdiscount
    
    
    
    void Output(int NumAdults, int NumKids, double Acost, double Kcost, double SubTotal, double TaxTip, double Surcharge, double PostTotal, double Deposit, double Balance, double Discount)
    {
    	outfile << fixed << showpoint << setprecision(2) << setw(10);
    	outfile << "Number of Adults: \t" << setw(10) << NumAdults << endl;
    	outfile << "Number of Children: \t" << setw(10) << NumKids << endl;
    	outfile << "Cost of Adult Meal: \t" << setw(10) << Acost << endl;
    	outfile << "Cost of Child Meal: \t" << setw(10) << Kcost << endl;
    	outfile << "Total Food Charge: \t" << setw(10) << SubTotal << endl;
    	outfile << "Tax and Tip: \t\t" << setw(10) << TaxTip << endl;
    	outfile << "Surcharge: \t\t" << setw(10) << Surcharge << endl;
    	outfile << "Total Party Cost: \t" << setw(10) << PostTotal << endl;
    	outfile << "Deposit: \t\t" << setw(10) << Deposit << endl;
    	outfile << "Total Balance Due: \t" << setw(10) << Balance << endl;
    	outfile << "Discount: \t\t" << setw(10) << Discount << endl
    		<< "Get these savings by paying the bill within 10 days." << endl << "Don't miss out on this chance to save." << endl << endl;
    }
    Last edited by 2kaud; May 30th, 2015 at 04:30 AM.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  7. #7
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: Help Pleasse! Beginning C++ program (can't debug)

    It does what it asks but my teacher said it doesn't work.
    The program either works and produces the correct output from the given input - or it does not. If the output produced is not correct as expected then the program does not work and has a fault - which you will need to investigate and correct. If the program does produce the expected output then it 'works' and the teacher cannot say that it doesn't. The teacher may be critical of your program design and code but cannot say it doesn't work if the output is correct.

    You need to establish the issue. Either the program does not work or it does and the design/code is being criticised.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured