Program will not compile

I am supposed to write a program for my class that calculates the windchill given the wind velocity ad temperature. I must use 4 seperate functions (one that displays instructions, reads in instructions, calculates result, and displays result) but I can not get my program to compile. I have been working for hours but I can not figure it out.

#include <iostream>
#include <cmath>
using namespace std;

void instruct() //function that displays instructions
{
cout << "This program calculates wind chill. " << endl;
}

void readIn(double T, double V) //function that reads in variables
{
cout << "Please enter the wind speed. " << endl;
cin >> V;
cout << "Please enter the temperature. " << endl;
cin >> T;
}

double calcChill(double T, double V) //function that calculates windchill
{
double windChill;
windChill = 35.74 + (0.6125 * T) - (35.75 * V(pow(0.16))) + (0.4275 * T * V(pow(0.16)));
return windChill;
}

void display(double windChill) //function that displays result
{
cout << "The wind chill is " << windChill;
}

int main()
{
instruct(); //calls for instruct function to run
double T, V, windChill;

readIn(T , V); //calls for readIn function to run

windChill = calcChill(T , V); //calls for calcChill function to run

display(windChill); //calls for display function to run
}



Any help is appreciated, thank you
Use pow like this:

windChill = 35.74 + (0.6125 * T) - (35.75 * pow(V, 0.16)) + (0.4275 * T * pow(V, 0.16));

Always initialize your variables:

double T = 0.0, V = 0.0, windChill = 0.0;
1
2
3
4
5
6
7
void readIn(double T, double V) //function that reads in variables
{
    cout << "Please enter the wind speed. " << endl;
    cin >> V;
    cout << "Please enter the temperature. " << endl;
    cin >> T;
}

You're modifying local variables passed by value. This does not affect your T, V defined in main().

https://www.cs.fsu.edu/~myers/c++/notes/references.html
Last edited on
Hello samgoulette,


PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.

Along with the proper indenting it makes it easier to read your code and also easier to respond to your post.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

Hint: You can edit your post, highlight your code and press the <> formatting button. This will not automatically indent your code. That part is up to you.

You can use the preview button at the bottom to see how it looks.

I found the second link to be the most help.



That was fun. Based on the suggestions and a couple of things I did the output I get is:

This program calculates wind chill.

Please enter the wind speed.: 10
Please enter the temperature.: 30

 The wind chill is 21

 Press Enter to continue:

Or

This program calculates wind chill.

Please enter the wind speed.: 10
Please enter the temperature.: 30

 The wind chill is 20.978

 Press Enter to continue:


The number of decimal places can be adjusted.

Andy
My program now works as it should, but now I must only allow the program to run if the temperature is between 0 and 50 degrees and the windspeed must be in between 0 and 100 mph. my code is 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
#include <iostream>
#include <cmath>
using namespace std;

void instruct()
{
  cout << "This program calculates wind chill. " << endl;
}

void readIn(double& T, double& V)
{
  cout << "Please enter the wind speed. " << endl;
  cin >> V;
  cout << "Please enter the temperature. " << endl;
  cin >> T;
}


double calcChill(double T, double V)
{
  double windChill;
  windChill = 35.74 + (0.6125 * T) - (35.75 * pow(V , 0.16)) + (0.4275 * T * pow(V , 0.16));
  return windChill;
}

void display(double windChill, double T, double V)

  if ((V >= 0) && (V <= 100) && (T > 0) && (T < 100))
  {
  cout << "The wind chill is " << windChill << endl;
  }
  else
  {
  cout << "ERROR: invalid data entered. " << endl;


int main()
{
instruct();
double T = 0.0, V = 0.0, windChill = 0.0;

readIn(T , V);

windChill = calcChill(T , V);

display(windChill);


when I go to compile I get this error:

error: expected initializer before ‘if’
if ((V >= 0) && (V <= 100) && (T > 0) && (T < 100))
^~
project4.cpp:38:3: error: expected unqualified-id before ‘else’
else
^~~~

Your display function needs curly braces { } around it.
And you're missing a closing brace on the "else" block (line 32-34).
And your main function does not have a closing brace either, although I assume that was just a copy-paste mishap.
Last edited on
Hello samgoulette,

Working with your program I made some changes for your consideration:
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
int main()
{
    instruct(); //calls for instruct function to run
 
    double temp{}, windSpeed{}, windChill{};

    std::cout << std::fixed << std::setprecision(3);  // <--- (0) to show whole numbers. 1 or more as you like.
    
    readIn(temp, windSpeed); //calls for readIn function to run

    windChill = calcChill(temp, windSpeed); //calls for calcChill function to run

    display(windChill, temp, windSpeed); //calls for display function to run
    //display(calcChill(temp, windSpeed));  // <--- Could be written as. It would eliminate the variable "windChill" and line 54.


    // A fair C++ replacement for "system("pause")". Or a way to pause the program.
    // The next line may not be needed. If you have to press enter to see the prompt it is not needed.
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // <--- Requires header file <limits>.
    std::cout << "\n\n Press Enter to continue: ";
    std::cin.get();

    return 0;  // <--- Not required, but makes a good break point.
}

For my reference I am using the line numbers in my IDE.

For this function there are 3 choices that you can do. I would recommend the last 1, but do what works best for you:
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
double calcChill(double temp, double windSpeed) //function that calculates windchill
{
    //double windChill;

    //windChill = (35.74 + (0.6125 * temp)) - (35.75 * pow(windSpeed, 0.16)) + (0.4275 * temp * pow(windSpeed, 0.16));

    //return windChill;

// ****************************************************

    //double windChill = 35.74 + (0.6125 * temp) - (35.75 * pow(windSpeed, 0.16)) + (0.4275 * temp * pow(windSpeed, 0.16));

    //return windChill;

// ****************************************************
    return  35.74 + (0.6125 * temp) - (35.75 * pow(windSpeed, 0.16)) + (0.4275 * temp * pow(windSpeed, 0.16));
}


In the "display" function I did the if statement this way:

if ((windSpeed < 3) && (windSpeed < 101) && (temp > -1) && (temp < 51))

The minimum wind speed is based on http://www.csgnetwork.com/windchillcalc.html


Wind speeds of less than 5 MPH statute have virtually no wind chill
factor on the old formula and less than 3 MPH statute have little
or no effect on the new formula!


I do not know if the 3 is the best choice because I did see a table where the minimum was 5. Either way at some point the wind speed gets low enough to where the wind speed has no effect on the wind chill.

If you are required to use a wind speed of zero than by all means use that.

Other than the corrections that Ganado has mentioned the program works well.

Andy
my program works great! Now I just need to make it so that the program will run continuously until the user prompts it to stop. Any suggestions on how I can do this?
What about entering windspeed of 0 to exit?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
void readIn(double& T, double& V)
{
  cout << "Please enter the wind speed (0 to exit). " << endl;
  cin >> V;

  if (V != 0.0) {
     cout << "Please enter the temperature. " << endl;
     cin >> T;
  }
}

...

double T = 0.0, V = 0.0;

do {
    readIn(T , V);
    if (V != 0.0)
        display(calcChill(T , V));
} while (V != 0.0);

Last edited on
Topic archived. No new replies allowed.