Help with File I/O and math

I am trying to write a program that will:

1. Output the result into a file instead of displaying them on screen
2. Change the input to read data from a file instead of from cin. The file name is called current.txt

The current.txt file has the values of:
3.75
5.50
2.19
0.93
4.47

There is also a resistance value in array:
double resistance[5] = {16, 27, 39, 56, 81};

the calculation will be using the following formula:
power[i] = resistance[i] * pow(current[i],2);

I am at a complete lose, I can display the file, but that is as far as I got.

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
#include <iostream>
#include <cstdlib>
#include <iomanip>
#include <fstream>
#include <string>
#include <cmath>
#include <vector>
#include <algorithm>

using namespace std;

//Main Program
int main() 
{
    ifstream inFile;


    const int SIZE = 15;
    char name[SIZE];

    inFile.open("c:\\current.txt");
    if (!inFile) {
        cout << "Unable to open file  demofile.txt\n";
		system ("pause");
        return 1;
    }

    cout << "Reading information from the file.\n";
    while (inFile >> name) 
	{
	cout  << name << endl;
    }

    inFile.close();      
    cout << "Done.";

	system ("pause");
    return 0;


}
	


Any help will be much appreciated. Thank you very much.
you need to declare an ofstream and then run a loop in which you multiply each element in the "name" array by the formula you have. then you output the info to the ofstream instead of cout.
Ok I think I have it down pretty well. I tried to shorten the code (shortcut it) but I can't find a way around it. this is what I have so far.

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
#include <iostream>
#include <cstdlib>
#include <iomanip>
#include <fstream>
#include <string>
#include <cmath>
#include <vector>
#include <algorithm>

using namespace std;

//Main Program
int main()
{
	ifstream inFile;
	ofstream outputFile;

	double curr1, curr2, curr3, curr4, curr5;
	double power1, power2, power3, power4, power5;
	double resistance[5] = { 16, 27, 39, 56, 81 };  //    Array of size 5

	outputFile.open("C:\\current.txt");
	if (!outputFile) 
	{
		cout << "Unable to create file current.txt\n";
		return 1;
	}

	cout << "Please enter 5 integer values for the current." << endl;
	cin >> curr1 >> curr2 >> curr3 >> curr4 >> curr5;

	// Ask the user to enter 5 valuse write on the file
	outputFile << curr1 << endl;
	outputFile << curr2 << endl;
	outputFile << curr3 << endl;
	outputFile << curr4 << endl;
	outputFile << curr5 << endl;

	// Close the file
	outputFile.close();
	cout << "Done.\n";

	// We will open the file here
	inFile.open("C:\\current.txt");

	// Read the integers from the user input from the created file
	// And store them in the variables
	inFile >> curr1;
	inFile >> curr2;
	inFile >> curr3;
	inFile >> curr4;
	inFile >> curr5;

	//  Output of the table.
	cout << endl
		 << endl
		 << "Resistance" << setw(12) << "Current" << setw(14) << "Power"
		 << endl
		 << "-------------------------------------"
		 << endl;
	 
	// This will do the calculations for each power for each current
	power1 = resistance[0] * pow(curr1, 2);
	power2 = resistance[1] * pow(curr2, 2);
	power3 = resistance[2] * pow(curr3, 2);
	power4 = resistance[3] * pow(curr4, 2);
	power5 = resistance[4] * pow(curr5, 2);

	// Here we will display the totals in table form.
	cout << setw(7) << resistance[0] << setw(12) << curr1 << setw(16) << power1 << endl;
	cout << setw(7) << resistance[1] << setw(12) << curr2 << setw(16) << power2 << endl;
	cout << setw(7) << resistance[2] << setw(12) << curr3 << setw(16) << power3 << endl;
	cout << setw(7) << resistance[3] << setw(12) << curr4 << setw(16) << power4 << endl;
	cout << setw(7) << resistance[4] << setw(12) << curr5 << setw(16) << power5 << endl;

	system("pause");
	return 0;
}
Topic archived. No new replies allowed.