Help with File IO

Developing a program that opens a file for writing and reads from that file. I also have some calculations that need to be done within the program. I am basically stuck on one part. I need yo know how to display all the info from the current.txt file I have created. It only displays my last values. I also attached a screenshot of my end result.

http://gyazo.com/b00489a0058de821b40f8ee5630030b9

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
#include <iostream>
#include <cstdlib>
#include <iomanip>
#include <fstream>
#include <string>
#include <cmath>
#include <stdlib.h>

using namespace std;

//Main Program
int main()
{
	ifstream inFile;
	ofstream outputFile;
    
    double current;
	double power;
	double resistance[5] = { 16, 27, 39, 56, 81 };  //    Array of size 5
    double totalResist = 0, totalCurr = 0, totalPower = 0;
    
	outputFile.open("current.txt");
	if (!outputFile)
	{
		cout << "Unable to create file current.txt\n";
		return 1;
	}
    
	// Ask the user to enter 5 valuse write on the file
	outputFile << "3.75" << endl;
	outputFile << "5.50" << endl;
	outputFile << "2.19" << endl;
	outputFile << "0.93" << endl;
	outputFile << "4.47" << endl;
    
	// Close the file
	outputFile.close();
	cout << "The file has been successfully created.\n";
    
	// We will open the file here
    inFile.open("current.txt");
    if (!inFile) {
        cout << "Unable to open file  demofile.txt\n";
        return 1;
    }
    
    cout << "Reading information from the file.\n";
    int lineNo = 1;
    while (inFile >> current)
    {
        cout << lineNo << ") " << current << endl;
        lineNo++;
    }
    
    inFile.close();
    cout << "Done.";
    
	//  Output of the table.
	cout << endl
    << endl
    << "Resistance" << setw(12) << "Current" << setw(14) << "Power"
    << endl
    << "-------------------------------------"
    << endl;
    
	// This calculates the power for each current.
    for (int i = 0; i < 5 && current; i++)
    {
        power = resistance[i] * pow(current, 2);
        totalPower += power;
        totalCurr += current;
        cout << setw(7) << resistance[i] << setw(12) << current << setw(16) << power << endl;
    }

    // Here I will display the totals for all the values.
    for (int i = 0; i < 5; i++)
        totalResist += resistance[i]; // Displays the total for the Resistance column

    
    cout << "\n-------------------------------------"
    << "\n                Total"
    << "\n-------------------------------------"
    << endl;
    
    
    cout << setw(7) << totalResist << setw(12) << totalCurr << setw(18) << totalPower << endl;
    
    
	return 0;
}

Can you post the text file you are reading in from? Just for context
text file: current.txt
3.75
5.50
2.19
0.93
4.47
Fix:
Typing up changes I made now, code is below.
Changed current to be an array of size 5
double current[5];
When you read in the currents, it now stores them in the array.
When you calculate and display for current, it will display the array index. Before you were trying to store 5 values in 5 variable. You need 5 variables to store 5 values.
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
#include <iostream>
#include <cstdlib>
#include <iomanip>
#include <fstream>
#include <string>
#include <cmath>
#include <stdlib.h>

using namespace std;

//Main Program
int main()
{
	ifstream inFile;
	ofstream outputFile;
    
    double current[5];
	double power;
	double resistance[5] = { 16, 27, 39, 56, 81 };  //    Array of size 5
    double totalResist = 0, totalCurr = 0, totalPower = 0;
    
	outputFile.open("current.txt");
	if (!outputFile)
	{
		cout << "Unable to create file current.txt\n";
		return 1;
	}
    
	// Ask the user to enter 5 valuse write on the file
	outputFile << "3.75" << endl;
	outputFile << "5.50" << endl;
	outputFile << "2.19" << endl;
	outputFile << "0.93" << endl;
	outputFile << "4.47";
    
	// Close the file
	outputFile.close();
	cout << "The file has been successfully created.\n";
    
	// We will open the file here
    inFile.open("current.txt");
    if (!inFile) {
        cout << "Unable to open file  demofile.txt\n";
        return 1;
    }
    
    cout << "Reading information from the file.\n";
    int lineNo = 1;
    while (!inFile.eof())
    {
		inFile >> current[lineNo-1] ;
		cout << lineNo << ") " << current[lineNo-1] << endl;
        lineNo++;
    }
    
    inFile.close();
    cout << "Done.";
    
	//  Output of the table.
	cout << endl
    << endl
    << "Resistance" << setw(12) << "Current" << setw(14) << "Power"
    << endl
    << "-------------------------------------"
    << endl;
    
	// This calculates the power for each current.
    for (int i = 0; i < 5 && current; i++)
    {
        power = resistance[i] * pow(current[i], 2);
        totalPower += power;
        totalCurr += current[i];
        cout << setw(7) << resistance[i] << setw(12) << current[i] << setw(16) << power << endl;
    }

    // Here I will display the totals for all the values.
    for (int i = 0; i < 5; i++)
        totalResist += resistance[i]; // Displays the total for the Resistance column

    
    cout << "\n-------------------------------------"
    << "\n                Total"
    << "\n-------------------------------------"
    << endl;
    
    
    cout << setw(7) << totalResist << setw(12) << totalCurr << setw(18) << totalPower << endl;
    
    system("pause");
	return 0;
}
Last edited on
Thank you so much. I really appreciate it. Now I know how these types of situations need to be handled. Again, Thanks.
Topic archived. No new replies allowed.