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;

//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
     if (infile.fail())
         cout << "The input file cannot be located" << endl;
     outfile.open("test_output.out", ios::out);
     if (outfile.fail())
         cout << "The output file could not be created" << endl;
       outfile << fixed << setprecision(0);
       //while loop to read the  
             infile >> planmin >> usedmin;
             outfile << name << " " << planmin << " " << usedmin << endl;
  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

Edited by valkerye: n/a

7 Years
Discussion Span
Last Post by Ancient Dragon

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
   name[i++] = c;
name[i] = '\0'; // null terminate the string
infile >> planmin >> usedmin;

Edited by Ancient Dragon: n/a


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.

This article has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.