Fstream function not working

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".


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
   #include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

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;
}


Last edited on
closed account (E0p9LyTq)
You are copying the file stream into your function, you should be passing a reference:

void process_name(ifstream& in_file, double total)
closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
//#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

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;
}
Last edited on
Running into a problem with if (in_file.fail())

I changed it to pass by reference and now I have this.

Problem is definitely in the above statement because it's returning 2.
It goes through all items in the list and exist out on its own.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

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 1;
	} // 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;

			
		{
			return 2;
		}
		cout << rank << ' ';

		process_name(in_file, boy_total);
		process_name(in_file, girl_total);
		cout << endl;
	}

	system("pause");
	return 0;
}


Last edited on
Is something missing at the blank lines 43 and 44? The return 2; at line 46 doesn't have any if statement, it always executes.
closed account (48T7M4Gy)
Show us a sample of your data file.
Yes, sorry messed up the copy paste.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

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 1;
	} // 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 2;
		}
		cout << rank << ' ';

		process_name(in_file, boy_total);
		process_name(in_file, girl_total);

		cout << endl;
	}
	system("pause");
	return 0;
}
Contents of the file look like this

1 Michael 462085 2.2506 Jessica 302962 1.5436
2 Christopher 361250 1.7595 Ashley 301702 1.5372
3 Matthew 351477 1.7119 Emily 237133 1.2082
4 Joshua 328955 1.6022 Sarah 224000 1.1413
5 Jacob 298016 1.4515 Samantha 223913 1.1408
6 Nicholas 275222 1.3405 Amanda 190901 0.9726
7 Andrew 272600 1.3277 Brittany 190779 0.9720
8 Daniel 271734 1.3235 Elizabeth 172383 0.8783
9 Tyler 262218 1.2771 Taylor 168977 0.8609
10 Joseph 260365 1.2681 Megan 160312 0.8168
11 Brandon 259299 1.2629 Hannah 158647 0.8083
12 David 253193 1.2332 Kayla 155844 0.7940
13 James 244775 1.1922 Lauren 153530 0.7822
14 Ryan 241105 1.1743 Stephanie 149725 0.7628
15 John 239730 1.1676 Rachel 148907 0.7587
closed account (48T7M4Gy)
Why so many .fails?
What is this program supposed to do?
Truthfully, i'm not entirely certain.
I think it counts how many people in gender x are named y.

if it helps at all here's the code in C

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
//====================================================

// Lab7A.cpp : Defines the entry point for the console application.

//

#include &lt;iostream&gt;

#include &lt;fstream&gt;

#include &lt;string&gt;

using namespace 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&amp; in_file, double&amp; total)

{

string name;

int count;

double percent;

in_file &gt;&gt; name &gt;&gt; count &gt;&gt; percent;

if (in_file.fail())

{

return;

} // Check for failure after each input

if (total &gt; 0)

{

cout &lt;&lt; name &lt;&lt; &quot; &quot;;

}

total = total - percent;

}

int main()

{

ifstream in_file;

in_file.open(&quot;babynames.txt&quot;);

if (in_file.fail())

{

return 0;

} // Check for failure after opening

double boy_total = 50;

double girl_total = 50;

while (boy_total &gt; 0 || girl_total &gt; 0)

{

int rank;

in_file &gt;&gt; rank;

if (in_file.fail())

{

return 0;

}



cout &lt;&lt; rank &lt;&lt; &quot; &quot;;

process_name(in_file, boy_total);

process_name(in_file, girl_total);

cout &lt;&lt; endl;

}

return 0;

}

//================================================== 
closed account (48T7M4Gy)
Truthfully, i'm not entirely certain.

LOL, so you're doing this for no purpose?

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.

Good luck with it.

http://www.cplusplus.com/doc/tutorial/files/
Nah, this is my last homework assignment.
I just have to fix the damn thing and I'm done.

well one of two parts.

I did the other part which involved me writing a program from scratch that uses the I/O concept, but don't know what on earth is wrong with this one.
Fixed it!

For anyone in the near or distant future looking: here it is.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>

using namespace 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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>
#include <fstream>
#include <string>

using namespace 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;   
}
Topic archived. No new replies allowed.