Thread: How come Dev C++ cant run some programs??

  1. #16
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > while ((c = getchar()) != EOF)
    Unfortunately, EOF is a sticky state, so attempting to do
    while( (c = getchar()) != '\n' );
    simply doesn't do what you want it to when you've just pressed ctrl-z to exit your first while loop.

    1. The easy thing to try is to open a console prompt and run the program from there, using say
    cd c:\code
    myprog.exe
    All of the traditional examples (including those in K&R) assume you're working at a command prompt interface.

    2. Use the external pause program to do the wait
    Code:
    int main ( ) {
        // your code here
        system("PAUSE");
        return 0;
    }
    It's not ideal, but gets past the EOF problem.

    3. Fix the EOF problem inside your code.
    Code:
    int main ( ) {
          int c, i, nwhite, nother;
          int ndigit[10];
          
          nwhite = nother = 0;
          for (i = 0; i < 10; ++i)
               ndigit[i] = 0;
               
          while ((c = getchar()) != EOF)
              if (c >= '0' && c <= '9')
                  ++ndigit[c-'0'];
              else if (c == ' ' || c == 'n' || c == '\t')
                       ++nwhite;
              else
                  ++nother;
           
          printf("digits =");
          for (i = 0; i < 10; ++i)
               printf(" %d", ndigit[i]);
          printf(", white space = %d, other = %d\n",
                nwhite, nother);
    
        printf ("Press [Enter] to continue");
        clearerr( stdin );
        while ((c = getchar()) != '\n' && c != EOF);
        return 0;
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  2. #17
    Registered User
    Join Date
    Jun 2004
    Posts
    17
    lol now that i thought about the 2nd part of my question, it was kinda dumb. but yeah thanks alot now i can continue on

  3. #18
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Salem, a
    Code:
    #include <stdio.h>
    might help too.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #19
    C/C++Newbie Antigloss's Avatar
    Join Date
    May 2005
    Posts
    216
    Quote Originally Posted by Salem
    3. Fix the EOF problem inside your code.
    Code:
    int main ( ) {
        /* ... ... */
        printf ("Press [Enter] to continue");
        clearerr( stdin );
        while ((c = getchar()) != '\n' && c != EOF);
        return 0;
    }
    What does this clearerr(stdin) for?
    I found that even without calling clearerr, this program still worked fine

  5. #20
    C/C++Newbie Antigloss's Avatar
    Join Date
    May 2005
    Posts
    216
    Quote Originally Posted by dwks
    Yeah, put a system("pause") or getche() before the return 0:
    Code:
    #include <stdio.h>
    
    int main(void) {
        printf("Hello, World!\n");
        getche();
        return 0;
    }
    With getche(), you can press any key, not just <enter>, to close the console.

    Post your program.
    getche is non-standard function.
    to use it, we should include conoi.h(windows) or ncurse.h(unix)

  6. #21
    Registered User
    Join Date
    Jun 2004
    Posts
    17
    ok i have a new problem....

    the following program is suppose to count blanks tabs and newlines but it only counts newlines on mine

    Code:
    #include <stdio.h>
    
    main()
    {
          int c, nl, nt, nb; /* declaration of char counting variables */
          
          nl = 0;
          nt = 0;
          nb = 0;
          
          while((c = getchar()) != EOF) /* if c isn't the end of file then */
              if (c == '\n') 
                 ++nl;
              if (c == '\t')
                 ++nt;
              else if (c == ' ')
                 ++nb;
          printf(" %d %d %d\n", nl, nt, nb);
    
          getchar();
    }
    argh i dont get it!! i dont think theres any errors but it just doesnt run the way is suppose to....

  7. #22
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    C is not Python

    You don't have a { } block after your while, so only the first if (c == '\n') ++nl; is being executed within the loop. The other if (c == '\t') ++nt; else if (c == ' ') ++nb; are getting executed once when the while loop finishes, and c is now EOF, not '\t' or ' '.

    You want:
    Code:
    while((c = getchar()) != EOF) /* if c isn't the end of file then */
    {
        if (c == '\n') 
            ++nl;
        if (c == '\t')
            ++nt;
        if (c == ' ')
            ++nb;
    }
    Also, you are writing C89 code, because you put main() instead of int main(), so you should put the required return 0; before main finishes.

    If you were compiling it as C99 code, you wouldn't need to put return 0, but then C99 would make using main() without the int illegal.

  8. #23
    Registered User
    Join Date
    Jun 2004
    Posts
    17
    ok now that u told me that whenever theres eof i need to the f6 thing but i dunno if this is an exception but it prints output without f6:

    Code:
    #include <stdio.h>
    
    main()
    {
          int c;
          
          c = 0;
          
          while((c = getchar()) != EOF) {
               putchar(c);
               if(c == ' ' || c == '\t' || c == '\n') /* newlines useless */
                  printf("\n");     
                  }
          getchar();
    }
    it shows the output of c without f6 ... weird......

  9. #24
    Registered User
    Join Date
    Aug 2005
    Posts
    6
    thats because your printf statement is within your while loop this time so it will print each time it goes through the while loop instead of waiting for EOF. In this case when you supply EOF the program simply calls getchar() and then exits since there is no other code after the while loop to execute. Make sense?
    Last edited by ./fsck; 09-14-2005 at 09:24 AM.

  10. #25
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    getche is non-standard function.
    to use it, we should include conoi.h(windows) or ncurse.h(unix)
    Usually I would add that, too, but with his compiler ("dev c++ (i think 4.9.9.2)"), just <stdio.h> works perfectly (at least I think it does; it works with 4.0).

    Code:
    if(c == ' ' || c == '\t' || c == '\n')
    Try the isspace() function in <ctype.h>.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  11. #26
    Registered User
    Join Date
    Jun 2004
    Posts
    17
    alright this books confusion to me just keeps on goin.....

    In this array program:

    Code:
    #include <stdio.h>
    
    main()
    {
          int c, i, nwhite, nother;
          int ndigit[10];
          
          nwhite = nother = 0;
          for (i = 0; i < 10; ++i)
               ndigit[i] = 0;
               
          while ((c = getchar()) != EOF)
              if (c >= '0' && c <= '9')
                  ++ndigit[c-'0'];
              else if (c == ' ' || c == 'n' || c == '\t')
                       ++nwhite;
              else
                  ++nother;
           
          printf("digits =");
          for (i = 0; i < 10; ++i)
               printf(" %d", ndigit[i]);
          printf(", white space = %d, other = %d\n",
                nwhite, nother);
     
          getchar();
    }
    ok the book says that the part that contains " ++ndigit[c-'0'];"
    that that integer expression tells that is a digit of 0 - 9. okkk....
    but how does it show that c is a numeric value of 0 -9 in c-'0' ???
    looks like a subtraction sign to me or something...

  12. #27
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    It is a subtraction sign.

    When you enter '0', '0'-'0' becomes the value 0.
    When you enter '1', '1'-'0' becomes the value 1.
    et cetera.

    The author is using the fact that the characters '0' through '9' are contiguous in the character set.

    Edit:

    If you didn't already know, character constants like '0' are just integer values. On the ASCII character set, '0' is 48, '1' is 49, '2' is 50, '3' is 51, etc. 'A' is 65, 'a' is 97.
    Last edited by cwr; 09-15-2005 at 03:44 AM.

  13. #28
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by Sephiroth
    ok the book says that the part that contains " ++ndigit[c-'0'];"
    that that integer expression tells that is a digit of 0 - 9. okkk....
    but how does it show that c is a numeric value of 0 -9 in c-'0' ???
    looks like a subtraction sign to me or something...
    The ascii-value of '0' is 48 and the acsii-value of '1' is 49.
    So if c ='1' the expression c - '0' actually is 49-48 => 1.
    Kurt
    edit: too late

  14. #29
    Registered User
    Join Date
    Jun 2004
    Posts
    17
    ok wait let me think about it for a sec. Ok so u mean that "[c-'0']"
    means that whatever c is , is going to turn into the value 0????
    but then how come cwr says is a subtraction sign?

    Does the value of c (whatever u inputted) just gets subtracted by '0' which is 48??? I am still kind of confused.

  15. #30
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by Sephiroth
    Does the value of c (whatever u inputted) just gets subtracted by '0' which is 48??? I am still kind of confused.
    I would phrase it this way.
    The value of '0' witch is 48 is subtracted from the value of c.
    Kurt
    Edit: guess that's what you said. ( english somtimes confuses me ).
    Last edited by ZuK; 09-16-2005 at 07:49 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Visual c++ 2005 express edition to run .c files
    By the_fall_guy in forum C Programming
    Replies: 4
    Last Post: 04-05-2007, 12:33 PM
  2. Dev C++ Won't Compile
    By mburt in forum Windows Programming
    Replies: 8
    Last Post: 08-21-2006, 11:14 PM
  3. Running programs - few stupid mistakes
    By Korhedron in forum C++ Programming
    Replies: 14
    Last Post: 03-10-2004, 03:10 PM
  4. Programs to run C applications
    By Mak in forum C Programming
    Replies: 3
    Last Post: 03-01-2004, 12:56 PM
  5. how to compile & run c programs in unix?
    By Unregistere in forum C Programming
    Replies: 2
    Last Post: 10-09-2002, 10:53 PM