Reading number from a file

Part 1 / Insert number to 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
// Reaper

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

int main()
{
    int num1;
    int num2;
    int num3;
    int num4;
    int num5;

    cout << "This will store 5 integers to a file.\n\n";

    ofstream datafile;

    datafile.open("intergers.txt");

    cout << "Enter number 1: ";
    cin >> num1;
    cout << "Enter number 2: ";
    cin >> num2;
    cout << "Enter number 3: ";
    cin >> num3;
    cout << "Enter number 4: ";
    cin >> num4;
    cout << "Enter number 5: ";
    cin >> num5;

    datafile << num1 << endl;
    datafile << num2 << endl;
    datafile << num3 << endl;
    datafile << num4 << endl;
    datafile << num5 << endl;
    cout << "\nYour numbers have been written to the intergers.txt file" << endl;

    datafile.close();

    return 0;
}


Part 2 / Read numbers from file and add them
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
// Reaper

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

int main()
{
    int num1;
    int num2;
    int num3;
    int num4;
    int num5;
    int total;

    cout << "This will read the numbers from the file and add them together.\n\n";

    ifstream datafile;

    datafile.open("/home/reaper/Desktop/Programs/Chapter 3/Storing And Retrieving Numbers Using Files/Part 1/Storing and Retrieving Numbers/intergers.txt");

    datafile >> num1;
    datafile >> num2;
    datafile >> num3;
    datafile >> num4;
    datafile >> num5;

    total = num1 + num2 + num3 + num4 + num5;

    cout << "\nThe numbers have been read in from the intergers.txt file.";

    cout << "\n\n";
    cout << "The total is " << total << endl;

    datafile.close();

    return 0;
}


my problem is at part 2, reading the number from the file. when i run part 2 it gives me a random number like -4999393249 and doesnt even read in my numbers, and even if in part 2 i change ifstream to ofstream it does the same thing. what am i doing wrong.
Last edited on
heres my new code for part2.

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
// Reaper

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

int main()
{
    int num1;
    int num2;
    int num3;
    int num4;
    int num5;
    int total;

    cout << "This will read the numbers from the file and add them together.\n\n";

    ifstream datafile;

    datafile.open("/home/reaper/Desktop/Programs/Chapter 3/Storing And Retrieving Numbers Using Files/Part 1/Storing And Retrieving Numbers/intergers.txt");

    if(datafile.fail())
    {
        cout << "Unable to open intergers.txt file." << endl;
        return (1);
    }

    datafile >> num1;
    datafile >> num2;
    datafile >> num3;
    datafile >> num4;
    datafile >> num5;

    cout << "\nThe numbers have been read in from the intergers.txt file.";

    total = num1 + num2 + num3 + num4 + num5;


    cout << "\n\n";
    cout << "The total is " << total << endl;

    datafile.close();

    return 0;
}


Ok, i got the file to read in the answer for the numbers read from the file, but i cant seem to get it to actually display the numbers read from the file, all i can get it to do is display the total. How can i get it to read in the number from the file also.
try this:
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
#include <iostream>
#include <fstream>

using namespace std;

int main()
{
	int counter, numbersHeld[5];
	ofstream fileTo;

	fileTo.open("integers.txt");

	for(counter = 0; counter < 5; counter = counter + 1)
	{
		cin>> numbersHeld[counter];
		fileTo<< numbersHeld[counter] << endl;
	}

	fileTo.close();
}

part 2:

#include <iostream>
#include <fstream>

using namespace std;

int main()
{
	int total, currNumber;
	ifstream fileFrom;

	fileFrom.open("integers.txt");
	total = 0;

	while(fileFrom.good()) //if this doesnt work try !fileFrom.eof()
	{
		fileFrom >> currNumber;
		total = total + currNumber;
	}

	fileFrom.close();
}
is there a way so i can do like in part and have the user input the intergers , part 2 read those intergers to the screen and add them together.

heres the code now

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
// Reaper

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

int main()
{
    int num1;
    int num2;
    int num3;
    int num4;
    int num5;
    int total;

    cout << "This will read the numbers from the file and add them together.\n\n";

    ifstream datafile;

    datafile.open("/home/reaper/Desktop/Programs/Chapter 3/Storing And Retrieving Numbers Using Files/Part 1/Storing And Retrieving Numbers/intergers.txt");

    if(datafile.fail())
    {
        cout << "Unable to open intergers.txt file." << endl;
        return (1);
    }

    while (!datafile.eof())
    {
        datafile >> num1;
        cout << num1 << endl;
        datafile >> num2;
        cout << num2 << endl;
        datafile >> num3;
        cout << num3 << endl;
        datafile >> num4;
        cout << num4 << endl;
        datafile >> num5;
        cout << num5 << endl;
    }

    cout << "\nThe numbers have been read in from the intergers.txt file.";

    total = num1 + num2 + num3 + num4 + num5;

    cout << "\n\n";
    cout << "The total is " << total << endl;

    datafile.close();

    return 0;
}


its now printing the numbers to the screen , but it displays this:

1
2
3
4
5
1
2
3
4
5

its only suppose to do 1-5 once, if i comment out any of the datafile >> or cout << , then no numbers get displayed how do i fix this.
Last edited on
Anyone know why?
open up the .txt file thats probably where the problem is
This is what my txt file says

1
2
3
4
5
Your problem is loop starting in line 29. Your data file has 5 values in it. You enter your while loop and read 5 values. Because you legitimately read the 5th value, the eof flag has not been set. So you go through the loop again.

The second time through, the << operator fails each time, so your values do not change. But, you have no control structures to get you out of the loop. So, you print out each of your values again.

I would suggest changing your while statement to an if statement.

Edit: Just get rid of the while loop. The if I was thinking of is already in line 23.
Last edited on
ah ok i see, i understand how that works now, thank you my program works now.
Topic archived. No new replies allowed.