-
stdio.h and iostream.h
can anyone tell me what the main difference of these two programs is?
when i am trying the ACM problem 100, the first one is accepted but the second one is wrong.
and can you also tell me the difference between "cin, cout" and "scanf, printf"?
code one
Code:
#include <stdio.h>
long n,t,f,l,c=0,m=0;
void main(void){
while (scanf("%d %d",&f,&l)==2){
printf("%d %d",f,l);
if( f>l ){
t = f ;
f = l ;
l = t ;
}
m=0;
for (n=f;n<=l;n++){
t=n;
c=1;
while (t!=1){
if (t%2==1) t=3*t+1;
else t=t/2;
c++;
}
if (c>=m) m=c;
}
printf(" %d\n",m);
}
}
code two
Code:
#include <iostream.h>
long n,t,f,l,c=0,m=0;
void main(void){
while (cin>>f>>l){
cout<<f<<l;
if( f>l ){
t = f ;
f = l ;
l = t ;
}
m=0;
for (n=f;n<=l;n++){
t=n;
c=1;
while (t!=1){
if (t%2==1) t=3*t+1;
else t=t/2;
c++;
}
if (c>=m) m=c;
}
cout<<" "<<m<<endl;
}
}
-
stdio.h is a C header I believe, and should be <cstdio> without the .h . <iostream> should be without the .h also, since the pre standard one is the one with .h and post standard is without.
I don't know what is making wrong, but I haven't looked closely.
Also printf/scanf are the C ways of IO that are usable in C++ and cout/cin are the C++ stream ways of IO. Google it if you want more infomation.
-
The second one doesn't output spaces for
Whereas the first one does with
Code:
printf("%d %d",f,l);
-
In terms of functionality, stdio.h and iostream.h basically achieve the same thing. It doesn't really matter whether you use scanf/printf or cin/cout... as long as you're reasonably consistent.
That's probably the reason the second program is being rejected. Technically, main is supposed to return int -- not void.
The first program is being compiled by a C compiler which is emitting a warning because main is returning void.
The second program is being compiled by a C++ compiler which is emitting an error because main is returning void.
-
I rather think the second one is compiled with VC++ 2003 or newer, which doesn't have iostream.h.
Of course, posting the error would relieve our inner eyes of responsibility.
-
Like cornedbee sugested, iostream.h is pre-1998 standard and MSVC++6.0 is a poor compiler in my view. After the standard was reviewed, they removed the .h from most C++ headers, with the excepton of conio.h which is non-standard anyway and windows.h
Use headers without .h to avoid errors such as these, most modern compilers would at least give a warning