Hi all,

I need help turning this:
W = 35.74 + 0.6215t - 35.75 (v0.16) + 0.4275t(v0.16)

into code. I guess the real problem is the how to turn v to the .16th power using this function:

double pow(double x, double y)

I know how to open a file with c++ but not how to read a file with numbers and spaces between.

like:
while (theFile >> number)
{

reads the numbers it doesn't read the number after white space. How can I code it to do so?

Thanks!

There is lots of on-line help for the pow function -- such as this one.

``````while (theFile >> number)
{
cout << number << "\n";
}``````

that will read every number in the file, assuming the file contains no other strings. The program will store evey number read into the same variable, so you will have to save it somewhere else if you want to use it for something else.

What I'm trying to do:

1. Attempt to open the file. The filename, c:\\windData.txt, should be hardcoded into your program. Note the two slash characters in the file name. If the file cannot be opened, display a message and exit the program.
2. From the file read a temperature and a wind speed. Both values should be stored in variables declared as double. The file is a text file.
3. Calculate the wind chill factor using a programmer written function, and display the result in the form:

For t = temperature from file
and v = wind speed from file
Wind chill index = calculated result degrees Fahrenheit.

4. Repeat these steps until an end of file is encountered.

What I have so far:

``````#include <iostream>
#include <fstream>
#include <string>
#include [itex]

using namespace std;
double pow(double x, double y);
double y = .16;
int main( )
{

// Open the file
ifstream theFile("windData.txt");
cout << "Attempting to open the file" << endl;

if (theFile.good( ) )
{
int number = 0;
int temp = 0;
int windspd = 0;
int count = 0;

cout << "File opened successfully." << endl;
while (theFile >> number)
{
if (count == 0)
{
temp = number;
windspd = number;
}
double pow(double v, double y)
{
double t = temp;
double v = windspd;
double W = 35.74 + 0.6215(t) - 35.75 (v(y)) + 0.4275(t)(v(y))
cout << W << endl;
score += number;
count++;
}
}
}
else
{
cout << "\nCould not open the file...";
exit(1);
}

system("PAUSE");
cin.get();
return 0;
}``````

I now have:

``````#include <iostream>
#include <fstream>
#include <string>
#include <cmath>

using namespace std;
double pow(double x, double y);
double y = .16;
int main( )
{

// Open the file
ifstream theFile("windData.txt");
cout << "Attempting to open the file" << endl;

if (theFile.good( ) )
{
double number = 0;
double temp = 0;
double windspd = 0;
double count = 0;

cout << "File opened successfully." << endl;
while (theFile >> number)
{
if (count == 0)
{
temp = number;
windspd = number;
}

double v = windspd;
long W = 35.74 + 0.6215(temp) - 35.75 pow(windspd, y) + 0.4275(temp)pow(windspd, y);
//double W = (35.74 + 0.6215(t)) - 35.75 (pow(v, y)) + 0.4275(t)(pow(v, y));
cout << W << endl;

temp += number;
count++;

}

}
else
{
cout << "\nCould not open the file...";
exit(1);
}

system("PAUSE");
cin.get();
return 0;
}``````

I get this error:
error: `6.21500000000000052402526762307388707995414733887e-1' cannot be used as a function
projo6gab.cpp:36: error: expected `,' or `;' before "pow"
projo6gab.cpp:35: warning: unused variable 'v'

which is formula line. Not sure how to proceed.

You can't program an exuation using math format. You have to use computer format. `a(2+x)` must be written as `a[B]*[/B](2+x)` . You must explicitly tell the program to multiply with *.

And be sure to put parentheses around every two terms. Don't assume `a + b * 3` is the same as `(a + b) * 3` -- it isn't.

Thanks I got it to work, but I'm not reading in the data from the file correctly. It looks like:

``````5   20
5   30
5   40``````

So I should be reading in only 3 lines for 3 calculations. The 5 is the temp, and the next nmber is the wind speed. Instead, it reads 6 numbers and makes 6 calculations :/

Thanks I got it to work, but I'm not reading in the data from the file correctly. It looks like:

``````5   20
5   30
5   40``````

So I should be reading in only 3 lines for 3 calculations. The 5 is the temp, and the next nmber is the wind speed. Instead, it reads 6 numbers and makes 6 calculations :/

Well, we don't know what your code looks like that reads the file (I assume you've changed your code), there's not much we can suggest. Maybe you can post that section.

you can read it liike this

``````while (theFile >> temp >> windspeed)
{
// blabla
}``````

>>I should be reading in only 3 lines for 3 calculations

C++ does allow you to read in a whole line at once, including all spaces and other whitespace characters, delimiting on any given char---which is the newline char by default, though you can make it any char you want. The functions/methods to do that are the two versions of getline(). There is one version of getline() for Cstyle strings and another for STL strings.

As you might expect, there are pros and cons to using these functions. On the one hand, they allow you to read in entire lines/paragraphs/manuscripts/whatever including every char therein, except the terminating char (you can even read in a single char if you want). On the other hand, the input must be placed in a string. This means you would then need to parse the string and convert the different tokens within the string into whatever data type you would like, which is more work than serial calls to the >> operator.

you can read it liike this

``````while (theFile >> temp >> windspeed)
{
// blabla
}``````

Thanks all for the help. I am almost there. Something's wrong with my calculation. I should be getting values of -15.etc -19.etc -21.etc, so something's wrong with my equation. Can someone straighten it out for me?

``````#include <iostream>
#include <fstream>
#include <string>
#include <cmath>

using namespace std;
double pow(double x, double y);
double y = .16;
int main( )
{

// Open the file
ifstream theFile("windData.txt");
cout << "Attempting to open the file" << endl;

if (theFile.good( ) )
{

double temp = 0;
double windspd = 0;
double count = 0;

cout << "File opened successfully." << endl;
while (theFile >> temp >> windspd)
{

cout << "For a temperature in degrees F = " << temp << endl;
cout << "and a wind velocity in mph = " << windspd << endl;

double v = windspd;
double W = (35.74 + 0.6215 * temp) - (35.75 * pow(windspd, y) + 0.4275 * temp * pow(windspd, y));
//double W = (35.74 + 0.6215(t)) - 35.75 (pow(v, y)) + 0.4275(t)(pow(v, y));
cout << W << endl;

numbers += number;
count++;

}

}
else
{
cout << "\nCould not open the file...";
exit(1);
}

system("PAUSE");
cin.get();
return 0;
}``````

The equation is: W = 35.74 + 0.6215t - 35.75 (v0.16) + 0.4275t(v0.16)
I am supposed to use the pow(double x, double y) function. Is it right?

In c++ operators are performed in order of precedence so any * in a statement will get done before any + or - in a statement. This may or may not be the sequence you want in your equation, so make your way explicit by grouping each binary operator with the two operands you want. If you don't understand this create a small test program to run the following:

``````int x;

x = (2+3) * 4;
cout << x << endl;        //x will be 20
x = 2 + (3 * 4);
cout  << x << endl;       //x will be 14
x = 2 + 3 * 4;
cout << x << endl;        //run the program to find out what x will be.``````

Figured it out, :D Thanks to all !!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.