Hello, I'm trying to create a program that reads data from outside file using functions. My goal is to get data from an outside file, and use that data for other functions that i havent created yet. this is my code so far but it wont read and giving me output errors.

PLease help, thanks so much!

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

ifstream infile;
ofstream outfile;
int noOfAdults, noOfKids;
float deposit;
string mealType;
bool weekend;

void getData ();

int main()
{	
	

	infile.open ("C:\\Users\\garu525\\Desktop\\CateringDatafile.rbh");
	if (!infile)
	{
		cout << "File not found.  Exiting. . ." << endl;
		exit(1);
	}

	outfile.open ("C:\\Users\\garu525\\Desktop\\CateringBill.rbh");
	
	while (!infile.eof())
	{
		getData ();
	}
	cout << fixed << showpoint << setprecision(2);
	cout << noOfAdults << '\t' << noOfKids << '\t' << mealType << '\t' <<weekend << '\t' << deposit << endl;	
	
		
	infile.close();
	outfile.close();
	system ("pause");
	return 0;
}

void getData ()
{
	infile >> noOfAdults >> noOfKids >> mealType >> weekend >> deposit;
	cout << noOfAdults << noOfKids << mealType << weekend << deposit;
	
	if (noOfAdults < 0 || (!infile)) outfile << "Error on number of Adults data.";
	if (noOfKids < 0 || (!infile)) outfile << "Error on number of Children data.";
	if (mealType != "S" || "D") outfile << "Error on Meal type data.";
	if (mealType == "S") mealType = "Standard";
	if (mealType == "D") mealType = "Deluxe";
	if (!weekend) outfile << "Error on weekend data";
	if (deposit < 0) outfile << "Error on Deposit data";
}

Sorry about that. What I meant is the data is not storing properly (goes to endless loop)and the output looks like this:

Error on number of Adults data.Error on number of Children data.Error on Meal type data.Error on weekend dataError on number of Adults data.Error on number of Children data.Error on Meal type data.Error on weekend dataError on number of Adults data.Error on number of Children data.Error on Meal type data.Error on weekend dataError on number of Adults data.Error on number of Children data.Error on Meal type data.Error on weekend dataError on number of Adults data.Error on number of Children data.Error on Meal type data.Error on weekend dataError on number of Adults data.Error on number of Children data.Error on Meal type data.Error on weekend dataError on number of Adults data.Error on number of Children data.Error on Meal

So what was read? Don;t just output an error message, output the wrong data too.

The easiest debug tool in C++ is the cout statement. Use it to follow your program as it executes.

This article has been dead for over six months. Start a new discussion instead.