Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help on first C++ program, compiling errors 1

Status
Not open for further replies.

Kyusaku

Technical User
Feb 20, 2001
26
US
Hi all, I am new here and would greatly appreciate some help with my program. I am very new to programming and have been troubled by my class assignment. It is a simple input output program that is supposed to read a file with a list of parts and prices, compute the cost for the number of parts and then figure out the tax and then the grand total. Can anyone tell me what I am doing wrong, (more like show me...if possible)Here is a link to my code and the input file.

Link to code:

Link to input file:

Thank very much for any help given!!!!
 
Kyusaku,
I looked at your program and there are several reasons why you getting errors. Your path to your file is incorrect needs to be "C:\\hardware.txt" Use "\\" to specify a single "\" in a string. Your read in a string from a file and then you are trying to perform calculations using that string. On way to handle this is to convert the the strings to floats using the atof function. I tried to keep as close to your original program as possible but realize you will learn better ways to handle such reading from a file, manipulating data and writing to a file. The code below will accomplish all that I think you set out to do.

HTH,
JC


#include <iostream.h>
#include <fstream.h>
#include <iomanip.h> //need for setw
#include <string.h>
#include <stdlib.h> //need for atof

double Pricing (char* sQty, char* sPrice);

void main()
{
char sName[15], sCode[15], sQty[15], sPrice[15];
double dTotalPrice, dTax;

ifstream infile (&quot;c:\\hardware.txt&quot;);
ofstream outp(&quot;c:\\outfile.txt&quot;);

infile >> sName >> sCode >> sQty >> sPrice;
cout << setw(15) << sName << setw(15) << sCode << setw(15)
<< &quot;Cost&quot; << setw(10) << &quot;Tax&quot; << setw(10) << &quot;Total&quot;
<< endl;
outp << setw(15) << sName << setw(15) << sCode << setw(15)
<< &quot;Cost&quot; << setw(10) << &quot;Tax&quot; << setw(10)<< &quot;Total&quot;
<< endl;

while (!infile.eof())
{
infile >> sName >> sCode >> sQty >> sPrice;
dTotalPrice = Pricing(sQty, sPrice);
dTax = dTotalPrice * 0.05;

cout << setw(15) << sName << setw(15) << sCode
<< setw(15) << dTotalPrice << setw(10) << dTax
<< setw(10) << dTotalPrice + dTax << endl;

outp << setw(15) << sName << setw(15) << sCode
<< setw(15) << dTotalPrice << setw(10) << dTax
<< setw(10) << dTotalPrice + dTax << endl;
}
}



double Pricing (char* sQty, char* sPrice)
{
return (atof(sQty) * atof(sPrice));
}
 
Thanks Mongooses!! That helped me out a lot!!

Cheers!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top