I have some problems in my program. I have to read info from input files and display a message. This is what I have:

#include <iostream>
#include <fstream>
#include <iomanip>

using namespace std;

void Printing(ofstream& outFile, int& percentFat);
void Percent(float& fatGrams, float& cal, float& fatCal, int& percentFat);

int main()
{
  string food;
  float fatGrams, cal, fatCal;
  int percentFat;
  ofstream outFile;
  ifstream inFile;

  inFile.open("in.dat*");
  outFile.open("out.data");

  /*getline(inFile, food);
  inFile >> fatGrams >> cal;
  inFile.ignore('/n');*/

  while(inFile)
  {
    getline(inFile, food);
    inFile >> fatGrams >> cal;
    inFile.ignore('/n');
    fatGrams =+ fatGrams;
    cal += cal;
    outFile << "You ate "<< food << endl
          << "Which had " << fatGrams << " grams of fat and " << cal << " calories" << endl;

  }
}

void Printing(ofstream& outFile, int& percentFat)
{ 
  if (percentFat < 30)
    outFile << "Congratulations! You had a Heart Healthy Meal." << endl;
  else
    outFile << "Warning! You had too many fat calories." << endl;
}   

void Percent(float& fatGrams, float& cal, float& fatCal, int& percentFat)
{   
  fatCal = fatGrams*99;
  percentFat = int((fatCal/cal)*100);
}         

My problem here is that I don't know how to use the getline and ignore function.Here is how one of the input files lookks like:

Egg McMuffin with orange juice and coffe
11
360
Chunky chicken salad with iced tea
6
198
Regular hamburger with small fries and Diet Coke
21
476

Line 18 -- get rid of the asterisk in the filename.
Line 29 - The escape character is '\', not '/'.
Line 29 - Don't forget the first parameter. Just make it a big number like 256.

inFile.ignore(256, '\n');

That should solve most of your problems. You still potentially have a problem of going through the while loop one too many times. To solve, consider adding this after you read things in to short-circuit the while loop if needed.

if(!inFile.good())
{
    break;
}
while(inFile.good()){
// read the file content

}

Thats another way to read file content. listen to vernon... he has given more information than you needed.
You can also get the size of the file and read the content in little chunks.

Thats another way to read file content.

Assuming that the body of the loop properly checks for and breaks on end of file, sure. However, this loop will likely print the last line of the file twice:

while (inFile.good()) {
    getline(inFile, line);
    cout << line << '\n';
}

The reason is because getline() will reach end of file and do nothing with line, but the output sits between the reaching of a "failure" state and the test for that "failure" state. The obvious fix would be to break on failure:

while (inFile.good()) {
    if (!getline(inFile, line)) {
        break;
    }

    cout << line << '\n';
}

But a C++ convention already exists that does this as the loop condition because it's more concise and says everything that needs to be said without extra fluff:

while (getline(infile, line)) {
    cout << line << '\n';
}

As such, the recommended approach is to ensure that your input mechanism can be called and tested as the loop condition because anything else risks a variation of the classic feof() bug from C.

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.