A dumb question but a question nevertheless?

So i've just recently started working with c++, and anyhow according to my teacher i have to include the string library (#include <string> ) if i am using string, and i have to use the math library (#include <math.h> )if i use complex math.

well the other day while i was doing a program for a contest

i didn't include the string library or the math library and the code still ran fine.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
using namespace std;

int main(){
    int K,num;
    string Code;
    cin>>K;
    cin>>Code;
    for(int i=0;i<Code.size();i++){
        if((int)Code[i]-64-(3*(i+1)+K)<=0){
            num=26+(int)Code[i]-64-(3*(i+1)+K);
        }else(num=(int)Code[i]-64-(3*(i+1)+K));
        cout<<(char)(num+64);
    }
    return 0;
}



so basically i'm asking do we really have to include the separate libraries, is iostream library enough.
You have to include them.
A particular header might pull in other headers, but that's an internal detail and nothing you are allowed to rely on. It varies between standard library implementations and even different releases of the same implementation.
iostream may include string, but you can't count on that.
Include what you need, when you need it.
Also - you wouldn't need math.h for simply adding and multiplying and simple things like that.

math.h has routines for cosine, sine, power, powers, square roots, etc....
isn't math.h a c library? it should be <cmath> iirc
Yes, you should use <cmath> to wrap the math.h header in the std:: namespace.
I also have it in the global namespace. ¿should this happen?
Not according to the reference:
http://www.cplusplus.com/reference/clibrary/
#include <iostream>
# include <math.h>
# include <string>
# include <stdio.h>
# include <stdlib.h>
# include <MALLOC.H>
# include <TIME.H>

The <...> part simply tells the compiler that that file can be found
where it's usually located on your hard-drive.
You can, actually save those files anywhere you wish, but then you need to say where they are. Compilers don't like to go looking for things.
# include c:\root\stuff\headers\"LIMITS.H" ( or something like this)

If you add 10 or 20 or 40 header files in a bank of #include statements, then it won't hurt or break anything, but may (will) slow down the program from a little to a lot when you run it. -also increases the size. This was a problem when you had to get the program to fit on a 560K floppy disc , but if you have 7 of 3TB drives and 16gb ram, then it isn't so much of a problem. If your program is less than , say, 1000 lines then you're not likely to notice the difference in speed.

If you leave-out the Include header file, then it "May" work.
It may also work only on your computer and when you hand it over to a friend or teacher, it may, just as quickly, stop working !
(because it then can't find everything it needs to run.)

Recommendation: Print a copy of all the common FUNCTIONS and also a copy of all the common Header files (like math.h)
Keep them at-hand, tape them to the PC, try to memorize their names.
- paper is good because then you can make comments, such as *Use this one always, or * use these for sorting programs, etc.
Topic archived. No new replies allowed.