0

Here is my code:

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

int main()

{

string fname,lname;
double pincrease //pay increase percentage,pay;
ifstream infile;
ofstream outfile;

infile.open("C:\\Users\\Connor\\Desktop");
outfile.open("C:\\Users\\Connor\\Desktop");

while(!infile.eof())
{
    infile>>lname>>fname>>pay>>pincrease;
    pay = pay*(pay*pincrease);
    outfile<<fname<<" "<<lname<<" "<<pay<<"\n";
    cin.clear();
}

infile.close();
outfile.close();

}

And here are the contents of my infile:

Miller Andrew 65789.87 5
Green Sheila 75892.56 6
Sethi Amit 74900.50 6.1

The information is in the form of Last Name:First Name:Pay:Pay Increase Percentage.

The order swap of the first and last name and the exclusion of the pay percent increase when I write to the outfile is intentional.

I'm trying to read the contents of the infile, modify them, and then write it to an outfile.

However when I execute the code I start what I'm pretty sure is an infinite loop but I'm not sure how to fix it.

user2240033
  • 51
  • 1
  • 7
  • 1
    Are you reading from and writing to your desktop folder? – mgamba Dec 20 '13 at 08:23
  • 1
    [`while(!infile.eof())` is rarely the right thing to do.](http://stackoverflow.com/q/5605125/78845) – Johnsyweb Dec 20 '13 at 08:27
  • @mgamba Yeah. I have two separate text files named infile and outfile on my desktop for convenience. – user2240033 Dec 20 '13 at 08:29
  • 1
    It looks like `infile` and `outfile` are pointing to the `Desktop` folder itself. Also the `lname` declaration is commented out. – mgamba Dec 20 '13 at 08:33
  • @mgamba Yeah the commenting out was an accident. Put the comments in the post for clarification but I couldn't do it right so I just removed them. – user2240033 Dec 20 '13 at 08:51
  • You still need to specify the actual files you are using. Also, `double pincrease //pay increase percentage,pay;` should be `double pincrease, pay;`. – mgamba Dec 20 '13 at 16:30

2 Answers2

2

Neither of these statements likely open files:

infile.open("C:\\Users\\Connor\\Desktop");
outfile.open("C:\\Users\\Connor\\Desktop");

Rather, they attempt (and likely fail) to open your Desktop folder. Rather, you likely wanted something more like:

infile.open("C:\\Users\\Connor\\Desktop\\infile");
outfile.open("C:\\Users\\Connor\\Desktop\\outfile");

Of course, infile and outfile at the end there should be replaced with actual filenames.

You can test whether your file opens succeeded by checking infile.is_open() and outfile.is_open(). You might add explicit if statements to test this:

if (!infile.is_open())
    // report/handle that you couldn't open input file

if (!outfile.is_open())
    // report/handle that you couldn't open output file

For your main loop, you shouldn't be testing eof as you do. Rather, use something like this:

while(infile>>lname>>fname>>pay>>pincrease)
{
    pay = pay*(pay*pincrease);
    outfile<<fname<<" "<<lname<<" "<<pay<<"\n";
    cin.clear();
}

Testing for EOF the way you were will try to read one extra record beyond end of file.

The EOF flag only gets set after a failed read attempt. Therefore you should always test for EOF after trying to read.

The standard ifstream is set up in such a way that a typical ifstream input operation (ie. infile >> item) will return a reference to an ifstream object. That's how you can do things like infile >> item1 >> item2 >> item3.

When you place that in the context of a loop control (as above), ifstream has the appropriate operator overloads that cause it to tell while whether to keep looping or not based on whether the reads succeeded.

Others have explained that overload magic well enough elsewhere. More info on the loop termination magic here: Why istream object can be used as a bool expression?

Community
  • 1
  • 1
Joe Z
  • 15,967
  • 3
  • 25
  • 38
  • Thank you so much. Especially for the link. – user2240033 Dec 20 '13 at 08:55
  • ihmo rathe unpractical syntax, when you have to decide which data types to read depending on previously read data. – Valentin Heinitz Dec 20 '13 at 08:55
  • @ValentinHeinitz : Sure, and that's definitely a language design issue. The standard `ifstream` doesn't lend itself well to a more general language tokenizer / grammar. But if that's what you need, then you should build something different than the basic tools `ifstream` provides. – Joe Z Dec 20 '13 at 08:57
1

Replace this

infile.open("C:\\Users\\Connor\\Desktop");
outfile.open("C:\\Users\\Connor\\Desktop");

with this

infile.open("C:\\Users\\Connor\\infile.txt");
outfile.open("C:\\Users\\Connor\\outfile.txt");

The code you have posted will not compile for two reasons. One, The variable pay has not been declared and two, you have commented out the termination of the following code.

double pincrease //pay increase percentage,pay;

Try the below code. Hope it helps

            int main()

            {

                string fname,lname;
                double pincrease; //pay increase percentage,pay;
                double pay;
                ifstream infile;
                ofstream outfile;

                infile.open("C:\\Users\\Connor\\infile.txt");
                outfile.open("C:\\Users\\Connor\\outfile.txt");

                while(!infile.eof())
                {
                    infile>>lname>>fname>>pay>>pincrease;
                    pay = pay*(pay*pincrease);
                    outfile<<std::setprecision(2)<<std::showpoint << std::fixed;;
                    outfile<<fname<<" "<<lname<<" "<<pay<<"\n";
                    cin.clear();
                }

                infile.close();
                outfile.close();

            }
Ravi Shenoy
  • 477
  • 1
  • 4
  • 14