Thread: io and for loops??

  1. #1
    Registered User
    Join Date
    May 2006
    Location
    in your head
    Posts
    49

    io and for loops??

    Hi to everyone, I started programming in c cause i have nothing else to do so i studied it using the book "THE C PROGRAMMING LANGUAGE 2nd edition by Brian W. Kerningham and Dennis Ritchie...now the problem is this....

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    main()
    
    {
        int start, end, count;
           
        printf("Input first number\n");
        scanf("%d", &start);
        
        printf("Input second number\n");
        scanf("%d", &end);
        
        for(count = "%d", start; count <= "%d", end; count++)
            
        printf("%d");
       
        system("pause");
        
    }
    all i want is that when i input like for ex. 1 for my start number 10 for my end number it will count 1 2 3 4 5 6...10 but i can't figure it out......help needed

  2. #2
    Registered User wintellect's Avatar
    Join Date
    Mar 2006
    Posts
    24
    you need to declare "d" as an int before you can use it

    And this bit needs to be re-written
    Code:
    for(count = "%d", start; count <= "%d", end; count++)
            
        printf("%d");

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    20
    you should modify your code as:
    Code:
    #include <stdio.h>
    #include <stdlib.h>// not necessary here (no use of any function included in this library) 
    
    int main(void)//main function takes no argument so you should use void*
    
    {
        int start, end, count;
           
        printf("Input first number\n");
        scanf("%d", &start);
        
        printf("Input second number\n");
        scanf("%d", &end);
        
        for(count = start; count <=end; count++)
            printf("%d ",count);
        
       
        return 0;
        
    }

  4. #4
    Registered User
    Join Date
    May 2006
    Location
    in your head
    Posts
    49
    Waahhhhhhhhh why haven't i figured that out????????!!!! but anyway thanks......

  5. #5
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    >>main()
    >>system("pause");

    I somehow doubt that K&R teaches code like that - look up
    another way to keep the console box open and use int main as
    already mentioned.

    >>you need to declare "d" as an int before you can use it

    What? "%d" is a format specifier - its used with printf, scanf and
    variants thereof - it is not a variable - go look up your own
    resources as well wintellect - that shows a severe
    misunderstanding of the basics.

    You could do this with two variables - start and end as follows:

    Code:
        for(; start <=end; start++)
            printf("%d ",start);
    but mass's way works just fine as well.
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

Popular pages Recent additions subscribe to a feed