Function errors: Copying one function to create another.

  • Context: MHB 
  • Thread starter Thread starter Teh
  • Start date Start date
  • Tags Tags
    Errors Function
Click For Summary
SUMMARY

The discussion focuses on creating a new function, KelvinToCelsius, based on the existing CelsiusToKelvin function in C++. The user initially encounters a scope error due to the function not being declared before its use. The correct implementation involves defining the KelvinToCelsius function to convert Kelvin to Celsius by subtracting 273.15 from the input value. The final code should accurately return the Celsius equivalent when provided with a Kelvin value.

PREREQUISITES
  • Understanding of C++ function declaration and definition
  • Knowledge of basic temperature conversion formulas
  • Familiarity with C++ input/output using iostream
  • Experience with debugging scope-related errors in C++
NEXT STEPS
  • Implement and test the KelvinToCelsius function correctly in C++
  • Learn about function scope and declaration in C++
  • Explore debugging techniques for C++ programs
  • Study temperature conversion algorithms in programming
USEFUL FOR

Programmers, C++ learners, and anyone interested in understanding function creation and debugging in C++.

Teh
Messages
47
Reaction score
0
I am having difficulties understanding this program may someone help me please.

Using the CelsiusToKelvin function as a guide, create a new function, changing the name to KelvinToCelsius, and modifying the function accordingly.
Code:
#include <iostream>
using namespace std;

double CelsiusToKelvin(double valueCelsius) {
   double valueKelvin = 0.0;

   valueKelvin = valueCelsius + 273.15;

   return valueKelvin;
}

/* Your solution goes here  */

/* ^ Your solution goes here ^ */

int main() {
   double valueC = 0.0;
   double valueK = 0.0;

   valueC = 10.0;
   cout << valueC << " C is " << CelsiusToKelvin(valueC) << " K" << endl;

   valueK = 283.15;
   cout << valueK << "  is " << KelvinToCelsius(valueK) << " C" << endl;

   return 0;
}
output: main.cpp: In function ‘int main()’:
main.cpp:24:55: error: ‘KelvinToCelsius’ was not declared in this scope
 
Technology news on Phys.org
Where are you having difficulty? Do you know you need to declare a function

[m]double KelvinToCelsius(double valueKelvin) {. . .}[/m]​

?
 
greg1313 said:
Where are you having difficulty? Do you know you need to declare a function

[m]double KelvinToCelsius(double valueKelvin) {. . .}[/m]​

?
The issue I have was to understand how this works
if i was to change the function to KelvinToCelsius
Code:
double CelsiusToKelvin(double valueCelsius) {
   double valueKelvin = 0.0;

   valueKelvin = valueCelsius + 273.15;

   return valueKelvin;
}

- - - Updated - - -

the said:
The issue I have was to understand how this works
if i was to change the function to KelvinToCelsius
Code:
double CelsiusToKelvin(double valueCelsius) {
   double valueKelvin = 0.0;

   valueKelvin = valueCelsius + 273.15;

   return valueKelvin;
}

This what I got so far and thanks for the help

Code:
double KelvinToCelsius (double valueKelvin) {
   double valueCelsius = 0.0;
   
   valueCelsius = valueCelsius + 273.15;
   
   return valueCelsius;

}
output:

Testing with valueKelvin = 283.15
Expected value: 10
Your value: 273.15
 
If you solve [m]valueKelvin = valueCelsius + 273.15[/m] for [m]valueCelsius[/m], what do you get?
 

Similar threads

  • · Replies 6 ·
Replies
6
Views
12K
  • · Replies 2 ·
Replies
2
Views
9K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
Replies
12
Views
3K
  • · Replies 22 ·
Replies
22
Views
4K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
6
Views
2K
  • · Replies 11 ·
Replies
11
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K