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

  • Context: Comp Sci 
  • Thread starter Thread starter nukeman
  • Start date Start date
  • Tags Tags
    C++ Code Short
Click For Summary

Discussion Overview

The discussion revolves around a request for assistance with a C++ code snippet intended to read a price and generate a sales slip. Participants are examining the code for errors and providing feedback on various aspects of its implementation.

Discussion Character

  • Homework-related
  • Technical explanation
  • Debate/contested

Main Points Raised

  • Some participants note that the inclusion of the iostream library is incorrect, suggesting it should be instead of (isostream>.
  • There are multiple comments regarding missing semicolons at the end of lines, with some lines incorrectly ending with commas.
  • Participants point out that there are errors in the use of operators, specifically that it should be cout << and cin >> instead of count << and cin <.
  • Concerns are raised about the variable names used in the code, with suggestions that they should be more descriptive to improve readability.
  • One participant mentions that the division of HST_RATE by 100 may lead to incorrect tax calculations due to integer division.
  • Some participants express uncertainty about the appropriateness of certain variable names, particularly HST_RATE and hst, while others defend their use based on regional tax practices.

Areas of Agreement / Disagreement

Participants generally agree that there are several errors in the code that need to be addressed, but there is no consensus on the best approach to naming variables or the implications of the HST_RATE.

Contextual Notes

Limitations include potential misunderstandings of C++ syntax, the impact of integer division on calculations, and the subjective nature of variable naming conventions.

Who May Find This Useful

Students learning C++ programming, particularly those working on homework assignments involving input/output operations and basic arithmetic calculations.

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;

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


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


else
{

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

count << " " << 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 count << 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 count << 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 2 ·
Replies
2
Views
2K
  • · Replies 9 ·
Replies
9
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 13 ·
Replies
13
Views
2K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 7 ·
Replies
7
Views
7K
Replies
10
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 6 ·
Replies
6
Views
3K