// Includes
#include "stdafx.h"
#include <iostream>
#include <conio.h>
usingnamespace std;
// Prototypes
double farhenheit();
// Farhenheit Table Function
double farhenheit()
{
int x;
double conversion;
cout << "F \t C" << endl;
cout << "_______________" << endl;
for ( x = 0 ; x <= 20 ; x++ )
{
conversion = x * -17.22;
cout << x << " \t " << endl;
}
_getch();
return conversion;
}
// Main Function
int main()
{
cout << "This will automatically display a farhenheit to celcius table from 0-20.\n\n";
farhenheit();
_getch();
return 0;
}
Don't worry this isn't homework, im teaching myself c++ more in depth due to the fact i only took a year of it in school.
The exercise from the ebook reads:
The formula for converting a temperature from Farhenheit to Celcius is where F is the Farhenheit temperature and C is the Celcius temperature. Write a function that accepts a farhenheit temperature as and argument. The function should return the temperature, converted to celcius. Demonstrate the function bby calling it in a loop that displays a table of the ferhenheit temperatures 0-20 and their celcius equivalents.
I want to make sure i am doing this right, can anyone look and see please. I have a feeling im not even close to doing this right.
In the exercise there is written "Write a function that accepts a farhenheit temperature as and argument. The function should return the temperature, converted to celcius". So you should write a function that accepts a Farhenheit temperature and returns a corresponding Celcius. Your function shall not contain any other code except this calculation.
Write a function that accepts a farhenheit temperature as and argument.
Do you know what they mean by argument?
The function declared below does not have an argument.
1 2
// Prototypes
double farhenheit();
This is not correct: conversion = x * -17.22;
I would move everything that you have in your function (except the return statement) in to main. And have your function just do the proper conversion. Call your function in the loop.
// Includes
#include "stdafx.h"
#include <iostream>
#include <conio.h>
usingnamespace std;
// Prototypes
int celcius(int , int);
// Farhenheit Table Function
int celcius(int x, int conversion)
{
for ( x = 0 ; x <= 20 ; x++)
{
conversion = x - 32 * ( 5 / 9 );
}
return conversion;
}
// Main Function
int main()
{
cout << "This will display the farhenheit to celcius conversion table from 0-20.\n\n";
for ( int x = 0 ; x <= 20 ; x++ )
{
cout << x << " \t " << celcius << endl;
}
_getch();
return 0;
}
ok heres my new code everything works except all the celcius temperatures say
Well im not like new new to programming its just usually i dont do like ex. return conversion, usually i dont do anything like that bu i got to learn it. but thank you
lol i mean the return part confused me, i know functions, just not whena have a return value of like my program its return conversion, usually i do like return 1 or return 0. also usually i dont make any args usually i just user int celcius() or void celcius(), not int celcius (int x, int conversion)
Well, I would expect the warning about the conversion. You can safely ignore it in this case if you don't want the decimal places showing and don't mind a bit of a rounding error.
What is the for loop doing in the celcius function?