Functions Lab

Need help to see what I did wrong and/or what am I missing


The formula to compute watts generated per day (represented by w)
w=g(〖s/25)〗^3
where g is the generator’s nameplate capacity (maximum wattage, for example, 3000) and s is the average speed of the wind in miles per hour. Note: this formula is only accurate for wind speeds from 6 to 25 miles per hour inclusive. If 25 < s ≤ 55 then the watts generated are just the maximum wattage possible. The application assumes that the turbine shuts down and generates zero watts with wind speeds over 55 miles per hour.
Valid nameplate capacities from the generator range from 300 to 8,000,000 watts, inclusive. For watts out of this range, print the following error messages and don’t calculate watts generated.
Nameplate Capacity, g Error message
g < 0 Nameplate capacity must be greater than zero.
0 <= g < 300 Nameplate capacity must be between 300 and 8,000,000 watts.
8,000,000 < g Nameplate capacity must be less than 8,000,000 watts.
For invalid wind speeds, don’t calculate watts generated by the wind turbine. Instead, print one of the following error messages.
Wind speed, s Error message
s < 0 Wind speed must be greater than zero.
0 <= s < 6 Wind speed is not sufficient to power the generator.
55 < s Wind speeds too high. The turbine is not operating.
This program prints a welcome message then prompts the user for the nameplate capacity. If the user enters 0 for the capacity, the program ends. Otherwise the program checks the validity of the nameplate capacity and either prints an error message or prompts the user for the wind speed. Then the program checks the validity of the wind speed and either prints an error message or calculates and prints . watts generated with exactly one digit to the right of the decimal. If the watts generated are less than 1,000, print out “watts”. Example:
The wind turbine generated 650.9 watts today.
If the watts generated is in the range [1,000, …, 1,000,000), print out “kilowatts”. Example:
The wind turbine generated 2.1 kilowatts today.
If the watts generated is greater than or equal to 1,000,000, print out “megawatts”. Example:
The wind turbine generated 4.3 megawatts today.
The program prints a good-bye message upon completion.
Thank you for using the Wind Turbine Calculator.
You must write and use the following functions:
bool getNameplateCapacity( double& n ): prompts the user for nameplate capacity and puts this value in the pass-by-reference variable. Returns true if the nameplate capacity is not zero (true if the user doesn’t want to exit the program), false otherwise.
bool checkNameplateCapacity( double n ): returns true if n is in the valid range of 300, …, 8,000,000 inclusive. Prints out error message if n is out of range as described above.
bool getAndCheckWindSpeed( double& w ): prompts the user for wind speed and puts this value in the pass-by-reference parameter. Checks this input and prints error message if out of range as described above. Returns false if entered speed is out of range, otherwise returns true.
double calcWattsGenerated( double nameplateCapacity, double windSpeed ): returns calculated watts generated (not kilowatts and not megawatts, but watts)
void printWattsGenerated( double watts ): prints out watts or kilowatts or megawatts generated
Use constants as appropriate.


here is what i have so far, making sure i'm on the right course

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
#include <iostream>
using namespace std;





int main()
{

   double nameplateCapacity;   // max can be generated per day by windmill
   double windSpeed;           // average wind speed in mph per day
   bool goodInput;             // maxWatts or average windspeed error
   double watts;               // watts generated in one particular day


   // set precision for all output
   cout.setf( ios::showpoint | ios::fixed );
   cout.precision( 1 );

   // say hello and get maxWatts for the first time
   cout << "Welcome to the Wind Turbine Calculator.\n";
   while ( getNameplateCapacity( nameplateCapacity ) )
   {
      goodInput = checkNameplateCapacity( nameplateCapacity );

      // if good maxWatts
      if ( goodInput )
      {
         goodInput = getAndCheckWindSpeed( windSpeed );

         // if no error in maxWatts and windSpeed then
         // calculate and print watts generated
         if( goodInput )
         {
            watts = calcWattsGenerated( nameplateCapacity, windSpeed );
            printWattsGenerated( watts );
         } // end if no input error in maxWatts and windSpeed
      }// end if good maxWatts
   } // end while maxWatts < 0

   cout << "Thank you for using the Wind Turbine Calculator.\n";
   return( 0 );
}


Last edited on
You haven't input the windspeed and you haven't written a single one of the functions that you were asked to write. Apart from that it's probably OK.

BTW, a watt is a "rate" unit already (energy per time), so "watts generated per day" is nonsense.
thats what the assignment has it out to be

i've done this so far, am i on the right course ? i'm having a little trouble with the "double calcWattsGenerated)
its asking to returns calculated wats generated (Line 44)

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>
using namespace std;

bool getNameplateCapacity(double &n)
{
    cout << "Nameplate Capacity? \n";
    cin >> n ;
    if (n>=0){
        return true;
    }else{
    return false;
    }
}
bool checkNameplateCapacity (double n)
{

       if ( n > 300 && n<= 8000000)
        return true;
        else if (n <0)
            cout << "Nameplate capacity must be greater than zero\n"<<endl ;
        else if ( n <300 )
        cout << "Nameplate capacity must be between 300 and 8000000 watts\n"<<endl;
        else if ( n > 8000000)
            cout << "Nameplate capacity must be less than 8000000 watts\n"<<endl;
        return false;

}
bool getAndCheckWindSpeed( double &w)
{

   	cout << "\nWind speed?: ";
	cin >> w ; //user inputs  wind speed

		if (w >6 && w <=55)
		 return true;

		else if (w < 0 )
            cout << " Wind speed must be greater than zero.\n"<<endl;
            else if ( 0 <= w < 6 )
                cout << "Wind speed is not sufficient to power the generator.\n"<<endl;
        else if (w > 20)
		cout << "Wind speed too high. Turbine is not operating.\n" <<endl;
}
double calcWattsGenerated ( double n, double w)
{
   calcWattsGenerated= n *(w/25)^3

    }
void printWattsGenerated (double watts)
{

}

int main()
{

   double nameplateCapacity;   // max can be generated per day by windmill
   double windSpeed;           // average wind speed in mph per day
   bool goodInput;             // maxWatts or average windspeed error
   double watts;               // watts generated in one particular day


   // set precision for all output
   cout.setf( ios::showpoint | ios::fixed );
   cout.precision( 1 );

   // say hello and get maxWatts for the first time
   cout << "Welcome to the Wind Turbine Calculator.\n";
   while ( getNameplateCapacity( nameplateCapacity ) )
   {
      goodInput = checkNameplateCapacity( nameplateCapacity );

      // if good maxWatts
      if ( goodInput )
      {
         goodInput = getAndCheckWindSpeed( windSpeed );

         // if no error in maxWatts and windSpeed then
         // calculate and print watts generated
         if( goodInput )
         {
            watts = calcWattsGenerated( nameplateCapacity, windSpeed );
            printWattsGenerated( watts );
         } // end if no input error in maxWatts and windSpeed
      }// end if good maxWatts
   } // end while maxWatts < 0

   cout << "Thank you for using the Wind Turbine Calculator.\n";
   return( 0 );
}

calcWattsGenerated= n *(w/25)^3


^ is not the exponent operator. There is the std::pow function. or just multiply.

The statement also needs semicolon, your compiler should have warned about that.

The value needs to be returned:

return n * std::pow( (w/25.0) ,3);

Remember to #include <cmath> to use the std::pow function.
so something like this

now i'm getting an error saying switch quantity not an integer on line 68

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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include <iostream>
#include <cmath>
using namespace std;

bool getNameplateCapacity(double &n)
{
    cout << "Nameplate Capacity? \n";
    cin >> n ;
    if (n>=0){
        return true;
    }else{
    return false;
    }
}
bool checkNameplateCapacity (double n)
{

       if ( n >= 300 && n<= 8000000)
        return true;
        else if (n <0)
            cout << "Nameplate capacity must be greater than zero\n"<<endl ;
        else if ( n <300 )
        cout << "Nameplate capacity must be between 300 and 8000000 watts\n"<<endl;
        else if ( n > 8000000)
            cout << "Nameplate capacity must be less than 8000000 watts\n"<<endl;
        return false;

}

bool getAndCheckWindSpeed( double &w)
{

   	cout << "\nWind speed?: ";
	cin >> w ; //user inputs  wind speed

		if (w >6 && w <=55)
		 return true;

		else if (w < 0 )
            cout << " Wind speed must be greater than zero.\n"<<endl;
            else if ( w >= 0 && w < 6 )
                cout << "Wind speed is not sufficient to power the generator.\n"<<endl;
        else if (w > 55)
		cout << "Wind speed too high. Turbine is not operating.\n" <<endl;
		return false;
}

double calcWattsGenerated (  double nameplateCapacity, double windSpeed)
{
    double watts;
   return nameplateCapacity * std::pow( (windSpeed/25.0) ,3) ;
    return watts;
}






void printWattsGenerated (double watts)

{
if (watts >=1000 && watts<=1000000) watts = '1';
   else if (watts >=1000000 ) watts = '2';
   else if (watts <=1000 ) watts = '3';


 switch (watts)
   {
       case '1':
       cout <<  "Kilowatts";
       break;
       case '2':
       cout <<  "Megawatts";
       break;
       case '3':
       cout <<  "watts";
       break;

   };
}

int main()
{

   double nameplateCapacity;   // max can be generated per day by windmill
   double windSpeed;           // average wind speed in mph per day
   bool goodInput;             // maxWatts or average windspeed error
   double watts;
              // watts generated in one particular day


   // set precision for all output
   cout.setf( ios::showpoint | ios::fixed );
   cout.precision( 1 );

   // say hello and get maxWatts for the first time
   cout << "Welcome to the Wind Turbine Calculator.\n";
   while ( getNameplateCapacity( nameplateCapacity ) )
   {
      goodInput = checkNameplateCapacity( nameplateCapacity );

      // if good maxWatts
      if ( goodInput )
      {
         goodInput = getAndCheckWindSpeed( windSpeed );

         // if no error in maxWatts and windSpeed then
         // calculate and print watts generated
         if( goodInput )
         {
            watts = calcWattsGenerated( nameplateCapacity, windSpeed );
            printWattsGenerated( watts );
         } // end if no input error in maxWatts and windSpeed
      }// end if good maxWatts
   } // end while maxWatts < 0

   cout << "Thank you for using the Wind Turbine Calculator.\n";
   return( 0 );
}

Last edited on
Remove lines 3 and 5. You don't use the local defined double watts variable, and you don't return the uninitialized value.
removed double watts variable and the return, i'm not getting any calculations ,



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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#include <iostream>
#include <cmath>
using namespace std;

bool getNameplateCapacity(double &n)
{
    cout << "Nameplate Capacity? \n";
    cin >> n ;
    if (n>0){
        return true;
    }else{
    return false;
    }
}
bool checkNameplateCapacity (double n)
{

       if ( n >= 300 && n<= 8000000)
        return true;
        else if (n <0)
            cout << "Nameplate capacity must be greater than zero\n"<<endl ;
        else if ( n <300 )
        cout << "Nameplate capacity must be between 300 and 8000000 watts\n"<<endl;
        else if ( n > 8000000)
            cout << "Nameplate capacity must be less than 8000000 watts\n"<<endl;
}

bool getAndCheckWindSpeed( double &w)
{

   	cout << "\nWind speed?: ";
	cin >> w ; //user inputs  wind speed

		if (w >6 && w <=55)
		 return true;

		else if (w < 0 )
            cout << " Wind speed must be greater than zero.\n"<<endl;
            else if ( w >= 0 && w < 6 )
                cout << "Wind speed is not sufficient to power the generator.\n"<<endl;
        else if (w > 55)
		cout << "Wind speed too high. Turbine is not operating.\n" <<endl;

}

double calcWattsGenerated (  double n, double w )
{

      return n * std::pow( ( w/25.0) ,3) ;
}

void printWattsGenerated (double watts)

{



if (watts >=1000 && watts<=1000000) watts ;

       cout <<  "Kilowatts\n";

   if (watts >=1000000 ) watts ;

       cout <<  "Megawatts\n";
   if (watts <=1000 ) watts  ;

       cout <<  "watts\n";

}

int main()
{

   double nameplateCapacity;   // max can be generated per day by windmill
   double windSpeed;           // average wind speed in mph per day
   bool goodInput;             // maxWatts or average windspeed error
   double watts;
              // watts generated in one particular day


   // set precision for all output
   cout.setf( ios::showpoint | ios::fixed );
   cout.precision( 1 );

   // say hello and get maxWatts for the first time
   cout << "Welcome to the Wind Turbine Calculator.\n";
   while ( getNameplateCapacity( nameplateCapacity ) )
   {
      goodInput = checkNameplateCapacity( nameplateCapacity );

      // if good maxWatts
      if ( goodInput )
      {
         goodInput = getAndCheckWindSpeed( windSpeed );

         // if no error in maxWatts and windSpeed then
         // calculate and print watts generated
         if( goodInput )
         {
            watts = calcWattsGenerated( nameplateCapacity, windSpeed );
            printWattsGenerated( watts );
         } // end if no input error in maxWatts and windSpeed
      }// end if good maxWatts
   } // end while maxWatts < 0

   cout << "Thank you for using the Wind Turbine Calculator.\n";
   return( 0 );
}
Last edited on
Please explain what is happening in the printWattsGenerated function?

As part of that, explain how an if statement works?
its supposed to print out the numerical value replacing the watts with kilo, or mega when necessary
so basically the if else if statement is supposed to run down the line until it meets the correct numerical values ie 100-1000000 is kilowatts , over 1000000 is megawatts , and under 100 is watts
but i'm not getting the numerical value with the calculations , its correctly giving me kilo, mega or watts though.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
double calcWattsGenerated (  double n, double w)
{

       return n * std::pow( ( w/25.0) ,3) ;
}

void printWattsGenerated (double watts)

{

if (watts >=1000 && watts<=1000000)
       cout << calcWattsGenerated <<  "Kilowatts\n";

   else if  (watts >=1000000 )
       cout << calcWattsGenerated <<  "Megawatts\n";
   else if (watts <=1000 )
       cout << calcWattsGenerated <<  "watts\n";

}
You almost have it. Think carefully about what the cout statements are doing. Hint: you are over thinking it.

The compiler should complain those 3 cout statements.
am i missing a cin statement to bring the calcwattsgenerated equation into the void ?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
double calcWattsGenerated (  double n, double w)
{

       return n * std::pow( ( w/25.0) ,3) ;
}

void printWattsGenerated (double watts )
{

if (watts >=1000 && watts<=1000000)
        std::cout << std::setprecision(3) << watts <<  "Kilowatts\n";

   else if  (watts >=1000000 )
       std::cout << std::setprecision(3) << watts <<  "Megawatts\n";
   else if (watts <=1000 )
        std::cout << std::setprecision(3) << watts <<  "watts\n";
}
Do you think the output looks correct?
1000 watts = 1 kilowatt
1000000 watts = 1 megawatt
You need to do this conversion somewhere, it doesn't magically know to do it for you.
Topic archived. No new replies allowed.