Sunday, April 28, 2024
 Popular · Latest · Hot · Upcoming
5
rated 0 times [  5] [ 0]  / answers: 1 / hits: 7675  / 1 Year ago, sat, december 10, 2022, 4:27:55

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<<"
Factorial of"<<count<<"="<<fact1<<"
";
}
};
int fact :: count;
void main()
{
int i;
clrscr();
fact f;
for(i=1;i<=15;i++)
{
f.calculate();
}
getch();
}


What shoud I do..???


More From » c++

 Answers
6

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 << "
Factorial of " << count << '=' << fact1 << '
';
}
};
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

[#44205] Saturday, December 10, 2022, 1 Year  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
eballxeye

Total Points: 370
Total Questions: 91
Total Answers: 139

Location: Suriname
Member since Sat, Jan 1, 2022
2 Years ago
;