Help Needed

This is a discussion on Help Needed within the C Programming forums, part of the General Programming Boards category; The progam is to count the number of lines, when i punch hash the program doesnt terminate, if anybody can ...

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    112

    Help Needed

    The progam is to count the number of lines, when i punch hash the program doesnt terminate, if anybody can point put my mistake.
    Code:
    #include<stdio.h>
    
    #include<conio.h>
    
    void main()
    
    {
    
     int c, n1;
     clrscr();  
     n1=0;
     
    while ((c==getchar()) != '#' )
     if (c=='\n');
     ++n1;
     printf("%d\n",n1);
     getch();
    
    }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,592
    Well there's the whole 'void main' thing, see the FAQ (and other things)

    > while ((c==getchar()) != '#' )
    How many comparisons?

    > if (c=='\n');
    How many statements does this if statement guard?
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Making mistakes
    Join Date
    Dec 2008
    Posts
    476
    There are some small mistakes. You have many syntax errors.
    EDIT: Salem was faster. Look at his avatar, it will help

    Code:
    #include<stdio.h>
    #include<conio.h>
    
    int main()                                       /* It's int! Arrggh! */
    { 
         int c, n1 = 0;                              /* Why not initialize n1 to zero? */
         clrscr();
     
        while ((c = getchar()) != '#' && c != EOF)   /* ASSIGN c to getchar() and also stop at end of input */
            if (c == '\n')                           /* No semicolon here */
               ++n1;
    
        printf("%d\n",n1);
        getch();
        return 0;                                    /* Here, see why it's int? */
    }
    return 0 from main means everything is done, there were no severe errors. Anything else means the program didn't do its jobproperly (because of a bug, environment etc.)
    Last edited by Brafil; 05-17-2009 at 03:24 AM.

  4. #4
    Banned ಠ_ಠ's Avatar
    Join Date
    Mar 2009
    Posts
    687
    Quote Originally Posted by Fatima Rizwan View Post
    Code:
    while ((c==getchar()) != '#' )
    
    
     if (c=='\n');
    use = instead of == in your while statement

    your if statement is terminated by the semicolon and does nothing

    void main is bad


    Edit: awww too slow
    ╔╗╔══╦╗
    ║║║╔╗║║
    ║╚╣╚╝║╚╗
    ╚═╩══╩═╝

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. free needed or not?
    By quantt in forum Linux Programming
    Replies: 3
    Last Post: 06-25-2009, 09:32 AM
  2. C Programmers needed for Direct Hire positions
    By canefan in forum Projects and Job Recruitment
    Replies: 0
    Last Post: 09-24-2008, 11:55 AM
  3. lock needed in this scenario?
    By George2 in forum C# Programming
    Replies: 1
    Last Post: 05-25-2008, 07:22 AM
  4. C++ help needed
    By Enkindu in forum Projects and Job Recruitment
    Replies: 3
    Last Post: 08-31-2004, 11:24 PM
  5. semi-colon - where is it needed
    By kes103 in forum C++ Programming
    Replies: 8
    Last Post: 09-12-2003, 05:24 PM

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