I need help to create C++ program for this question
Input the price of a product and the tax code (R or L).If the code is ‘R’ the tax rate is 8% and the rate is
12% for ‘L’. Calculate the total price and tax paid for the product. Continue processing until 0 is
encountered for price. Do error checks for all input data. Produce a summary table for all products.
Use the following input from your data file.
Sample input
100.25 R
350.5 L
450.3 L
500.5 L
-50 R
50.3 R
255.5 G
0

Recommended Answers

All 7 Replies

Please read this.

I worked on it and here is the code
could you please help

float
price;
double tax, total;

char code;

ins >> price;
 ins.open(in_file); 
 outs.open(out_file);

while (price>0.0 || price<0.0)
{ 
	outs << "Enter a code";
	ins >> code; 	// inside the loop because last line has only one value.
					
	if (price<0.0)
		outs << price << code << "Wrong price";
		outs << "Please enter a valid price";
		ins >> price;
	else if 
	{ 
		if((code != 'L') && (code != 'R'))
		{
			outs << price << code << "Error in code, code must be R or L";
			outs << "Please enter a valid code";
			ins >> code;
		}
		else if
		{	// processing the data
			if (code == 'L')
				tax = price * 0.12;
			else if (code == 'R')
				tax = price * 0.08;
			else 
				outs << "error in code" << endl; // if code in not 'R' or 'L'
												// do not calculate tax.
			
			if (price >= 0.0)
				total = price + tax;
				outs << "The total is: " << total << endl;
			
			else if
			outs << "price should be positive" << endl; // if price is not positive
														// do not calculate tax.
		} 
	}
}

I am guessing from what you have posted that you have never successfully run a C++ program at all. I say this because of the large amount of stuff in your program that is simply not part of C++.

It is one thing to show someone a program and say "This program almost works, but there is a problem that I just can't find." It is something else entirely to post something that isn't a C++ program and say "Please make this work for me." If you had done the first of these, I'd be glad to help you, but I'm not willing to substitute for your textbook. Sorry.

hi
I modified it but I have mistakes with if and else (wrong matching)
could you please help

#include <iostream>; // requires for keyboard and screen I/O
#include <iomanip>; // for setprecision
#include <fstream>; // requires for external file streams
#include <cmath>; // used for mathematical operations
#include <conio.h>; // for getch

// Associating program identifiers with external file names
#define in_file "data.txt" 
#define out_file "result.txt" 

using namespace std;
void main()
{
	ifstream ins; // associates ins as an input stream 
	ofstream outs; // associates outs as an output stream



float
price;
double tax, total;

char code;

ins >> price;
 ins.open(in_file); 
 outs.open(out_file);

while (price>0.0 || price<0.0)
{ 
	outs << "Enter a code";
	ins >> code; 	// inside the loop because last line has only one value.
					
	if (price<0.0)
		outs << price << code << "Wrong price";
		outs << "Please enter a valid price";
		ins >> price;
	
	else 
	{		if((code != 'L') && (code != 'R'))
			outs << price << code << "Error in code, code must be R or L";
			outs << "Please enter a valid code";
			ins >> code;
			else
			{	// processing the data
				if (code == 'L')
					// calculate tax
					tax = price * 0.12;
					outs << " The tax is " << tax << endl;
				else  (code == 'R')
					// calulate tax
					tax = price * 0.08;
					outs << " The tax is " << tax << endl;
				
			
// calcuate total
total = price + tax;
outs << "The total is: " << total << endl;
			}
			
	}
}
	
	
	ins.close();
	outs.close();
	getch (); // holding the screen
}

The braces are incorrectly laid out. Try using a better editor. It shows you where the braces are wrong and one more thing , when multiple statements have to be executed for a set of conditions , braces have to be there. You have not used them anywhere.

How about this:

#include <iostream>; // requires for keyboard and screen I/O
#include <iomanip>; // for setprecision
#include <fstream>; // requires for external file streams
#include <cmath>; // used for mathematical operations
#include <conio.h>; // for getch

// Associating program identifiers with external file names
#define in_file "data.txt" 
#define out_file "result.txt" 

using namespace std;
void main()
{
	ifstream ins; // associates ins as an input stream 
	ofstream outs; // associates outs as an output stream



float
price;
double tax_r, tax_l, total;

char code;

ins >> price;
 ins.open(in_file); 
 outs.open(out_file);

while (price>0.0 || price<0.0)
{ ins >> code; // inside the loop because last 
				//line has only one value.
	if (price<0.0)
		outs << price << code << "Wrong price";
	else 
	{if ((code != "L") && (code != "R"))
		outs << price << code << "Wrong code";
	else	// processing the data
		{
				if (code == 'L')
				{
					// calculate tax
					tax_l = price * 0.12;
					total = price + tax_l;
				}
				else  (code == 'R');
				{
					// calulate tax
					tax_r = price * 0.08;
					total = price + tax_r;
				}


	ins.close();
	outs.close();
	getch (); // holding the screen

		}
			
	}
}

}

How about getting your program to compile and execute, then posting the version you executed along with its output, and explaining what it's doing that you don't understand?

If you have never written a C++ program that has actually compiled successfully, then you shouldn't be trying to write one this big for your first effort. If, on the other hand, you have written a program that has compiled successfully, then you should be capable of getting this one to compile too.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.