Click here to Skip to main content
14,698,679 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
<pre>



<pre>#include<iostream.h>
#include<conio.h>
 float Celsius(float,float ,float , float ,int);
float Kelvin(float C,float K,float F,float ans,int y);                                     //prototypes
float Fahrenheit(float C,float K,float F,float ans,int y);
int main()
{

int x;
float answer;
cout<<"\t\t\tWElCOME TO TEMPERATURE CALCULATOR";
getch();
cout<<"\n\nSelect your first operator =>";
cout<<"\n1.Kelvin";
cout<<"\n2.Celsius";
cout<<"\n3.Fahrenheit\n\n";
cin>>x;
switch(x)
{
  case 1:float Kelvin(float C,float K,float F,float ans,int y) ;
			break;
  case 2:float Celsius(float C,float K,float F, float ans,int y)   ;
			break;
  case 3:float Fahrenheit(float C,float K,float F,float ans,int y)    ;
			break;
  }
 return 0 ;
  }                                                                                         //end of main
//------------------------------------------------------------------------------------------------------------------------------------------
float Kelvin(float C,float K,float F, float ans,int y)
{
cout<<"\nSelect your second operator =>";
cout<<"\n1.Celsius";
cout<<"\n2.Fahrenheit";
cin>>y;
switch(y)
 {case 1:cout<<"Enter Kelvin";
			cin>>K;
			C=K-273.15;
			ans=C;
			break;
  case 2:cout<<"Enter Kelvin" ;
			cin>>K;
			F=K*9/5-459.67;
			ans=F;
			break;
  }
  cout<<"your answer is "<<ans;
  return (0);
}


float Celsius(float C,float K,float F, float ans,int y)
{
cout<<"Select your second operator =>";
cout<<"\n1.Kelvin";
cout<<"\n2.Fahrenheit";
cin>>y;
switch(y)
 {case 1:cout<<"Enter Celsius";
			cin>>C;
			K=C+273.15;
			ans=K;
			break;
  case 2:cout<<"Enter Celsius" ;
			cin>>C;
			F=C*9/5+32;
			ans=F;
			break;
  }
  return ans;
}



float Fahrenheit(float C,float K,float F,float ans,int y)
{
cout<<"Select your second operator =>";
cout<<"\n1.Kelvin";
cout<<"\n2.Celsius";
cin>>y;
switch(y)
 {case 1:cout<<"Enter Fahrenheit";
			cin>>F;
			K=(F+459.67)*5/9;
			ans=K;
			break;
  case 2:cout<<"Enter Fahrenheit";
			cin>>F;
			C=(F-32)*5/9;
			ans=C;
			break;
  }
return ans;
}



the error is in 21st 23rd and 25th line inside the switch
in the function calls
i am compiling using turboc++ 4.5
i will copypaste the errorsbelow:
error FUNC.CPP 21:Expression syntax in function main()
error FUNC.CPP 23:Expression syntax in function main()
error FUNC.CPP 25:Expression syntax in function main()
i am trying to make a temperature converter using functions and some switches


What I have tried:

//i have tried changing the function calls with almost everything ie parameters and the definition just everything buti didnot ask my teacher although i tried looking into the book but that doesnt help trying to do this for ovr a week but i cant pls help .can somebody repair the code that will be really helpful


thanks in advance
Posted
Updated 22-Feb-18 11:29am
v3
Comments
jeron1 25-Jan-18 12:32pm
   
Maybe take a look here to get started with function call syntax. Google will show you many references regarding this.C Functions[^]. Never be afraid to ask a teacher no matter what they may say, it's their job.

Obviously you don't understand the basics of function/subroutine calling.
Advice: forget this project for now and learn properly how things works with C/C++.
Find a couple of tutos and follow them completely.
Here is links to references books on C and C++ by the authors of the languages. Note than C is the ancestor of C++, so knowing C is always useful with C++.
The C Programming Language - Wikipedia, the free encyclopedia[^]
https://hassanolity.files.wordpress.com/2013/11/the_c_programming_language_2.pdf[^]
http://www.ime.usp.br/~pf/Kernighan-Ritchie/C-Programming-Ebook.pdf[^]

C++ Programing Language[^]
Quote:
buti didnot ask my teacher

Too bad, explaining you how things works is its job.
   
v2
You cases:
That is just not the way you do function calls - it's how you declare them.

The above is the answer to your stated problem. Look up the uncountably many examples online of how this is done (i.e., it's part of almost any C++ code).

Or the help, showing usage of functions for Turbo C++.

   
v2
You placed the function prototypes in the wrong place. They should be declared outside of any other functions :
//prototypes
float Celsius( float, float , float , float ,int );
float Kelvin( float C, float K, float F, float ans, int y );                                     
float Fahrenheit( float C, float K, float F, float ans, int y );

int main()
{
// rest of code goes here.
}
It is better to have variable names listed in the prototypes and use descriptive names for the variables because most of the time single letter names are not useful.
   
Comments
Richard MacCutchan 27-Jan-18 4:04am
   
Nothing wrong with having prototypes inside a function.
My two cents
#include <iostream>
#include <utility>
#include <map>
#include <string>
using namespace std;

enum Scale
{
  KELVIN,
  CELSIUS,
  FAHRENHEIT,
};

int main()
{
  using Fun = double (*)(double);

  map < pair<Scale, Scale>, Fun > conv;
  map < Scale, string> desc;

  desc[KELVIN] = "Kelvin";
  desc[CELSIUS] = "Celsius";
  desc[ FAHRENHEIT] = "Fahreneit";

  conv[ make_pair(KELVIN, CELSIUS) ] = [](double k){return (k-273.15);};
  conv[ make_pair(KELVIN, FAHRENHEIT)] =  [](double k){return (k-273.15)*9/5 +32;};
  conv[ make_pair(CELSIUS, KELVIN)] = [](double c){return (c+273.15);};
  conv[ make_pair(FAHRENHEIT, KELVIN)] = [](double f){ return (f-32)*5/9+273.15;};

  Scale s[2];
  int choice;

  cout << "choose the first scale (1 Kelvin, 2 Celsius, 3 Fahreneit)" << endl;
  cin >> choice;
  s[0] = static_cast<Scale>(choice-1);

  cout << "choose the second scale (1 Kelvin, 2 Celsius, 3 Fahreneit)" << endl;
  cin >> choice;
  s[1] = static_cast<Scale>(choice-1);

  double t, tc;

  cout << "enter the temperature (" << desc[s[0]] << ")" << endl;
  cin >> t;
  
  if ( s[0] == KELVIN  && s[1] == KELVIN )
  {
    tc = t;
  }
  else if ( s[0] != KELVIN && s[1] != KELVIN )
  {// here we use Kelvin as intermediate scale
    auto f = conv[ make_pair( s[0], KELVIN )];
    auto g = conv[ make_pair( KELVIN, s[1] )];
    tc = g(f(t));
  }
  else
  {
    auto f = conv[ make_pair(s[0], s[1])];
    tc = f(t);
  }

  cout << "the converted temperature (" << desc[s[1]] << ") is " << tc << endl;
}
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900