Thread: stdio.h and iostream.h

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    7

    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;
            }
    }

  2. #2
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    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.

  3. #3
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    New York
    Posts
    1,905
    The second one doesn't output spaces for
    Code:
    cout<<f<<l;
    Whereas the first one does with
    Code:
    printf("%d %d",f,l);

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    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.

    Code:
    void main(void)
    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.
    Callou collei we'll code the way
    Of prime numbers and pings!

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    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.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  6. #6
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. iostream.h
    By SirCrono6 in forum C++ Programming
    Replies: 4
    Last Post: 02-03-2006, 01:23 AM
  2. Including iostream.h
    By DvdHeijden in forum C++ Programming
    Replies: 7
    Last Post: 01-15-2005, 12:23 PM
  3. Problem during compile (iostream.h error)
    By JoJo in forum C Programming
    Replies: 4
    Last Post: 04-29-2003, 06:58 PM
  4. iostream.h problems
    By UniqueScreenNam in forum C++ Programming
    Replies: 9
    Last Post: 11-11-2002, 12:34 PM
  5. stdio.h vs iostream.h
    By Hiroyuki in forum C Programming
    Replies: 2
    Last Post: 03-13-2002, 09:21 PM