Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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..???

share|improve this question
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
add comment

3 Answers 3

up vote 5 down vote accepted

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=0;i<=count;i++)
        {
            fact1=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
share|improve this answer
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
add comment

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

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.