why a certain line is not being executed

This is a discussion on why a certain line is not being executed within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> int main() { int sum=0; int i; for ( i=1;i<=1000;i++){ if ((i%3==0)||(i%5==0)){ sum=sum+i; printf("%d\n",sum); //the first printf ...

  1. #1
    Banned
    Join Date
    Oct 2008
    Posts
    1,535

    why a certain line is not being executed

    Code:
    #include <stdio.h>
    
    int main() {
    
     int sum=0;
     int i;
     for ( i=1;i<=1000;i++){
    
         if ((i%3==0)||(i%5==0)){
             sum=sum+i;
             printf("%d\n",sum);      //the first printf
         }
     }
    printf("%d\n",sum);   //this line is not being executed  
        return 0;
    }
    the first printf should give me a straight list of all the "i" numbers
    and the last printf which supposed to give me the total sum
    doesnt even executes?

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    It does on my machine...

    I modified the code to print A) on the first printf, and B) on the last one.

    The output I got was:
    Code:
    A) 3
    A) 8
    A) 14
    A) 23
    A) 33
    A) 45
    A) 60
    ....
    A) 230178
    A) 231173
    A) 232169
    A) 233168
    A) 234168
    B) 234168
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    what software do you use??

  4. #4
    Banned
    Join Date
    Oct 2008
    Posts
    1,535

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    That screenshot looks like the first printf isn't happening, but the second one is (since you only have one number and not four hundred and fifty or so). Of course that means that the code you compiled is not the code you posted.

  6. #6
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    when i remove the first one it shows absolutely nothing

    i changed the first printf to

    printf("\t&#37;d\n",sum);

    but nothing changed

    maybe my software is bad

    is there any free c/c++ compiler which doesnt needs sp2 or linux
    ??

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    gcc-mingw is OK and works on (or at least as far back as) Win2K, so shouldn't need any particular XP service pack - of course, I kind of like SP2, but that's a separate issue.

    What compiler are you using now?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    code block 8.02

    it gives me that CMD screen
    its realy weird
    i am used to a console box

    i tried to install gcc-mingw
    but the files i found ends with "tar" or "gz" which are asociated with linux

    ???

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Ok, so if you are using code::blocks, then the actual compiler is gcc-mingw 3.4.2, which shouldn't be much different from the gcc mingw 3.4.5 that I'm using here. So it should work.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    you have this cmd box as output too??

    i think that it because of that cmd box

    even in dos command it showed only the last line

    ??

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I use a standard command prompt "box" from Windows XP (sp2 but I'm sure that makes no difference at all as I have written many programs in Win2K with the same compiler and never had any problem with "lines missing").

    What if you do "myprog > out.txt" and have a look at out.txt?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  12. #12
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i cant use text output

    i need a console box

    what should i do so all the output will be presented on the cmd creen

    plus when i remove the first printf it doesnt give any output
    although the second output should print the total sum

    ???

  13. #13
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by transgalactic2 View Post
    i cant use text output

    i need a console box

    what should i do so all the output will be presented on the cmd creen

    plus when i remove the first printf it doesnt give any output
    although the second output should print the total sum

    ???
    And what exactly are you outputting to the console - hieroglyphs?

    I'm not suggesting you should do this for any other purpose than to show that the output is there.

    You may also want to add "fflush(stdout)" at the end of your main function - that would flush the output to console if needed.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,632
    plus when i remove the first printf it doesnt give any output
    So you are saying that this program:
    Code:
    #include <stdio.h>
    
    int main()
    {
        int sum=0;
        int i;
        for ( i=1;i<=1000;i++)
        {
            if ((i%3==0)||(i%5==0))
            {
                sum=sum+i;
                /*printf("%d\n",sum);*/ /* the first printf */
            }
        }
        printf("%d\n",sum);   /* this line is not being executed */
        return 0;
    }
    gives you no output (instead of 234168) when compiled and run in Code::Blocks?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #15
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    maybe i need to change the settings of the code blocks ??

    apparently if the code works fine in your software
    then the problem is not with the code but with the software running it


    Code:
    #include <stdio.h>
    
    int main() {
    
     int sum=0;
     int i;
     for ( i=1;i<=100;i++){
    
         if ((i&#37;3==0)||(i%5==0)){
             printf("%d\n",sum);
             sum=sum+i;
    
         }
     }
    printf("%d\n",sum);
        return 0;
    }

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Printing Length of Input and the Limited Input
    By dnguyen1022 in forum C Programming
    Replies: 33
    Last Post: 11-29-2008, 03:13 PM
  2. Unreachable code?
    By Leojeen in forum C Programming
    Replies: 15
    Last Post: 09-28-2008, 07:11 PM
  3. Finding carriage returns (\c) in a line
    By JizJizJiz in forum C++ Programming
    Replies: 37
    Last Post: 07-19-2006, 05:44 PM
  4. Adding Line numbers in Word
    By Mister C in forum A Brief History of Cprogramming.com
    Replies: 24
    Last Post: 06-24-2004, 08:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21