I'm having an issue with an end of file while loop.
I have narrowed a problem with a previous program into a smaller bit of code and the error is in the read in and i hove no idea what to do about it... I have done this kind of thing before but never with the set spaced char variable so that has to be it.

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

main()
{
//definition of the variables for the program
      
ifstream infile;
ofstream outfile;
char name[15];
int planmin, usedmin, fivecount, thoucount, twothoucount, bonustotal, bonusmin, quotamin, quotatot, mintotal, users; 
float average;


     //opening of the input and putput files
     infile.open("Sell_U_R.dat");
     if (infile.fail())
       {
         cout << "The input file cannot be located" << endl;
         exit(1);
       }
     
     outfile.open("test_output.out", ios::out);
     if (outfile.fail())
       {
         cout << "The output file could not be created" << endl;
         exit(1);
       } 
       
       outfile << fixed << setprecision(0);
       
       
       
       //while loop to read the  
       while(!infile.eof())
          {
             infile.getline(name,15);
             infile >> planmin >> usedmin;
                       
       
             outfile << name << " " << planmin << " " << usedmin << endl;
             
             
             
          }   
            
       
  infile.close();
  outfile.close();      
  system("pause");
  return 0;
}

I've just tried to output what I've input and its not working correctly so I'm not sure what I've done wrong with the read in

I'm getting an infinite loop of output but with an input file that looks like

W. Jones 1000 1250
Bud Wilson 2000 2075
Jim Jones 500 2873
Andre T. Lui 1000 824

I'm getting

W. Jones 2 0
2 0
2 0
2 0
2 0
2 0
2 0

Recommended Answers

All 3 Replies

Because the name may contain one or more spaces you need to read the file one character at a time until the first digit is found, which will be the end of the name.

int i = 0;
char c;
while( infile.get(c) )
{
   if( isdigit(c) )
   {
      infile.unget(); // put the digit back onto the file
     break;
   }
   name[i++] = c;
}
name[i] = '\0'; // null terminate the string
infile >> planmin >> usedmin;

part of the given information is that the longest name is 15 characters and the start of the integers is at least 20 spaces away

this is way beyond anything we've done at this point .. I barely understand what that code means tho i appreciate the help ^_^

If you don't know what a function does then google for it and read all about it. For example, google for fstream and the first google link will tell you all about fstream. Open up that link and you will see all the fstream methods. Now do the same for isdigit (which returns 0 if the character is not a digit '0' - '9' or non-0 if it is.

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.