Why isnt my code working?

Hi there. I am learning C++ by myself and I am currently facing some issues with my code here. I use Dev C++ freeware software to compile my codes and it is giving me the following errors. Please help and Thank you in advance :)

line 5: error: `cout' undeclared (first use this function)
(Each undeclared identifier is reported only once for each function it appears in.)
line 10: error: `cin' undeclared (first use this function)
line 19: error: expected `;' before '{' token
----------

#include <iostream>
int main()
{ int choice ;
double temp,conv_temp ;
cout << "Temperature Conversion Program by Vibhu " << "\n" ;
cout << "Choose one of the following options for conversion" << "\n" ;
cout << "1. Fahrenheit to Celsius" << "\n" ;
cout << "2. Celsius to Fahrenheit" << "\n" ;
cout << "Enter your choice ( 1 or 2) : " ;
cin >> choice;

if (choice == 1) {
cout << "\n" << "Enter temperature value in Fahrenheit : " ;
cin >> temp;
conv_temp = (temp - 32)/1.8;
cout << "Temperature in Celsius is " << conv_temp << "\n" ;
}
else
(choice == 2 ) {
cout << "\n" << "Enter Temperature value in Celsius : " ;
cin >> temp;
conv_temp = (1.8* temp ) + 32;
cout << "The temperature in Fahrenheit is " << conv_temp << "\n" ;
}
return 0;
}
using namespace std;

Should fix your problems?

Don't really know much about namespace but I guess it brings some functions into scope or something.
two new errors have occured now,

line 4: error: expected unqualified-id before '{' token
line 4: error: expected `,' or `;' before '{' token

----

#include <iostream>
int main();
using namespace std;
{ int choice ;
double temp,conv_temp ;
cout << "Temperature Conversion Program by Vibhu " << "\n" ;
cout << "Choose one of the following options for conversion" << "\n" ;
cout << "1. Fahrenheit to Celsius" << "\n" ;
cout << "2. Celsius to Fahrenheit" << "\n" ;
cout << "Enter your choice ( 1 or 2) : " ;
cin >> choice;

if (choice == 1) {
cout << "\n" << "Enter temperature value in Fahrenheit : " ;
cin >> temp;
conv_temp = (temp - 32)/1.8;
cout << "Temperature in Celsius is " << conv_temp << "\n" ;
}
else
(choice == 2 ) ; {
cout << "\n" << "Enter Temperature value in Celsius : " ;
cin >> temp;
conv_temp = (1.8* temp ) + 32;
cout << "The temperature in Fahrenheit is " << conv_temp << "\n" ;
}
return 0;
}
First of all I am using turbo c++. I dont know anything about 1st and 2nd error.
but have found out error in:
line 19: error: expected `;' before '{' token
you could not put (choice == 2 ) in front of the last else statement.
try this:
1
2
3
4
5
6
7
8
9
else if (choice == 2 ) 
 {
cout << "\n" << "Enter Temperature value in Celsius : " ;
cin >> temp;
conv_temp = (1.8* temp ) + 32;
cout << "The temperature in Fahrenheit is " << conv_temp << "\n" ;
}
else
cout<<"invalid choice enter 1 or 2";

cheers,
cyber dude

Last edited on
btw use the code format for showing code (on the right hand side ->)

using namespace std;
has to be either inside the main function or before it.

Atm the compiler is really confused since you have started the main definition but not the function and you try to do the using namespace std;.

So either, inside the function or before it.

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
#include <iostream>

using namespace std; //moved it
int main(){
int choice ;
double temp,conv_temp ;
cout << "Temperature Conversion Program by Vibhu " << "\n" ;
cout << "Choose one of the following options for conversion" << "\n" ;
cout << "1. Fahrenheit to Celsius" << "\n" ;
cout << "2. Celsius to Fahrenheit" << "\n" ;
cout << "Enter your choice ( 1 or 2) : " ;
cin >> choice;

if (choice == 1) {
cout << "\n" << "Enter temperature value in Fahrenheit : " ;
cin >> temp;
conv_temp = (temp - 32)/1.8;
cout << "Temperature in Celsius is " << conv_temp << "\n" ;
}
else
(choice == 2 ) ; {
cout << "\n" << "Enter Temperature value in Celsius : " ;
cin >> temp;
conv_temp = (1.8* temp ) + 32;
cout << "The temperature in Fahrenheit is " << conv_temp << "\n" ;
}
return 0;
}
Last edited on
i checked this code on codepad.org ( http://codepad.org/ebzuLdFO ) and got it it correct.

It said i need to add a semi colon before the curly brace

else // line 18
(choice == 2 ) ; { // this is line 19


but WHY?
i didnt add a semi colon before after the first condition, then why after the 2nd one?

if (choice == 1) { // no semi colon after the parenthesis and before the curly brace

Is this a rule?
Thank you Mr. Cyberdude and Mr. Wingren
:)
Two statements - two errors.:)

1
2
int main();
 using namespace std;


You shall remove the semicolon after main(); and place the open brace.
The correct code will look the following way

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>
 
int main()
{
   using namespace std;

   int choice ;

   cout << "Temperature Conversion Program by Vibhu " << "\n" ;
   cout << "Choose one of the following options for conversion" << "\n" ;
   cout << "1. Fahrenheit to Celsius" << "\n" ;
   cout << "2. Celsius to Fahrenheit" << "\n" ;
   cout << "Enter your choice ( 1 or 2) : " ;

   cin >> choice;

   if ( choice == 1 ) 
   {
      cout << "\n" << "Enter temperature value in Fahrenheit : " ;
      
      double temp;
      cin >> temp;
 
      double conv_temp = ( temp - 32.0 ) / 1.8;

      cout << "Temperature in Celsius is " << conv_temp << endl; 
   }
   else if ( choice == 2 )
   {
      cout << "\n" << "Enter Temperature value in Celsius : " ;

     double temp;
     cin >> temp;
 
      double conv_temp = ( 1.8 * temp ) + 32.0;
      cout << "The temperature in Fahrenheit is " << conv_temp << endl;
   }
   else
   {
      cout << "Error: incorrect menu selection" << endl;
   }

   return 0;
 } 
Last edited on
working :

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
#include <iostream>
using namespace std ;
int main()
{ int choice ;
  double temp,conv_temp ;
  cout << "Temperature Conversion Program by Vibhu " << "\n" ;
  cout << "Choose one of the following options for conversion" << "\n" ;
  cout << "1. Fahrenheit to Celsius" << "\n" ;
  cout << "2. Celsius to Fahrenheit" << "\n" ;
  cout << "Enter your choice ( 1 or 2) : " ;
  cin >> choice;
  
  if (choice == 1) {
      cout << "\n" << "Enter temperature value in Fahrenheit : " ;
      cin >> temp;
      conv_temp = (temp - 32)/1.8;
      cout << "Temperature in Celsius is " << conv_temp << "\n" ; 
      }
      else
      if (choice == 2 ) {
      cout << "\n" << "Enter Temperature value in Celsius : " ;
      cin >> temp;
      conv_temp = (1.8* temp ) + 32;
      cout << "The temperature in Fahrenheit is " << conv_temp << "\n" ;
      }
      else
      cout << "Invalid Entry. Put '1' or '2' only. " ;
  return 0;
  }
Last edited on
THank Mr. Vlad for such an effort :)
Thank you gentlemen for your help. this is my final code :

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
#include <iostream>
using namespace std ;
int main()
{ int choice ;
  double temp,conv_temp ;
  cout << "Temperature Conversion Program by Vibhu " << "\n" ;
  cout << "Choose one of the following options for conversion" << "\n" ;
  cout << "1. Fahrenheit to Celsius" << "\n" ;
  cout << "2. Celsius to Fahrenheit" << "\n" ;
  cout << "Enter your choice ( 1 or 2) : " ;
  cin >> choice;
  
  if (choice == 1) {
      cout << "\n" << "Enter temperature value in Fahrenheit : " ;
      cin >> temp;
      conv_temp = (temp - 32)/1.8;
      cout << "Temperature in Celsius is " << conv_temp << "\n" ; 
      }
      else
      if (choice == 2 ) {
      cout << "\n" << "Enter Temperature value in Celsius : " ;
      cin >> temp;
      conv_temp = (1.8* temp ) + 32;
      cout << "The temperature in Fahrenheit is " << conv_temp << "\n" ;
      }
      else {
      cout << "Invalid Entry. Put '1' or '2' only. " << "\n" ;
      }
      
  system("pause");
  return 0;
  }
Last edited on
Topic archived. No new replies allowed.