Error compliling/running C++ program

Asked by Bharat B. Sahni on 2009-01-29

Dear/s,

I am trying to run the following C++ program in Ubuntu 8.10,

#include <iostream.h>
#include <iomanip.h>
#include <stdlib.h>
using namespace std;
void c_to_f(void);
void f_to_c(void);

void
main(void)
{
 int choice;
 char again;
 do
 {
  system("CLS");

  cout<<setw(10)<<" "<< "What conversion would you like to make?\n";// menu
  cout<<setw(20)<<" "<< "1. Celsius to Fahrenhei\n\n";
 // make a choice which functions to use.
  cout<<setw(20)<<" "<< "2. Fahrenheit to Celsius\n\n";
  cin>>choice;

   switch(choice)
 // go to chosen function
   {
    case 1:
     {
      c_to_f();
      break;
     }
    case 2:
     {
      f_to_c();
      break;
   }
  default:
  {
   cout<<setw(10)<<" "<< "Enter 1 ot 2 "<< endl;
 // validate and correct input of function choice.

  }

 }

 cout<<setw(10)<<" "<< "Do you wish to do another conversion? y for yes, n for no "; // return loop on y for yes.
 cin>> again;
 }while (again='Y'||again='y';

}

void c_to_f(void)
{

 system("CLS"); //clean screen for function data.
 int temp,fahrenheit;

 cout<< "\n\n\n ";
 cout<<setw(10)<<" "<< "Enter the temperature in whole degress Celsius. \a";
 cin>>temp;

 fahrenheit=((temp*9)/5)+32;
 cout<<endl<<setw(10)<<" "<<temp<<" degrees celsius is " <<fahrenheit\a\n\n\n ";
}

void f_to_c(void)
{
 system("CLS"); // clear screen for function data.
 int temp, celsius;

 cout<< "\n\n\n ";
 cout<<setw(10)<<" "<< "Enter the temperature in whole degrees fahrenheit. \a";
 cin>>temp;
 celsius=((temp-32)*5)/9;

 cout<<endl<<setw(10)<<" "temp<<" degrees fahrenheit is "<<celsius<<" degrees celsius \a\\n\n\n";
}

But when I try to compile/run I get the following errors;

bharat@Bharat-desktop:~/C++Examples$ g++ Temp.cpp -o Temp
Temp.cpp:1:22: error: iostream.h: No such file or directory
Temp.cpp:2:21: error: iomanip.h: No such file or directory
Temp.cpp:64: error: stray ‘\’ in program
Temp.cpp:64: error: stray ‘\’ in program
Temp.cpp:64: error: stray ‘\’ in program
Temp.cpp:64: error: stray ‘\’ in program
Temp.cpp:64:79: warning: missing terminating " character
Temp.cpp:64: error: missing terminating " character
Temp.cpp:9: error: ‘::main’ must return ‘int’
Temp.cpp: In function ‘int main()’:
Temp.cpp:17: error: ‘cout’ was not declared in this scope
Temp.cpp:17: error: ‘setw’ was not declared in this scope
Temp.cpp:21: error: ‘cin’ was not declared in this scope
Temp.cpp:38: error: ‘endl’ was not declared in this scope
Temp.cpp:48: error: lvalue required as left operand of assignment
Temp.cpp:48: error: expected `)' before ‘;’ token
Temp.cpp: In function ‘void c_to_f()’:
Temp.cpp:58: error: ‘cout’ was not declared in this scope
Temp.cpp:59: error: ‘setw’ was not declared in this scope
Temp.cpp:60: error: ‘cin’ was not declared in this scope
Temp.cpp:64: error: ‘endl’ was not declared in this scope
Temp.cpp:64: error: expected `;' before ‘a’
Temp.cpp: In function ‘void f_to_c()’:
Temp.cpp:72: error: ‘cout’ was not declared in this scope
Temp.cpp:73: error: ‘setw’ was not declared in this scope
Temp.cpp:74: error: ‘cin’ was not declared in this scope
Temp.cpp:77: error: ‘endl’ was not declared in this scope
Temp.cpp:77: error: expected `;' before ‘temp’
bharat@Bharat-desktop:~/C++Examples$

Please guide how to resolve/compile/run this program,

Thanking you,

Bharat

Question information

Language:
English Edit question
Status:
Solved
For:
Ubuntu Edit question
Assignee:
No assignee Edit question
Solved by:
Siyan Panayotov
Solved:
2009-01-30
Last query:
2009-01-30
Last reply:
2009-01-29
Best Siyan Panayotov (xsisqox) said : #1

There are several errors which needed to be fixed. Also the function call system("CLS") won't work, because the command CLS is a DOS command! You should use system("clear")! And one more thing - make sure, that your main() function is always type int!

#include <iostream>
#include <iomanip>
#include <stdlib.h>
using namespace std;
void c_to_f(void);
void f_to_c(void);

int main(void)
{
 int choice;
 char again;
 do
 {
  system("CLS");

  cout<<setw(10)<<" "<< "What conversion would you like to make?\n";// menu
  cout<<setw(20)<<" "<< "1. Celsius to Fahrenhei\n\n";
 // make a choice which functions to use.
  cout<<setw(20)<<" "<< "2. Fahrenheit to Celsius\n\n";
  cin>>choice;

   switch(choice)
 // go to chosen function
   {
    case 1:
     {
      c_to_f();
      break;
     }
    case 2:
     {
      f_to_c();
      break;
   }
  default:
  {
   cout<<setw(10)<<" "<< "Enter 1 ot 2 "<< endl;
 // validate and correct input of function choice.

  }

 }

 cout<<setw(10)<<" "<< "Do you wish to do another conversion? y for yes, n for no "; // return loop on y for yes.
 cin>> again;
 }while (again=='Y'||again=='y');
 return 0;
}

void c_to_f(void)
{

 system("CLS"); //clean screen for function data.
 int temp,fahrenheit;

 cout<< "\n\n\n ";
 cout<<setw(10)<<" "<< "Enter the temperature in whole degress Celsius: \a";
 cin>>temp;

 fahrenheit=((temp*9)/5)+32;
 cout<<endl<<setw(10)<<" "<<temp<<" degrees celsius is " <<fahrenheit<<"\a\n\n\n ";
}

void f_to_c(void)
{
 system("CLS"); // clear screen for function data.
 int temp, celsius;

 cout<< "\n\n\n ";
 cout<<setw(10)<<" "<< "Enter the temperature in whole degrees fahrenheit: \a";
 cin>>temp;
 celsius=((temp-32)*5)/9;

 cout<<endl<<setw(10)<<" "<<temp<<" degrees fahrenheit is "<<celsius<<" degrees celsius \a\n\n\n";
}

Thanks,
Siyan,

Yes the problem is solved,

Bharat