C++ Help
Raging Bull
05-18-2003, 02:11 PM
hey guys, any of you know whats wrong with this and y its not wrking.
CHeers, RB
//Exercise 12.4
#include <fstream.h>
#include <iomanip.h>
#include <iostream.h>
double Engine_Speed;
double s[15];
double t[15][10];
int i, j, Throttle;
double Torque(double Engine_Speed, int Throttle)
{
// Looping through speed array until the correct value is found
for (i=0; i<15; i++)
{
// Is the speed array value speed[i] greater than the required speed
if (s[i] > Engine_Speed)
{
// If yes, jump out of the loop
break;
}
if (s[i] > s[14])
i = 14;
}
// Interpolating between values to find the torque at the required speed
//double Torque;
double Torque = (t[i-1][Throttle] + ((t[i][Throttle] - t[i-1][Throttle])*(Engine_Speed - s[i-1]))
/ (s[i] - s[i-1]));
return Torque;
}
// Main program
int main()
{
ifstream Input_File("Torque.dat", ios::in | ios::nocreate);
if (!Input_File)
{
cerr << "Failed " << endl;
}
for (i=0; i<=14; i++) // For each row i (Engine speed)
{
Input_File >> s[i];
for (j=0; j<=9; j++) // For each row j (Throttle setting)
{
Input_File >> t[i][j];
}
}
Input_File.close();
cin >> Throttle;
cin >> Engine_Speed;
Torque(Engine_Speed, Throttle);
cout << "speed " << s[0];
cout << "\n" << "throttle " << t[0][0] << "\n \n \n";
cout << Torque;
return 0;
}
*** Torque.dat File ***
0.0 -1.0 -1.0 -1.0 -1.0 -1.0 -1.0 -1.0 -1.0 -1.0 -1.0
78.5 1.6 13.9 26.1 38.4 50.7 63.0 75.3 87.6 99.9 112.2
104.7 -0.7 12.5 25.7 38.9 52.1 65.2 78.4 91.6 104.8 118.0
130.9 -2.9 11.1 25.1 39.1 53.1 67.1 81.1 95.1 109.1 123.1
157.1 -5.1 9.7 24.5 39.3 54.1 68.9 83.7 98.5 113.3 128.1
209.4 -9.5 6.7 22.9 39.2 55.4 71.6 87.8 104.1 120.3 136.5
261.8 -14.0 3.5 20.9 38.4 55.8 73.3 90.7 108.2 125.6 143.1
314.2 -18.4 0.1 18.5 37.0 55.5 74.0 92.4 110.9 129.4 147.9
366.5 -22.8 -3.5 15.8 35.0 54.3 73.6 92.9 112.2 131.5 150.8
418.9 -27.3 -7.4 12.6 32.5 52.4 72.3 92.2 112.1 132.0 152.0
471.2 -31.7 -11.5 8.8 29.0 49.2 69.5 89.7 109.9 130.2 150.4
523.6 -36.1 -15.6 5.0 25.5 46.1 66.6 87.2 107.7 128.3 148.8
576.0 -40.6 -20.1 0.4 20.8 41.3 61.8 82.3 102.7 123.2 143.7
628.3 -45.0 -24.6 -4.2 16.2 36.6 56.9 77.3 97.7 118.1 138.5
680.7 -49.4 -29.4 -9.4 10.6 30.6 50.6 70.6 90.6 110.6 130.6
CHeers, RB
//Exercise 12.4
#include <fstream.h>
#include <iomanip.h>
#include <iostream.h>
double Engine_Speed;
double s[15];
double t[15][10];
int i, j, Throttle;
double Torque(double Engine_Speed, int Throttle)
{
// Looping through speed array until the correct value is found
for (i=0; i<15; i++)
{
// Is the speed array value speed[i] greater than the required speed
if (s[i] > Engine_Speed)
{
// If yes, jump out of the loop
break;
}
if (s[i] > s[14])
i = 14;
}
// Interpolating between values to find the torque at the required speed
//double Torque;
double Torque = (t[i-1][Throttle] + ((t[i][Throttle] - t[i-1][Throttle])*(Engine_Speed - s[i-1]))
/ (s[i] - s[i-1]));
return Torque;
}
// Main program
int main()
{
ifstream Input_File("Torque.dat", ios::in | ios::nocreate);
if (!Input_File)
{
cerr << "Failed " << endl;
}
for (i=0; i<=14; i++) // For each row i (Engine speed)
{
Input_File >> s[i];
for (j=0; j<=9; j++) // For each row j (Throttle setting)
{
Input_File >> t[i][j];
}
}
Input_File.close();
cin >> Throttle;
cin >> Engine_Speed;
Torque(Engine_Speed, Throttle);
cout << "speed " << s[0];
cout << "\n" << "throttle " << t[0][0] << "\n \n \n";
cout << Torque;
return 0;
}
*** Torque.dat File ***
0.0 -1.0 -1.0 -1.0 -1.0 -1.0 -1.0 -1.0 -1.0 -1.0 -1.0
78.5 1.6 13.9 26.1 38.4 50.7 63.0 75.3 87.6 99.9 112.2
104.7 -0.7 12.5 25.7 38.9 52.1 65.2 78.4 91.6 104.8 118.0
130.9 -2.9 11.1 25.1 39.1 53.1 67.1 81.1 95.1 109.1 123.1
157.1 -5.1 9.7 24.5 39.3 54.1 68.9 83.7 98.5 113.3 128.1
209.4 -9.5 6.7 22.9 39.2 55.4 71.6 87.8 104.1 120.3 136.5
261.8 -14.0 3.5 20.9 38.4 55.8 73.3 90.7 108.2 125.6 143.1
314.2 -18.4 0.1 18.5 37.0 55.5 74.0 92.4 110.9 129.4 147.9
366.5 -22.8 -3.5 15.8 35.0 54.3 73.6 92.9 112.2 131.5 150.8
418.9 -27.3 -7.4 12.6 32.5 52.4 72.3 92.2 112.1 132.0 152.0
471.2 -31.7 -11.5 8.8 29.0 49.2 69.5 89.7 109.9 130.2 150.4
523.6 -36.1 -15.6 5.0 25.5 46.1 66.6 87.2 107.7 128.3 148.8
576.0 -40.6 -20.1 0.4 20.8 41.3 61.8 82.3 102.7 123.2 143.7
628.3 -45.0 -24.6 -4.2 16.2 36.6 56.9 77.3 97.7 118.1 138.5
680.7 -49.4 -29.4 -9.4 10.6 30.6 50.6 70.6 90.6 110.6 130.6
Marco_Wrc
05-18-2003, 03:08 PM
How can we help you if you don't tell us what it should do.
Marco_Wrc
05-18-2003, 03:44 PM
Ok, normally I don't dive into other's code, but I did this time.
You call the function Torque:
Torque(Engine_Speed, Throttle);
Then you want output from that function:
cout << Torque;
But, that doesn't work because:
The function torque returns a double. You need to print that double, not print the function Torque.
So, to fix it you can assign the double to a variable:
double torq = Torque(Engine_Speed, Throttle);
cout << torq;
Or print the output from Torque directly:
cout << Torque(Engine_Speed, Throttle);
If you do it the way you do: cout << Torque; You'll print out the memory address of the function Torque.
You call the function Torque:
Torque(Engine_Speed, Throttle);
Then you want output from that function:
cout << Torque;
But, that doesn't work because:
The function torque returns a double. You need to print that double, not print the function Torque.
So, to fix it you can assign the double to a variable:
double torq = Torque(Engine_Speed, Throttle);
cout << torq;
Or print the output from Torque directly:
cout << Torque(Engine_Speed, Throttle);
If you do it the way you do: cout << Torque; You'll print out the memory address of the function Torque.
Raging Bull
05-18-2003, 05:06 PM
cheers marco, appreciate the help. Something so simple bin given me grief all night. Cheers again, ill remember you!!!
Automotive Network, Inc., Copyright ©2026
