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
SUMMARY

The forum discussion centers around a C++ program designed to read a regular price from standard input and output a sales slip. Key issues identified include incorrect syntax such as using "Cout" instead of "cout", missing semicolons, and undeclared variables like "hs". Additionally, the division of integer constants leads to incorrect tax calculations. The discussion emphasizes the importance of meaningful variable names and proper syntax for successful compilation.

PREREQUISITES
  • Understanding of C++ syntax and structure
  • Familiarity with standard input/output operations in C++
  • Knowledge of variable declaration and initialization
  • Basic understanding of tax calculations in programming
NEXT STEPS
  • Learn about C++ variable naming conventions and best practices
  • Study C++ input/output stream operations using
  • Explore the differences between integer and floating-point division in C++
  • Review common C++ compilation errors and debugging techniques
USEFUL FOR

C++ beginners, students learning programming, and anyone looking to improve their coding practices and debugging skills.

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
2K
  • · 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