Trying to Read Input File

Hi, I'm trying to read from a file and execute a while statement to analyze the data, but I'm having some issues. The initial user input and display is working, but the rest of the code is not executing. What might I be missing here?

This is the code in its entirety. I'm trying to let the user name the integers file prior to executing the calculations.

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
////////////////////////////////////////////////////////////////////////////////////////////////////////
//CSC 134, Lab 3A
//Author: Kevin Miller
//Date: June 4, 2015
//This class reads data from a file and outputs calculations and information from the data.
////////////////////////////////////////////////////////////////////////////////////////////////////////

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

using namespace std;

int main()
{
	//Variables
	string inputfile;
	int num = 0;
	int even = 0;
	int totaleven = 0;
	int odd = 0;
	int totalodd = 0;
	int sumodd = 0;
	int sumeven = 0;
	int sumall = 0;
	int smallest = 1000;
	int largest = 0;
	double average = 0.0;
	int count = 0;
	int value = 0;
	ifstream inFile ("integers.txt");

	cout << "Please enter the name of your file: " << endl;

	cin >> inputfile;
	
	cout << "This program processes the numbers in " << inputfile << "." << endl;

	inFile.open(inputfile.c_str());
	
	while (inFile.eof() == false)
	{
		inFile >> num;
		count++;
		
		if (num % 2 == 0)
		{
			num = even;
			sumeven = sumeven + even;
			totaleven++;
		}
		if (num % 2 != 0)
		{
			num = odd;
			sumodd = sumodd + odd;
			totalodd++;
		}
		sumall = sumeven + sumodd;

		if (num > largest)
		{
			largest = value;
		}

		if (num < value)
		{
			smallest = value;
		}
			

	}

	average = (largest + smallest) / 2;

	//Printouts
	cout << "The total amount of numbers is: " << count << endl;
	cout << "The total amount of even numbers is: " << totaleven << endl;
	cout << "The sum of the even numbers is " << sumeven << endl;
	cout << "The total amount of odd numbers is: " << totalodd << endl;
	cout << "The sum of the odd numbers is " << sumodd << endl;
	cout << "The sum of all the numbers is" << sumall << endl;
	cout << "The largest number is:" << largest << endl;
	cout << "the smallest number is: " << smallest << endl;
	cout << "The average of the largest and smallest numbers is: " << average << endl;

	

	inFile.close();

	system("pause");

	return 0;

}
Last edited on
Please tidy your code up (by editing orig. post!) so it is correctly formatted.

Thanks, Andy

(I do hope you code has ended up "mashed" here due to problems cutting and pasting it, and that it doesn't look that bad in your editor or IDE. Note that some editors use a tab size of 4 whereas the code display here uses 8. For that reason I usually convert all tabs to spaces before pasting code here, so it looks the same I see it in Visual Studio or CodeLite.)
I edited it to include the entire code. I've been playing around with different setups, but nothing seems to execute the while statement to determine the calculations and display them. It just stops after the second cout statement.
It's not stopping as such; it's hitting an infinite loop.

The problem is that you're trying to open inFile twice -- once on line 32 (for integers.txt) and again on line 40 with the name the user provides.

If you change line 32 to

ifstream inFile;

it should be OK wrt reading the file.

Andy

PS You code plus a bit of diagnostics so you can see what I did to confirm my diagnosis (plus a few random comments.)

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
////////////////////////////////////////////////////////////////////////////////////////////////////////
//CSC 134, Lab 3A
//Author: Kevin Miller
//Date: June 4, 2015
//This class reads data from a file and outputs calculations and information from the data.
////////////////////////////////////////////////////////////////////////////////////////////////////////

#include<iostream>
#include<fstream>
#include<string>
#include<iomanip>
#include<cstdlib> // for system() when building GCC

using namespace std;

int main()
{
    //Variables
    string inputfile;
    int num = 0;
    int even = 0;
    int totaleven = 0;
    int odd = 0;
    int totalodd = 0;
    int sumodd = 0;
    int sumeven = 0;
    int sumall = 0;
    int smallest = 1000;
    int largest = 0;
    double average = 0.0;
    int count = 0;
    int value = 0;
    //ifstream inFile ("integers.txt");
    ifstream inFile; // don't open inFile twice

    cout << "Please enter the name of your file: " << endl;

    cin >> inputfile;
    
    cout << "This program processes the numbers in " << inputfile << "." << endl;

    inFile.open(inputfile.c_str());

    // debug code
    int max = 200;
    while (inFile.eof() == false)
    {
        inFile >> num;
        if(inFile.bad())
            cout << "file is bad\n";
        count++;

        // debug code
        cout << count << " : " << num << "\n";
        
        if (num % 2 == 0)
        {
            num = even; // what does this line do?
            sumeven = sumeven + even;
            totaleven++;
        }
        if (num % 2 != 0) // do you know about 'else'?
        {
            num = odd; // what does this line do?
            sumodd = sumodd + odd;
            totalodd++;
        }
        sumall = sumeven + sumodd; // do you need to do this so often?

        if (num > largest)
        {
            largest = value;
        }

        if (num < value) // ???
        {
            smallest = value;
        }

        // debug code
        --max;
        if(0 == max)
            break;
    }

    average = (largest + smallest) / 2; // is this how to calc an average?? 

    //Printouts
    cout << "The total amount of numbers is: " << count << endl;
    cout << "The total amount of even numbers is: " << totaleven << endl;
    cout << "The sum of the even numbers is " << sumeven << endl;
    cout << "The total amount of odd numbers is: " << totalodd << endl;
    cout << "The sum of the odd numbers is " << sumodd << endl;
    cout << "The sum of all the numbers is" << sumall << endl;
    cout << "The largest number is:" << largest << endl;
    cout << "the smallest number is: " << smallest << endl;
    cout << "The average of the largest and smallest numbers is: " << average << endl;

    inFile.close();

    system("pause");

    return 0;
}
Last edited on
PS The standard way to read a text file is:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <fstream>
using namespace std;

int main() {
    ifstream inFile("integers.txt");
    if(!inFile.is_open()) {
        cout << "file failed to open\n";
        return 1;
    }
    int num = 0;
    while(inFile >> num) { // this checks for errors as well as eof
        cout << num << "\n";
    }
    return 0;
}


How does that funky while (std::cin >> foo) syntax work?
https://isocpp.org/wiki/faq/input-output#istream-and-while
Thank for the help. It still doesn't seem to do what I want it to. I've added notes about what the functions are supposed to do. Also, with the current code, it's not reading the file I have in the resources folder (integers.txt). It's actually compiling but it's printing all 0s and reading the default variables that I assigned...


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
#include<iostream>
#include<fstream>
#include<string>
#include<iomanip>

using namespace std;

int main()
{
	//Variables
	string inputfile;
	int num = 0;
	int even = 0;
	int totaleven = 0;
	int odd = 0;
	int totalodd = 0;
	int sumodd = 0;
	int sumeven = 0;
	int sumall = 0;
	int smallest = 1000;
	int largest = 0;
	double average = 0.0;
	int count = 0;
	int value = 0;
	ifstream inFile;

	cout << "Please enter the name of your file: " << endl;

	cin >> inputfile;
	
	cout << "This program processes the numbers in " << inputfile << "." << endl;

	inFile.open(inputfile.c_str());
	
	int max = 200;
	while (inFile.eof() == false)
	{
		inFile >> num;
		if (inFile.bad())
			cout << "file is bad\n";
		count++; //find the total number of integers in integer.txt
		
		cout << count << " : " << num << "\n";

		if (num % 2 == 0) 
			{
				num = even; //determine which numbers are even
				sumeven = sumeven + even; //find the sum of all the even numbers
				totaleven++; //find the total number of even integers
			}
		if (num % 2 != 0)
		{
			num = odd; //determine which numbers are odd
			sumodd = sumodd + odd; //find the sum of all the odd numbers
			totalodd++; //find the total number of odd integers
		}
		sumall = sumeven + sumodd;
	
		if (num > largest) //find the largest number
		{
			largest = value;
		}
	
		if (num < value) //find the smallest number
		{
			smallest = value;
		}
			
		--max;
		if (0 == max)
			break;

	}

	average = (largest + smallest) / 2; //This averages the largest and smallest number

	//Printouts
	cout << "The total amount of numbers is: " << count << endl;
	cout << "The total amount of even numbers is: " << totaleven << endl;
	cout << "The sum of the even numbers is " << sumeven << endl;
	cout << "The total amount of odd numbers is: " << totalodd << endl;
	cout << "The sum of the odd numbers is " << sumodd << endl;
	cout << "The sum of all the numbers is" << sumall << endl;
	cout << "The largest number is:" << largest << endl;
	cout << "the smallest number is: " << smallest << endl;
	cout << "The average of the largest and smallest numbers is: " << average << endl;

	

	inFile.close();

	system("pause");

	return 0;

}
When I stopped inFile from opening twice the file reading worked fine for me. (Your file is either one number per line or at least space delimited?)

Is your program finding the right file??

And I flagged this line

num = odd;

as what it is actually doing is setting num to zero (I don't get what you mean by "determine which numbers are even")

Earlier on you set the variables even and odd to zero, and then set the value you've just read in -- num -- to their value (i.e. zero), so by the time you do you your summing and testing for smallest and largest num is always zero. So all your sums will be zero, your max will be zero, and your min 1000.

Also:
- the variable value looks totally spurious. On line 64 it should be smallest and on line 61 and 66 it should be num. I don't think value should exist at all.
- do you know how to calculate the mean? (You have the required data to do this.)

Andy
Last edited on
I am trying to calculate the mean but just for the largest and smallest numbers, so that's why I have largest + smallest / 2.

I got rid of the num = odd/even functions and left it at sumodd = sumodd + num after the if statements.

I think the only issue that's causing the program to fail to do its intended function is that it's not reading from the file. I'm getting all default numbers (200 lines of 0s). I'm using Visual Studio 2010, and I've tried putting integers.txt in the project folder in ever possible place under that fold as well as adding it as an existing item through Visual Studio. Any ideas why it's not reading it?
It would make sense to protect your code by using ifstream::is_open() to check the file was successfully opened.
http://www.cplusplus.com/reference/fstream/ifstream/is_open/

Then you can use the _getcwd() function to get your program to tell you where it's looking.
https://msdn.microsoft.com/en-us/library/sf98bd4y.aspx
(this is the Microsoft take on the POSIX getcwd function.)

If you're running your app from Visual Studio, change the "Working Directory" on the "Debug" page of your projects' properties.

Andy
Topic archived. No new replies allowed.