3

I read somewhere that I need to install 'build essential packager' & so I tried:

sudo apt-get install build-essential 
Reading package lists... Done
Building dependency tree       
Reading state information... Done
build-essential is already the newest version.
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.

But the file still wont compile or run...

gcc -Wall -W -Werror factorial.cpp -o factorial.

gives me:

gcc -Wall -W -Werror factorial.cpp -o factorial.
factorial.cpp:3:22: fatal error: iostream.h: No such file or directory
compilation terminated

This is my piece of code: //WAP to demonstrate static member for calculating factorial

    #include<iostream.h>    
    #include<conio.h>
class fact
{
int i;
    static int count;
    public : 
    void calculate()
    {
        long int fact1=1;
        count++;
        for(i=0;i<=count;i++)
        {
            fact1=fact1*i;
        }
        cout<<"\nFactorial of"<<count<<"="<<fact1<<"\n";
    }
};
int fact :: count;
void main()
{
    int i;
    clrscr();
    fact f;
    for(i=1;i<=15;i++)
    {
        f.calculate();
    }
    getch();
}

What shoud I do..???

Jorge Castro
  • 71,754
Roshan
  • 1,203
  • 5
    This is a programming question (with lots of programming errors) and so should be asked on Stack Overflow. The only platform specific bit is the use of conio which isn't available for Ubuntu (or most other Unix like platforms). – dv3500ea Jul 16 '11 at 17:50

3 Answers3

5

There are several issues with your test source package.

My guess is that you are trying to compile using slightly older C++ standards (gcc instead of g++) and probably based on a Windows routine (using conio).

I've tidied up the test program for you:

#include <iostream> /* dont need .h */    
using namespace std; /* use a namespace */
/* #include <conio.h>   this is a windows header - dont need */

class fact
{
int i;
    static int count;
    public : 
    void calculate()
    {
        long int fact1=1;
        count++;
        for (i = 2; i <= count; i++)
        {
            fact1 *= i;
        }
        cout << "\nFactorial of " << count << '=' << fact1 << '\n';
    }
};
int fact :: count;

int main(void) /* you had an invalid main declaration */
{
    int i;
    /* clrscr();  not necessary */
    fact f;
    for (i = 1; i <= 15; i++)
    {
        f.calculate();
    }
    /* getch(); not necessary */

    return 0; /* need to return a standard value */
}

then compile using

g++ factorial.cpp -o factorial
David Foerster
  • 36,264
  • 56
  • 94
  • 147
fossfreedom
  • 172,746
  • 2
    The calculate loop also needs to start at i=1 or the result will always be 0. – Maxime R. Jul 16 '11 at 18:02
  • 2
    Good comment - there are many issues with this source - very inefficient etc. My main aim for the answer was just to get this thing to compile! – fossfreedom Jul 16 '11 at 18:09
0

Try: #include <iostream>

https://stackoverflow.com/questions/214230/iostream-vs-iostream-h-vs-iostream-h

Maxime R.
  • 3,820
-1

Try another one just to test because it says No such file or directory. Or maybe the source file is corrupted.

dv3500ea
  • 37,204
Jwtiyar
  • 27