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

In summary: for example, you have a variable called dis that is being multiplied by 100 but it should be divided by 100.
  • #1
nukeman
655
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
  • #2
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.
 
  • #3
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.
 
  • #4
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.
 
  • #5
Mark44 said:
Make that semicolon ;
This is a colon :

:smile: I always get those terms mixed up
 

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

What is the purpose of the code?

The purpose of the code is to perform a specific task or solve a problem using the C++ programming language.

Is the code efficient and optimized?

This can vary depending on the specific code and its intended purpose. However, some general indicators of efficiency and optimization include the use of appropriate data types, minimizing unnecessary loops or computations, and avoiding redundant code.

Are there any potential errors or bugs?

It is always important to thoroughly check for potential errors and bugs in any code. This can include testing for unexpected inputs, boundary cases, and logic errors. Additionally, using debugging tools or techniques can help identify and fix any issues.

Can the code be improved or simplified?

There are often multiple ways to approach a programming task, and it is common for code to be improved or simplified. This can include using more efficient algorithms, reducing the number of lines of code, or improving readability and organization.

Are there any best practices or coding standards being followed?

Following best practices and coding standards can help improve the overall quality and maintainability of code. This can include using descriptive variable and function names, proper indentation and formatting, and commenting code for clarity and documentation purposes.

Similar threads

Replies
3
Views
969
Replies
9
Views
2K
Replies
8
Views
2K
Replies
6
Views
3K
Replies
13
Views
2K
Replies
3
Views
2K
Replies
5
Views
4K
Back
Top