Apr 7, 2013 at 10:17am UTC
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;
}
Apr 7, 2013 at 10:22am UTC
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.
Apr 7, 2013 at 10:32am UTC
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;
}
Apr 7, 2013 at 10:33am UTC
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 Apr 7, 2013 at 10:34am UTC
Apr 7, 2013 at 10:35am UTC
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 Apr 7, 2013 at 10:45am UTC
Apr 7, 2013 at 10:40am UTC
Thank you Mr. Cyberdude and Mr. Wingren
:)
Apr 7, 2013 at 10:44am UTC
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 Apr 7, 2013 at 10:45am UTC
Apr 7, 2013 at 10:47am UTC
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 Apr 7, 2013 at 10:51am UTC
Apr 7, 2013 at 10:53am UTC
THank Mr. Vlad for such an effort :)
Apr 7, 2013 at 10:56am UTC
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 Apr 7, 2013 at 10:57am UTC