Fatal Error in c++ while using local variables [duplicate] - c++

This question already has answers here:
Where to get iostream.h
(3 answers)
Closed 7 years ago.
As am learning cpp tutorial
#include <iostream.h>
using namespace std;
int main()
{
//variable declaration
int a,b;
int c;
//actual initialization
a=10;b=20;
c=a+b;
cout<<c;
return 0;
}
my error
fatal error: iostream.h

Just change <iostream.h> to <iostream>
Reason is that .h header extensions were used for C includes but aren't used for C++ anymore.
In fact, you can actually use C libraries with .h it's just there isn't one for iostream since its C++ exclusive, hence the fatal error.

Related

Global variable and extern [duplicate]

This question already has answers here:
Why must c++ statements be contained within functions?
(3 answers)
Code outside functions
(3 answers)
Can C++ have code in the global scope?
(1 answer)
Closed 1 year ago.
I have this three files
header.h
extern int global_var;
header.cpp
#include "header.h"
int global_var=50;
main.cpp
#include<iostream>
#include "header.h"
using namespace std;
global_var+=100;
int main(){
cout<<global_var;
}
when I run the prog error occurred :
global_var does not name a type
I know when I put global var inside main function work
but I want to know why this problem occurred

how come an undeclared variable is outputting a value [duplicate]

This question already has answers here:
Why is "using namespace std;" considered bad practice?
(41 answers)
Closed 5 years ago.
In my function sumofrange I decided to output an undeclared variable just to learn the different compiler errors in C++. To my surprise, time seems to output 1 even though it is not declared anywhere.
#include <iostream>
#include <cmath>
using namespace std;
int sumOfrange( int lower, int upper){
cout<<time<<endl;
return ((( (pow(upper,2)) + upper) - ((pow(lower,2)) + lower)) / 2);
}
int main(){
cout<<sumOfrange(7,100)<<endl;
return 0;
}
You are outputting the address of a std::time function declared in a <ctime> header. You are also using a using namespace std; statement. Why that should be avoided is explained in this SO post. Depending on the compiler and the platform you might get the hexadecimal output similar to (0x)00DC52E0 if using a VC++ compiler on Windows or a number 1 if using a g++ compiler on Linux.

"gets() was not declared in this scope" error [duplicate]

This question already has answers here:
why g++ shows "gets()" not declared ,even after including <cstdio>
(3 answers)
Closed 3 years ago.
With the following code, I get the "gets() was not declared in this scope" error:
#include <iostream>
#include <string.h>
using namespace std;
int main()
{
// string str[]={"I am a boy"};
string str[20];`
gets(str);
cout<<*str;
return 0;
}
The function std::gets() was deprecated in C++11 and removed completely from C++14.
As gets() is a C style function, so if you need to include it in your c++ code then you need to include the header file called stdio.h and moreover you can only pass a c style string to gets() function not c++ string class.
So after slight modification in your code it becomes:
#include <iostream>
#include <string.h>
#include "stdio.h"
using namespace std;
int main()
{
// string str[]={"I am a boy"};
char str[20];`
gets(str);
printf("%s",str);
return 0;
}

g++ can't find standard C++ library [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
No such file iostream.h when including
Even after naming the source file with .cpp extension. my compiler gives this error, both in command prompt and Codeblocks. How can I fix this issue?
#include <iostream.h>
int main(){
cout<<"Hello World!\n";
return 0;
}
That header doesn't exist in standard C++. It was part of some pre-1990s compilers, but it is certainly not part of C++.
Use #include <iostream> instead. And all the library classes are in the std:: namespace, for ex­am­ple std::cout.
Also, throw away any book or notes that mention the thing you said.
Using standard C++ calling (note that you should use namespace std for cout or add using namespace std;)
#include <iostream>
int main()
{
std::cout<<"Hello World!\n";
return 0;
}
You should be using iostream without the .h.
Early implementations used the .h variants but the standard mandates the more modern style.

fatal error: iostream.h no such file or directory [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
No such file iostream.h when including
Even after naming the source file with .cpp extension. my compiler gives this error, both in command prompt and Codeblocks. How can I fix this issue?
#include <iostream.h>
int main(){
cout<<"Hello World!\n";
return 0;
}
That header doesn't exist in standard C++. It was part of some pre-1990s compilers, but it is certainly not part of C++.
Use #include <iostream> instead. And all the library classes are in the std:: namespace, for ex­am­ple std::cout.
Also, throw away any book or notes that mention the thing you said.
Using standard C++ calling (note that you should use namespace std for cout or add using namespace std;)
#include <iostream>
int main()
{
std::cout<<"Hello World!\n";
return 0;
}
You should be using iostream without the .h.
Early implementations used the .h variants but the standard mandates the more modern style.