Function help [modified]
-
updated code again, I think my function isn't working because when i return the value it doesnt look like its changed to celsius. Also, i only know how to print the value of myarray[0], how do i print each value entered? Thanks for the help. Code: // Computer Lab test.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include using namespace std; double c; double CheckCent(double c) { if (c>30) { return c - 32 /1.8; } return c; } // call this function to convert fahrenheit values into celsius (function name is CheckCent) int _tmain(int argc, _TCHAR* argv[]) { double myarray[51]; //double i; int flag; flag=1; int i; for(i = 0; i < 51 && flag == 1; i++) { // Loop to enter temperatures - pressing 1 continues, pressing 0 stops // cout << "Enter Temperature Reading:"; cin >>myarray[i]; cout << "Enter 1 to input more values, 0 to stop"; cin >>flag; }// End for cout << "Number of elements in the array is: " << i; while(i < 50) { myarray[i]=CheckCent(myarray[i]); cout <<"\n"; cout << myarray[0]; return 0; } }
-
updated code again, I think my function isn't working because when i return the value it doesnt look like its changed to celsius. Also, i only know how to print the value of myarray[0], how do i print each value entered? Thanks for the help. Code: // Computer Lab test.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include using namespace std; double c; double CheckCent(double c) { if (c>30) { return c - 32 /1.8; } return c; } // call this function to convert fahrenheit values into celsius (function name is CheckCent) int _tmain(int argc, _TCHAR* argv[]) { double myarray[51]; //double i; int flag; flag=1; int i; for(i = 0; i < 51 && flag == 1; i++) { // Loop to enter temperatures - pressing 1 continues, pressing 0 stops // cout << "Enter Temperature Reading:"; cin >>myarray[i]; cout << "Enter 1 to input more values, 0 to stop"; cin >>flag; }// End for cout << "Number of elements in the array is: " << i; while(i < 50) { myarray[i]=CheckCent(myarray[i]); cout <<"\n"; cout << myarray[0]; return 0; } }
You have the Source Code in Hand! Why not load it in your debugger and find out Where and Why it does not work. I am afraid that the reality is that writing code involves debugging. actually, it is an integral part of it. of all the members on this forum, I guess that few of the experts regularly write code that always compiles. Fewer write code that immediately performs as advertised. Those that do are continually worried about what their code 'might' do under adverse conditions. Unfortunately Debugging is part and parcel of writing code. This forum is NOT about doing the Debugging for you.
LateNightsInNewry
-
You have the Source Code in Hand! Why not load it in your debugger and find out Where and Why it does not work. I am afraid that the reality is that writing code involves debugging. actually, it is an integral part of it. of all the members on this forum, I guess that few of the experts regularly write code that always compiles. Fewer write code that immediately performs as advertised. Those that do are continually worried about what their code 'might' do under adverse conditions. Unfortunately Debugging is part and parcel of writing code. This forum is NOT about doing the Debugging for you.
LateNightsInNewry
Is this better? I kinda figured out how to use the function, but now I think thats where my problem lies. When i choose say 35 as the first value inputted, and then stop the loop, i dont get a correct celsius value but a number like 14.2323.2323. Here is my updated code, thanks. [code] // Computer Lab test.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include using namespace std; // call this function to convert fahrenheit values into celsius (function name is CheckCent) double CheckCent(double c) { if (c <= 30) return c; c=c-32/1.8; return c; } // main function int _tmain(int argc, _TCHAR* argv[]) { double myarray[51]; //double i; int flag; flag=1; int i; for(i = 0; i < 51 && flag == 1; i++) { // Loop to enter temperatures - pressing 1 continues, pressing 0 stops // cout << "Enter Temperature Reading:"; cin >>myarray[i]; cout << "Enter 1 to input more values, 0 to stop"; cin >>flag; }// End for cout << "Number of elements in the array is: " << i; cout <<"\n"; for(int j=0;j<=i;j++) { myarray[j]=CheckCent(myarray[j]); cout << myarray[0]; } return 0; } [/code]
-
Is this better? I kinda figured out how to use the function, but now I think thats where my problem lies. When i choose say 35 as the first value inputted, and then stop the loop, i dont get a correct celsius value but a number like 14.2323.2323. Here is my updated code, thanks. [code] // Computer Lab test.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include using namespace std; // call this function to convert fahrenheit values into celsius (function name is CheckCent) double CheckCent(double c) { if (c <= 30) return c; c=c-32/1.8; return c; } // main function int _tmain(int argc, _TCHAR* argv[]) { double myarray[51]; //double i; int flag; flag=1; int i; for(i = 0; i < 51 && flag == 1; i++) { // Loop to enter temperatures - pressing 1 continues, pressing 0 stops // cout << "Enter Temperature Reading:"; cin >>myarray[i]; cout << "Enter 1 to input more values, 0 to stop"; cin >>flag; }// End for cout << "Number of elements in the array is: " << i; cout <<"\n"; for(int j=0;j<=i;j++) { myarray[j]=CheckCent(myarray[j]); cout << myarray[0]; } return 0; } [/code]
-
Here's my function :
double FahrenheitToCelsius( double reading ) { return ( reading - 32.0 ) / 1.8; }
That's what I would have written too, but, This member appears not to know how to debug, or, that debugging is an integral part of code developmpent.
LateNightsInNewry