I am pulling a list of 3 items with a list price from a text file, I ask for the percentage off and calculate it against the list price then I update to file with revised price. The program compiles correctly, but it never displays the info in the console after I input the percent and hit enter. Data does flash onscreen and the text file gets created but the numbers are not correct. I tried using setprecision but it does not go two decimal places to the left as instructed.
an exampe of the data on file is like
goods 22.67
goods 24.00
Any knowledge would be appreciated.

#include "stdafx.h"

#include <iostream>

#include <iomanip>

#include <fstream>
using namespace std;

int  main()
{
	double percent, appleP, coffeeP, teaP, coffeeN, appleN, teaN;
	char Apples, coffee, tea;

	
	// Open file that has the list prices
	ifstream inputFile;
	inputFile.open("productlistIn.txt");

	//Close file with new price
	ofstream outputFile;
	outputFile.open("updatedproductlistIn.txt");

	//Read list prices from file
	//store them in the variables.
	inputFile >> appleP;
	inputFile >> teaP;
	inputFile >> coffeeP;

	//Get percentage from user.
	cout << "Please enter the amount to increase the prioe" << endl;
	cin >>percent;
	cin.ignore( );

	//Calculation for percentage increase
	coffeeN = coffeeP * percent + coffeeP;
	appleN = appleP * percent + appleP;
	teaN = teaP * percent + teaP;

	//Display the list price and new price
	cout << "Apples "<< appleP << "new price "<< setprecision(2) <<fixed << appleN << endl;
	cout << "coffee "<< coffeeP << "new price"<< setprecision(2) <<fixed << coffeeN << endl;
	cout << "tea "<< teaP << "new price"<< setprecision(2) <<fixed << fixed <<teaN << endl;


	//Write updated price to file.
	outputFile << "Apples " << appleP << "new price "<< appleN << endl;
	outputFile << "coffee "<< coffeeP << "new price"<< coffeeN << endl;
	outputFile << "tea " << teaP << "new price" << teaN << endl;

	// Close the file
	outputFile.close();
	cout << "Done.\n";
	return 0;

Recommended Answers

All 19 Replies

What does the layout of the file look like, for instance are:

inputFile >> appleP;
inputFile >> teaP;
inputFile >> coffeeP;

appleP, teaP and coffeeP all separated by space or a newline? I suspect if they are laid out like the 2 line sample you gave you are trying to read the "goods" into your price variables.

As far as the setprecision goes, I don't see anything explicitly wrong in your code, so I'm not sure.

Put a cin.get() in between lines 53 and 54 so that you get a "pause" effect so you can check out what's on screen. If necessary you can output what you've just input to check it to make sure (I know you think the problem lies in the output but it couldn't hurt).

the updated file ends up looking like
Apples -9.25596e+061new price -1.20328e+062
coffee -9.25596e+061new price-1.20328e+062
tea -9.25596e+061new price-120327520754113190000000000000000000000000000000000000000000000.00
instead of
Apples space (list price) space new 00.00
Tea space (list price) space new 00.00
the initial list is in the same format with only the list price

Right so it's exactly like what I said. Create some strings to throw away (or just use 1 if you really don't need the labels at all).

string applelabel,tealabel,coffeelabel;
//or just string label;
inputFile >> applelabel;   //or inputFile >> label;
inputFile >> appleP;
inputFile >>tealabel;      //or inputFile >> label;
inputFile >> teaP;
inputFile >> coffeelabel;   //or inputFile >> label;
inputFile >> coffeeP;

That way everything's happening in the proper order.

my list price is
Apples 2.23
Coffee 1.23
Tea 4.98
the numbers that display for the percentage are all the same which is not correct. I set it up to
//Calculation for percentage increase
coffeeN = coffeeP * percent + coffeeP;
appleN = appleP * percent + appleP;
teaN = teaP * percent + teaP;
which is
new price = list price * percent plus list price\\ should multiply the percent times the list price plus the list price, this should equal the new price

just got your update will try with string, but will that fix the math from coming up the same price.

If you were reading strings into your numeric variables before then it should improve things a bit. One way to find out...

Thanks for the suggestion, after working on it I got it to calculate correctly, but something is wrong when I enter the number in
it should prompt to insert another number if the percent is greater than .5 . It prompts me twice and calculates the numbers regardless what number I input.

#include "stdafx.h"

#include <iostream>

#include <iomanip>

#include <fstream>
using namespace std;

int main()
{
	double percent, appleP, coffeeP, teaP, coffeeN, appleN, teaN;
	char label[7];// For labels of products on file.
	
	// Open file that has the list prices
	ifstream inputFile;
	inputFile.open("productlistIn.txt");

	//Close file with new price
	ofstream outputFile;
	outputFile.open("updatedproductlistIn.txt");

	//Read list prices from file
	//store them in the variables.
	inputFile >> label;
	inputFile >> appleP;
	inputFile >> label;
	inputFile >> coffeeP;
	inputFile >> label;
	inputFile >> teaP;


	//Get percentage from user.
	cout << "Please enter the amount to increase the prioe" << endl;
	cin >>percent;
	cin.ignore( );

	// Is the percent greater than .5?
	     if ( percent > .5 )  // If greater than .5
    cout << "Please enter an amount that is .5 or less" << endl;
    cin >> percent;
	

	//Calculation for percentage increase
	appleN = appleP * percent + appleP;
	coffeeN = coffeeP * percent + coffeeP;
	teaN = teaP * percent + teaP;

	//Display the list price and new price
	cout << "Apples "<< appleP << " new price " << setprecision(2) << fixed << appleN << endl;
	cout << "coffee " << coffeeP << " new price " << setprecision(2) << fixed << coffeeN << endl;
	cout << "tea    " << teaP << " new price " << setprecision(2) << fixed <<teaN << endl;
	
	//Write updated price to file.
	outputFile << "Apples " << appleP << " new price " << setprecision(2) << fixed << appleN << endl;
	outputFile << "coffee " << coffeeP << " new price " << setprecision(2) << fixed << coffeeN << endl;
	outputFile << "tea    " << teaP << " new price " << setprecision(2) << fixed <<teaN << endl;


	// Close the file
	outputFile.close();
	cout << "Done.\n";
	return 0;

put that section of the code into a do/while loop

do{
      cout<<"Please enter a percent(<.5): "; //you're meaning for 50% right?
      cin >> percent;
      if(percent > 0.5)
               cout << "Invalid.  Re-enter\n";  
       //you could have a break statement within this if 
       //statement but I think it's tidier as is
}while(percent >.5);

I couldnt get the do while to work, so I tried a if else and got the error
illegal else without the matching if error when compiling line 55

#include "stdafx.h"

#include <iostream>

#include <iomanip>

#include <fstream>
using namespace std;

int main()
{
	double percent, appleP, coffeeP, teaP, coffeeN, appleN, teaN;
	char label[7];// For labels of products on file.
	
	// Open file that has the list prices
	ifstream inputFile;
	inputFile.open("productlistIn.txt");

	//Close file with new price
	ofstream outputFile;
	outputFile.open("updatedproductlistIn.txt");

	//Read list prices from file
	//store them in the variables.
	inputFile >> label;
	inputFile >> appleP;
	inputFile >> label;
	inputFile >> coffeeP;
	inputFile >> label;
	inputFile >> teaP;


	//Get percentage from user.
	cout << "Please enter the amount to increase the prioe" << endl;
	cin >> percent;
	cin.ignore( );

	//Was percent greater than .5?
	if (percent > .5)

		cout << "Please enter a percent lesser than .5." << endl;
		cin >> percent;
	
	else
	{
	//Display the list price and new price
	cout << "Apples "<< appleP << " new price " << setprecision(2) << fixed << appleN << endl;
	cout << "coffee " << coffeeP << " new price " << setprecision(2) << fixed << coffeeN << endl;
	cout << "tea    " << teaP << " new price " << setprecision(2) << fixed <<teaN << endl;
	
	}
	//Calculation for percentage increase
	appleN = appleP * percent + appleP;
	coffeeN = coffeeP * percent + coffeeP;
	teaN = teaP * percent + teaP;

		
	//Write updated price to file.
	outputFile << "Apples " << appleP << " new price " << setprecision(2) << fixed << appleN << endl;
	outputFile << "coffee " << coffeeP << " new price " << setprecision(2) << fixed << coffeeN << endl;
	outputFile << "tea    " << teaP << " new price " << setprecision(2) << fixed <<teaN << endl;


	// Close the file
	outputFile.close();
	cout << "Done.\n";
	cin.get();

	return 0;

}

Your leading code tag got messed up I can't see which is line 55. Post the code for the do while as that's the right way to do it.

Sorry the error is on line 44
Error 1 error C2181: illegal else without matching if 44

#include "stdafx.h"

#include <iostream>

#include <iomanip>

#include <fstream>
using namespace std;

int main()
{
	double percent, appleP, coffeeP, teaP, coffeeN, appleN, teaN;
	char label[7];// For labels of products on file.
	
	// Open file that has the list prices
	ifstream inputFile;
	inputFile.open("productlistIn.txt");

	//Close file with new price
	ofstream outputFile;
	outputFile.open("updatedproductlistIn.txt");

	//Read list prices from file
	//store them in the variables.
	inputFile >> label;
	inputFile >> appleP;
	inputFile >> label;
	inputFile >> coffeeP;
	inputFile >> label;
	inputFile >> teaP;


	//Get percentage from user.
	cout << "Please enter the amount to increase the prioe" << endl;
	cin >> percent;
	cin.ignore( );

	//Was percent greater than .5?
	if (percent > .5)

		cout << "Please enter a percent lesser than .5." << endl;
		cin >> percent;
	
	else
	{
	//Display the list price and new price
	cout << "Apples "<< appleP << " new price " << setprecision(2) << fixed << appleN << endl;
	cout << "coffee " << coffeeP << " new price " << setprecision(2) << fixed << coffeeN << endl;
	cout << "tea    " << teaP << " new price " << setprecision(2) << fixed <<teaN << endl;
	
	}
	//Calculation for percentage increase
	appleN = appleP * percent + appleP;
	coffeeN = coffeeP * percent + coffeeP;
	teaN = teaP * percent + teaP;

		
	//Write updated price to file.
	outputFile << "Apples " << appleP << " new price " << setprecision(2) << fixed << appleN << endl;
	outputFile << "coffee " << coffeeP << " new price " << setprecision(2) << fixed << coffeeN << endl;
	outputFile << "tea    " << teaP << " new price " << setprecision(2) << fixed <<teaN << endl;


	// Close the file
	outputFile.close();
	cout << "Done.\n";
	cin.get();

	return 0;

}

when I did the while statem earlier today it gave me about 30+ errors I dont have the code for it saved, I am trying to input it in again, but even without the if statement it was still making me enter the percent twice before it showed the new price

Ah. When you put an if statement with no braces, only the next line down is part of that if statement. So to the compiler it looks like

if( )
   statement;

another statement

else -- no if because they are broken up

Your if statement will not allow you to ask the question again after one incorrect response. If you want it to repeat you need the do/while.

okay got the do while to work but when i input .5 the first time it says that its not valid and asks me to reenter and when I do it displays the correct list

//
#include "stdafx.h"

#include <iostream>

#include <iomanip>

#include <fstream>
using namespace std;

int main()
{
	double percent, appleP, coffeeP, teaP, coffeeN, appleN, teaN;
	char label[7];// For labels of products on file.
	
	// Open file that has the list prices
	ifstream inputFile;
	inputFile.open("productlistIn.txt");

	//Close file with new price
	ofstream outputFile;
	outputFile.open("updatedproductlistIn.txt");

	//Read list prices from file
	//store them in the variables.
	inputFile >> label;
	inputFile >> appleP;
	inputFile >> label;
	inputFile >> coffeeP;
	inputFile >> label;
	inputFile >> teaP;


	//Get percentage from user.
	cout << "Please enter the amount to increase the prioe" << endl;
	cin >> percent;
	cin.ignore( );

	do
	{      
      cout<<"Please enter a percent lesser than (.51): ";
	  cin >> percent;
	  cin.ignore( );
      if(percent < 0.5);
               cout << "Invalid.  Re-enter\n";  

}while (percent > 0.51);

	//Calculation for percentage increase
	appleN = appleP * percent + appleP;
	coffeeN = coffeeP * percent + coffeeP;
	teaN = teaP * percent + teaP;

	//Display the list price and new price
	cout << "Apples "<< appleP << " new price " << setprecision(2) << fixed << appleN << endl;
	cout << "coffee " << coffeeP << " new price " << setprecision(2) << fixed << coffeeN << endl;
	cout << "tea    " << teaP << " new price " << setprecision(2) << fixed <<teaN << endl;
	
	
			
	//Write updated price to file.
	outputFile << "Apples " << appleP << " new price " << setprecision(2) << fixed << appleN << endl;
	outputFile << "coffee " << coffeeP << " new price " << setprecision(2) << fixed << coffeeN << endl;
	outputFile << "tea    " << teaP << " new price " << setprecision(2) << fixed <<teaN << endl;


	// Close the file
	outputFile.close();
	cout << "Done.\n";
	cin.get();

	return 0;

No ; on the if line if(percent < 0.5); I think you have the sign backwards for the warning too, should be percent > 0.51 just like you have on the while condition.

I changed the signs but no matter what number I put in the first input it says its not lesser than 5.1
I enter it in the next prompt and it takes it. what gives lol

I totally missed it. The whole point of the do/while is that you can get the input first and evaluate it versus the condition after. So you don't need the lines 35-37 at all. You get the first input right off the bat.
So retool your do/while statement to just say "enter a number < 0.51" or whatever or change the error message.

Thanks for all your help jonsca, I finally got it to work by deleting that code.

Awesome, good luck with the rest of it!

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.