Hello,
Can somebody tell me why the following doesn't work?
I loosely converted it from C to C++ (just changed it from > to >) from my textbook but calling the functions produces an error that its "attempting to reference a deleted function".
//====================================================
// Lab7A.cpp : Defines the entry point for the console application.
//
#include <iostream>
#include <fstream>
#include <string>
usingnamespace std;
/**
Reads name information, prints the name if total >= 0, and adjusts the total.
in_file the input stream
total the total percentage that should still be processed
*/
void process_name(ifstream& in_file, double& total)
{
string name;
int count;
double percent;
in_file >> name >> count >> percent;
if (in_file.fail())
{
return;
} // Check for failure after each input
if (total > 0)
{
cout << name << " ";
}
total = total - percent;
}
int main()
{
ifstream in_file;
in_file.open("babynames.txt");
if (in_file.fail())
{
return 0;
} // Check for failure after opening
double boy_total = 50;
double girl_total = 50;
while (boy_total > 0 || girl_total > 0)
{
int rank;
in_file >> rank;
if (in_file.fail())
{
return 0;
}
cout << rank << " ";
process_name(in_file, boy_total);
process_name(in_file, girl_total);
cout << endl;
}
return 0;
}
//==================================================
My best advice is to use the file I/O tutorial on this site because the samples there are easily adaptable to reading a file and once you've don that you can extend it to process the data etc. Doing it your way is the hard way.
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
usingnamespace std;
/**
Reads name information, prints the name if total >= 0, and adjusts the total.
@param in_file the input stream
@param total the total percentage that should still be processed
*/
void process_name(ifstream& in_file, double& total)
{
string name;
int count;
double percent;
in_file >> name >> count >> percent;
if (in_file.fail()) { return; } // Check for failure after each input
if (total > 0) { cout << name << " "; }
total = total - percent;
}
int main()
{
ifstream in_file;
in_file.open("babynames.txt");
if (in_file.fail()) { return 0; } // Check for failure after opening
double boy_total = 50;
double girl_total = 50;
while (boy_total > 0 || girl_total > 0)
{
int rank;
in_file >> rank;
if (in_file.fail()) { return 0; }
cout << rank << " ";
process_name(in_file, boy_total);
process_name(in_file, girl_total);
cout << endl;
}
system("pause");
return 0;
}
Perhaps this style would be more usual in C++. Rather than explicitly using fail(), this code achieves the same output - though it does not use return, hence the system pause line will be reached at end of file.
#include <iostream>
#include <fstream>
#include <string>
usingnamespace std;
/**
Reads name information, prints the name if total >= 0, and adjusts the total.
@param in_file the input stream
@param total the total percentage that should still be processed
*/
void process_name(ifstream& in_file, double& total)
{
string name;
int count;
double percent;
if (in_file >> name >> count >> percent)
{
if (total > 0) { cout << name << " "; }
total = total - percent;
}
}
int main()
{
ifstream in_file("babynames.txt");
if (!in_file) { return 0; } // Check for failure after opening
double boy_total = 50;
double girl_total = 50;
int rank;
while ((boy_total > 0 || girl_total > 0) && in_file >> rank)
{
cout << rank << " ";
process_name(in_file, boy_total);
process_name(in_file, girl_total);
cout << endl;
}
system("pause");
return 0;
}