Input and Output to file - help please

Ok, I need to read from a file, use its contents to preform calculations, then output to a different file.

The input file contains whole number values arranged like this:

50000
10250
15250

ect...

Now, I need to read this file, then use each number as an INT type to preform calculations on.

I then need to output the calculated info to an output file along with some added text to explain the calculations.

You can see what I am trying to do from the code below.

My problem is that I need a better way to actually grab the input, and send the output.
Right now the only way I know how to output is to redirect the cout to a file.

The input is semi-functional. It only takes the first number of each line.

Please forgive me if any code is hard to read..I have been trying tons of things to get this to work..so there may be some left over code from previous attempts.

#include <iostream>
#include <fstream>
using namespace std;

#define RaisePercent .025; //.025 = 2.5%

float OldSalary, NewSalary, SalaryIncrease = 0;
float TotalOldSalary, TotalNewSalary, TotalIncrease = 0;
ifstream ins;
ofstream outs;

void Raise(ifstream&, ofstream&);
void GetInput();
////////////////////////////////////////////////////////////////////

int main()
{

cout.rdbuf(outs.rdbuf()); // Redirects cout to file
GetInput();
Raise(ins, outs);

return 0;
}
////////////////////////////////////////////////////////////////////

void GetInput()
{
ins.open("C:\\Documents and Settings\\Arcane\\Desktop\\In.txt");
outs.open("C:\\Documents and Settings\\Arcane\\Desktop\\Out.txt");
}
///////////////////////////////////////////////////////////////////

void Raise(ifstream& ins, ofstream& outs)
{
char input;
while (!ins.get(input) == NULL)
{
ins.ignore(256, '\n');
OldSalary = input - '0'; //Convert CHAR to INT

cout<<"Original Salary $"<<OldSalary<<endl<<endl;


TotalOldSalary = TotalOldSalary + OldSalary;

SalaryIncrease = OldSalary * RaisePercent;
NewSalary = SalaryIncrease + OldSalary;

TotalNewSalary = TotalNewSalary + NewSalary;
TotalIncrease = TotalIncrease + SalaryIncrease;


cout<<"The new Salary is $"<<NewSalary<<endl;
cout<<"This is an increase of $"<<SalaryIncrease<<endl<<endl;
}

cout<<"The total salary was $"<<TotalOldSalary<<endl;
cout<<"The total new salary will be $"<<TotalNewSalary<<endl;
cout<<"This is a total increase of $"<<TotalIncrease<<endl<<endl<<endl;
}

Input File:

50000
20000
15000
60000


Output File:

Original Salary $5

The new Salary is $5.125
This is an increase of $0.125

Original Salary $2

The new Salary is $2.05
This is an increase of $0.05

Original Salary $1

The new Salary is $1.025
This is an increase of $0.025

Original Salary $6

The new Salary is $6.15
This is an increase of $0.15

The total salary was $14
The total new salary will be $14.35
This is a total increase of $0.35


Thanks in advance!
[3514 byte] By [ArcaneDreams] at [2007-11-20 11:33:45]
# 1 Re: Input and Output to file - help please
Dear,
You are getting only one digit of each line because you are using variable of type char which can hold just one character at a time. Use pointer to character variable instead to get whole line.
Then use atoi() function to convert string value into integer.
Hopefully the problem will be resolved.
anwar4849 at 2007-11-10 22:25:05 >
# 2 Re: Input and Output to file - help please
Thanks!

I changed this:

char input;
while (!ins.get(input) == NULL)
{
ins.ignore(256, '\n');
OldSalary = input - '0'; //Convert CHAR to INT

To this:

char input [256];
while (!ins.get(input, 256) == NULL)
{
ins.ignore(256, '\n');
OldSalary = atoi(input);

And all is well. :)

Now, is there any way to write to file other than redirecting the cout like I have done?
The reason I ask is because I would like to cout something to the user and write something else to the file.

And do you have any references or links to anywhere I may learn all about how to read from files?

For instance if instead of having an input file of just numbers...

50000
25000

I would like to try having this:

John Kennedy: $50,000
George Washington: $45,250

I would need a way to parse the text and take certain parts out. I have done this before working with TCL scripts..but that is a whole different beast.

Thanks again!
ArcaneDreams at 2007-11-10 22:26:10 >
# 3 Re: Input and Output to file - help please
You can use file pointers to open and access files. For details of file handling, visit the following link:

http://www.cplusplus.com/doc/tutorial/files.html
anwar4849 at 2007-11-10 22:27:08 >