Click here to Skip to main content
15,884,099 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Why total payment/ not show up?

What I have tried:

C++
#include<iostream>
#include<string.h>
using namespace std;
int main()
{
	char code,choice;
	int qty,TPrice,customer,sum,Tcost,payment,price;
	int calcBad ();
	int calcPing ();
	int calcPaint();
	int calcHockey();
	float calcMember();
	void printReceipt();

	cout<<"-----------------------------------------------------------------------"<<endl;
	cout<<"                        WELCOME TO SAN SPORTS                          "<<endl;
	cout<<"                        NO.TEL : 012-3456789                           "<<endl;
	cout<<" ADDRESS : LOT67, JALAN 9, KAMPUNG JAYA, 45600, BESTARI JAYA, SELANGOR"<<endl;
	cout<<"-----------------------------------------------------------------------"<<"\n"<<endl;
	cout<<"Do you want to buy sports equipment? [Y/N]"<<endl;
	cin>>choice;

	while(choice!='N')

	{
		cout<<"Choose Your Sport \n(B-Badminton, P-Pingpong, A-PaintBall, H-Hockey) : ";
		cin>>code;
		
		if (code == 'B')
		{
			price=calcBad();
		
		}
	
		else if (code == 'P')
		{
			price=calcPing();
			
		}
	
		else if (code == 'A')
		{
			price=calcPaint();
		
		}

		else if (code == 'H')
		{
			price=calcHockey();
		}
		
		cout<<"Quantity of equipment :";
		cin>>qty;
		TPrice =price*qty;
		cout<<"Total price: RM "<<TPrice<<"\n"<<endl;
		cout<<"Do you want to buy anything else ? [Y/N]"<<endl;
		cin>>choice;
		cout<<endl;
		
		if(choice=='Y')
		{ 
			sum=TPrice+TPrice;
		}
		else if(choice=='N')
		{
			sum=TPrice;
		}
		
	}
	cout<<"Total Purchase : RM"<<sum<<"\n"<<endl;		
	payment=calcMember();
	cout<<"Your Discount : RM "<<payment<<endl;
	Tcost =sum-payment;
	cout<<"Total Cost : RM "<<Tcost<<endl;
	printReceipt();
    return sum;
    
}

int calcBad ()
{
	char equip;
	int price;
	
	cout<<"Enter your equipment \n(1-Racket, 2-Shuttlecock, 3-Net) : ";
	cin>>equip;
		
	switch (equip)
	{		
		case '1' : cout<<"\nEquipment : Racket"<<endl;
				   price = 70.00;
				   break;
				
		case '2' : cout<<"\nEquipment : Shuttlecock"<<endl;
				   price = 32.00;
				   break;
		
		case '3' : cout<<"\nEquipment : Net"<<endl;
			   	   price = 28.00;
				   break;
		
	}
	return price;
}

int calcPing ()
{
	char equip;
	int price;
	
	cout<<"\nEnter your equipment \n(1-Table Tennis, 2-Pingpong Ball, 3-Racquet) : ";
	cin>>equip;
		
	switch (equip)
	{		
		case '1' : cout<<"\nEquipment : Table Tennis"<<endl;
					price = 84.00;
					break;
			
		case '2' : cout<<"\nEquipment : Pingpong Ball"<<endl;
			   	   price = 5.00;
				   break;
		
		case '3' : cout<<"\nEquipment : Racquet"<<endl;
				   price = 10.00;
			   	   break;
		
	}
	return price;
}

int calcPaint ()
{
	char equip;
	int price;
	
	cout<<"Enter your equipment: \n(1-Gun, 2-Mask, 3-Bullets) ";
	cin>>equip;
		
	switch (equip)
	{		
		case '1' : cout<<"\nEquipment : Gun"<<endl;
				   price = 75.00;
				   break;
			
		case '2' : cout<<"\nEquipment : Mask"<<endl;
				   price = 45.00;
				   break;
		
		case '3' : cout<<"\nEquipment : Bullets"<<endl;
				   price = 36.00;
				   break;
		
	}
	return price;
}

int calcHockey ()
{
	char equip;
	int price;
	
	cout<<"Enter your equipment: \n(1-Hockey Stick, 2-Hockey Ball, 3-Mouth Guard, 4-Helmet) ";
	cin>>equip;
		
	switch (equip)
	{		
		case '1' : cout<<"\nEquipment : Hockey Stick"<<endl;
				   price = 120.00;
				   break;
			
		case '2' : cout<<"\nEquipment : Hockey Ball"<<endl;
			   	   price = 39.00;
				   break;
		
		case '3' : cout<<"\nEquipment : Mouth Guard"<<endl;
				   price = 10.00;
				   break;
			
		case '4' : cout<<"Equipment : Helmet"<<endl;
				   price = 162.00;
				   break;
		
	}
	return price;
}

float calcMember ()
{
	char cat;
	float payment,TPrice;
	
	cout<<"Enter your category \n(R-Regular, M-Membership) : ";
	cin>>cat;
	
	if(cat == 'R')
		payment=TPrice*0.10;

	else if (cat == 'M')
		payment=TPrice*0.40;
		
	return payment;
}

void printReceipt()
{
	int payment,TPurc,qty,TPrice,Tcost;
	
	cout<<"***********************************************************************"<<endl;
	cout<<"                          WELCOME TO SAN SPORTS                        "<<endl;
	cout<<"                          NO.TEL : 012-3456789                         "<<endl;
	cout<<" ADDRESS : LOT67, JALAN 9, KAMPUNG JAYA, 45600, BESTARI JAYA, SELANGOR"<<endl;
	cout<<"***********************************************************************"<<"\n"<<endl;
	cout<<"Total:RM "<<TPrice<<endl;
	cout<<"Your Discount: RM"<<payment<<endl;
	cout<<"Your Payment: RM"<<Tcost<<endl;
	cout<<"Total Purchase: RM"<<TPurc<<"\n"<<endl;
	cout<<"************************"<<"THANK YOU FOR COMING"<<"***********************"<<endl;
}
Posted
Updated 14-Jul-22 4:03am
v2

Quote:
void printReceipt()
{
int payment,TPurc,qty,TPrice,Tcost; //<== UNINITIALISED variables
//...
}
The above function prints the receipt using local, uninitialised variables (i.e. garbage).
You should pass the actual values as arguments to the function.
 
Share this answer
 
v2
Comments
Patrice T 14-Jul-22 9:59am    
+5
CPallini 14-Jul-22 10:28am    
Thank you.
merano99 14-Jul-22 10:07am    
Since the formulation and translation of my answer took some time, you were faster. (Congratulation)
My suggestion (see below) would be to move the variables into a class instead of having to constantly pass them in. This would also help for other functions affected by the same problem, e.g. calcMember().
(+5)
CPallini 14-Jul-22 10:28am    
Thank you.
Quote:
Why total payment/ not show up?

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

1.11 — Debugging your program (stepping and breakpoints) | Learn C++[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 
Comments
CPallini 14-Jul-22 10:29am    
5.
Quote:
Why total payment/ not show up?

The real question was probably why the printReceipt() function doesn't output any meaningful values?

Let's see:
C++
void printReceipt()
{
int payment, TPurc, qty, TPrice, Tcost;

cout << "***********************************************************************" << endl;
cout << "                          WELCOME TO SAN SPORTS                        " << endl;
cout << "                          NO.TEL : 012-3456789                         " << endl;
cout << " ADDRESS : LOT67, JALAN 9, KAMPUNG JAYA, 45600, BESTARI JAYA, SELANGOR" << endl;
cout << "***********************************************************************" << "\n" << endl;
cout << "Total:RM " << TPrice << endl;
cout << "Your Discount: RM" << payment << endl;
cout << "Your Payment: RM" << Tcost << endl;
cout << "Total Purchase: RM" << TPurc << "\n" << endl;
cout << "************************" << "THANK YOU FOR COMING" << "***********************" << endl;
}

All variables are declared locally here and are therefore not uninitialized local variables. The compiler should also display this if you have configured it accordingly.

There are still some things we should address before a proposal for a solution would make sense. It is not common to declare functions locally in main(). I would recommend putting these either at the beginning of the source code, or in a separate header file.

There seems to be some problems with data types.

payment = calcMember();
warning C4244: "=": Conversion from "float" to "int", possible data loss

Since the variables payment and price obviously contain floating point numbers, it would make sense to declare them as float or double as well.

change to this:
C++
// int calcBad()
float calcBad()
{
   // int price;
   float price;
   ...
   price = 32.00;
   return price;
}

If the value 32.00 is actually to be stored in a variable without losing the decimal places, only a floating point number can be used.

To make the program a "real" C++ program, one solution would be to define all functions and data in one class.
It could look like this, for example:
C++
class Paiment {
public:
  float calcMember();
  // ...
  void printReceipt();
private:
  float m_payment, m_TPurc, m_TPrice, m_Tcost;
  int m_qty;
};

void Paiment::printReceipt()
{
...
}

The class-based variables can be identified with the addition m_ to avoid confusion. Now all local variables should be deleted and those already declared in the class should be used instead. Of course, at least one instance of the class should also be created in main().
 
Share this answer
 
Comments
Patrice T 14-Jul-22 10:30am    
+5
CPallini 14-Jul-22 10:30am    
5.Moving to a class makes really sense.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900