Comp Sci Can someone look over my C++ code - Simple short code

  • Thread starter Thread starter nukeman
  • Start date Start date
  • Tags Tags
    C++ Code Short
AI Thread Summary
The discussion centers on a request for help with a C++ program that calculates a sales slip based on user input for a regular price and potential discounts. Key issues identified in the code include incorrect capitalization of "cout," undeclared variables, and improper use of operators, which prevent the code from compiling. Additionally, there are concerns about variable naming conventions, suggesting that more descriptive names should be used for clarity. The division for calculating tax is also highlighted as problematic due to integer division, which affects the accuracy of the calculations. Overall, several syntax errors and logical issues need to be addressed for the code to function correctly.
nukeman
Messages
651
Reaction score
0

Homework Statement



My computer crashed, so I can't check my code I wrote, and the school linux lab is full, so I really need some help.

I already wrote the code (ill post it below) so I can't compile it and check for some errors. Can anyone quickly glance it over and see any mistakes?

The program will read a regular price from standard input and output a saleslip.

Here is the code:

//This program computes a regular price from standard input
//and outputs a saleslip

#include (isostream>
using namespace std;


int main()

{

const int HST_RATE = 12;
double priceItem, total, per, sp, hst, dis;
char sale;

cout << " " << endl;
cout << " This program calculates the total cost of some items " << endl;
cout << " Enter the Regular Price " ;
cin >> priceItem;
Cout << " Is this item on Sale (Y/N)? "
cin << sale;


if ( sale == 'y' or sale == 'Y')
{
cout << "Enter the percent discount: "
cin << per;
dis = (per/100) * price;
sp price - dis;
hs = (HST_RATE/100) * sp;
total = sp + hst;
cout << "Sale Slip" << endl,
cout << "===" << endl;
cout "Regular Price $: " << price << endl;
cout << "Discount $: " << dis << endl;
cout << " Sale Price $: " << so << endl;
cout << "HST $: " << hst << endl;
cout << "Total $: " << total << endl;
}


else
{

hst = (HST_RATE/100) * price;
total = price + hst;
cout << "Sale Slip" << endl;
cout << "===" << endl;
cout << "Regular Price $ " << price << endl;
cout << "HST $ << hst << endl;
cout << "Total $ " << total << endl;
}

cout << " " << endl;

return 0;

}

Homework Equations





The Attempt at a Solution

 
Physics news on Phys.org
Use (code) and (/code) tags (inside brackets, not parentheses) to preserve your formatting.
nukeman said:

Homework Statement



My computer crashed, so I can't check my code I wrote, and the school linux lab is full, so I really need some help.

I already wrote the code (ill post it below) so I can't compile it and check for some errors. Can anyone quickly glance it over and see any mistakes?

The program will read a regular price from standard input and output a saleslip.

Here is the code:

Code:
//This program computes a regular price from standard input
//and outputs a saleslip

#include (isostream>
using namespace std;


int main()

{

	const int HST_RATE = 12;
	double priceItem, total, per, sp, hst, dis;
	char sale;

	cout << " " << endl;
	cout << " This program calculates the total cost of some items " << endl;
	cout << " Enter the Regular Price   " ;
        cin >> priceItem;
	Cout << " Is this item on Sale (Y/N)? "
	cin << sale;


	if ( sale == 'y' or sale == 'Y')
	{
	cout << "Enter the percent discount:  "
	cin << per;
	dis = (per/100) * price;
	sp  price - dis;
	hs  = (HST_RATE/100) * sp;
	total = sp + hst;
	cout << "Sale Slip" << endl,
	cout << "===" << endl;
	cout "Regular Price   $: " << price << endl;
	cout << "Discount     $: " << dis << endl;
	cout << " Sale Price  $: " << so << endl;
	cout << "HST          $: " << hst << endl;
	cout << "Total        $: " << total << endl;
	}

	
	else
	{
	
	hst = (HST_RATE/100) * price;
	total = price + hst;
	cout << "Sale Slip" << endl;
	cout << "===" << endl;
	cout << "Regular Price $ " << price << endl;
	cout << "HST 	$ << hst << endl;
	cout << "Total  $ " << total << endl;
	}

	cout << " " << endl;

	return 0;

}
There are several things wrong that will keep this from compiling.
1) You have Cout where it should be cout.
2) hs is undeclared. The variable you have is hst.

When you get those fixed, there is at least one other problem. (I have only scanned your code, so there might be more.)
HST_RATE/100 == 0, because both are integer constants. Have you learned that there are two kinds of division in C (and in C++, C#, etc.)? Because HST_RATE/100 isn't what you thought, some tax is going to be computed incorrectly.

Other comment
Some of your variables have terrible names: HST_RATE, per, sp, hst, dis. What do these mean? You should give your variables meaningful names so that someone else looking at your code has some idea of what they're supposed to represent. Your instructor might deduct points from your code for this. I would.
 
Some errors I've noticed:
- To call the iostream library, you should use <iostream>
- Many of the lines are missing a colon at the end and some have a comma instead
- You've messed up some of the operators. Remember, it's cout << and cin >>
- Some of the lines are meaningless, like "sp price - dis;" (Did you miss an equal sign?)

Mark44 said:
Some of your variables have terrible names: HST_RATE, per, sp, hst, dis. What do these mean? You should give your variables meaningful names so that someone else looking at your code has some idea of what they're supposed to represent. Your instructor might deduct points from your code for this. I would.

In some Canadian provinces there is the Harmonized Sales Tax that combines federal and provincial sales taxes, and is commonly known as the HST. So I'd say HST_RATE and hst are fine, and the others should be changed.
 
Jokerhelper said:
Some errors I've noticed:
- To call the iostream library, you should use <iostream>
The OP has (isostream>, which makes two errors.
Jokerhelper said:
- Many of the lines are missing a colon at the end and some have a comma instead
Make that semicolon ;
This is a colon :
Jokerhelper said:
- You've messed up some of the operators. Remember, it's cout << and cin >>
- Some of the lines are meaningless, like "sp price - dis;" (Did you miss an equal sign?)



In some Canadian provinces there is the Harmonized Sales Tax that combines federal and provincial sales taxes, and is commonly known as the HST. So I'd say HST_RATE and hst are fine, and the others should be changed.
 
Mark44 said:
Make that semicolon ;
This is a colon :

:smile: I always get those terms mixed up
 

Similar threads

Replies
3
Views
1K
Replies
9
Views
2K
Replies
6
Views
3K
Replies
13
Views
2K
Replies
3
Views
2K
Back
Top