Thread: How to exit program???

  1. #1
    Registered User
    Join Date
    Jun 2010
    Location
    127.0.0.1
    Posts
    9

    How to exit program???

    I am working my way through "The C Programming Language".

    I am on the topic of input/output in the first chapter and at the end of the section we are supposed to write a program that counts the spaces, tabs, and new lines. I wrote the program no problem but i can't get the program to exit and give me the output for number of spaces, tabs, and new lines.

    i even tried on of the simpler programs from the book just to make sure it wasn't my logic. it wasn't, i just don't know how to exit the program correctly.

    here is what i have for reference (my program):

    Code:
    #include <stdio.h>
    
    main()
    	{
    	int c;	/* Counting variable */
    	int ns = 0;	/* Number of spaces */
    	int nl = 0; /* Number of lines */
    	int nt = 0;	/* Number of tabs */
    	
    	for ( c = 0; (c = getchar()) != EOF; c++)
    		{
    		if( c == '\t')
    			{
    			nt++;
    			}
    		if( c == '\n')
    			{
    			nl++;
    			}
    		if( c == ' ')
    			{
    			ns++;
    			}		
    		}
    	printf("%d\t%d\t%d\n", ns, nt, nl);	
    	}
    thanks in advance.

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    81
    exit(0); ->SUCCESS_EXIT
    exit(1); ->FAILURE_EXIT

    also there is another option brake; which does not exit a program but brakes a loop (just in case)!
    Last edited by nullifyed; 06-20-2010 at 07:21 PM.

  3. #3
    Registered User jimtuv's Avatar
    Join Date
    May 2010
    Location
    Sylvania, Ohio
    Posts
    94
    you should use

    Code:
    int main (void)
    and at the bottom

    Code:
    return 0;

  4. #4
    Registered User
    Join Date
    Jun 2009
    Posts
    486
    for ( c = 0; (c = getchar()) != EOF; c++)
    Why are you incrementing c? If you are getting something from the input stream each time, the c++ deosn't actually have any effect.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by spottedzebra
    I wrote the program no problem but i can't get the program to exit and give me the output for number of spaces, tabs, and new lines.
    What exactly do you mean by "can't get the program to exit"? Perhaps you find that the command prompt window flashes by too quickly for you to view the output? If so, just run the program from the command prompt, or use getchar() to wait for input so that the window stays open.

    Quote Originally Posted by =nullifyed
    exit(0); ->SUCCESS_EXIT
    exit(1); ->FAILURE_EXIT
    The macros are EXIT_SUCCESS and EXIT_FAILURE. They are implementation defined.

    Quote Originally Posted by nullifyed
    also there is another option brake; which does not exit a program but brakes a loop (just in case)!
    Actually, the keyword is break, though now I am tempted to be funny and #define brake break
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    81
    Quote Originally Posted by laserlight View Post
    What exactly do you mean by "can't get the program to exit"? Perhaps you find that the command prompt window flashes by too quickly for you to view the output? If so, just run the program from the command prompt, or use getchar() to wait for input so that the window stays open.


    The macros are EXIT_SUCCESS and EXIT_FAILURE. They are implementation defined.


    Actually, the keyword is break, though now I am tempted to be funny and #define brake break
    xaxaxaa good!!! sorry...i am not good at english and my head is NOT Ok....

  7. #7
    Registered User
    Join Date
    Jun 2010
    Location
    127.0.0.1
    Posts
    9
    thanks for all the replies so quickly. i am a noob at C so i don't fully understand all of your responses and it would seem to me i wouldn't need to use anything advanced (i.e. not already taught in the book i am using)


    Quote Originally Posted by nullifyed View Post
    exit(0); ->SUCCESS_EXIT
    exit(1); ->FAILURE_EXIT

    also there is another option brake; which does not exit a program but brakes a loop (just in case)!
    i am not sure what this is/means. i have not been introduced to anything like this yet.
    Quote Originally Posted by jimtuv View Post
    you should use

    Code:
    int main (void)
    and at the bottom

    Code:
    return 0;
    same goes for this.

    Quote Originally Posted by KBriggs View Post
    Why are you incrementing c? If you are getting something from the input stream each time, the c++ deosn't actually have any effect.
    me being a noob. thanks for the catch, i took it out.

    Quote Originally Posted by laserlight View Post
    What exactly do you mean by "can't get the program to exit"? Perhaps you find that the command prompt window flashes by too quickly for you to view the output? If so, just run the program from the command prompt, or use getchar() to wait for input so that the window stays open.


    The macros are EXIT_SUCCESS and EXIT_FAILURE. They are implementation defined.


    Actually, the keyword is break, though now I am tempted to be funny and #define brake break
    i executing the program directly in the command prompt so I am not missing it popping up.

    what i mean by not being able to exit the program is this.

    I load the program (running ubuntu and using the preload compiler) :

    Code:
    ./a.out
    then i allows me to input whatever combination of characters i want.

    now i want it to count the spaces, tabs, and lines but i don't know how to make it finish or exit, meaning it prints the output in the command prompt.


    if the printf command is moved inside the 'for' loop it will work but it will have output every time i press enter. the counting will be correct but this is not what the program is supposed to do.

    here is a basic program from the text for reference to what i am learning from: ( i did not write this one )

    Code:
    #include <stdio.h>
    /* count lines in input */
    main()
    {
    int c, nl;
    nl = 0;
    while ((c = getchar()) != EOF)
    if (c == '\n')
    ++nl;
    printf("%d\n", nl);
    }
    thanks again to everyone for the help
    Last edited by spottedzebra; 06-20-2010 at 09:41 PM.

  8. #8
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    For EOF, hit ctrl+d.

  9. #9
    Registered User
    Join Date
    May 2010
    Posts
    81
    Quote Originally Posted by Bayint Naung View Post
    For EOF, hit ctrl+d.
    exactly. just type space tab enter many times and then crt&D....

  10. #10
    Registered User jimtuv's Avatar
    Join Date
    May 2010
    Location
    Sylvania, Ohio
    Posts
    94
    This may explain why you should be using

    Code:
    int main (void){
    
          //your program goes here
    
           return 0;
    }
    Cprogramming.com FAQ > main() / void main() / int main() / int main(void) / int main(int argc, char *argv[])


    I hope it helps you understand a bit more. This is not advanced it's just standard with C99.

  11. #11
    Registered User
    Join Date
    Jun 2010
    Location
    127.0.0.1
    Posts
    9
    Quote Originally Posted by Bayint Naung View Post
    For EOF, hit ctrl+d.
    thats exactly what i needed. my program works perfectly not that it is anything special.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. HELP with Program!
    By afnitti in forum C Programming
    Replies: 9
    Last Post: 04-15-2009, 08:06 PM
  2. exit a program at any point.
    By slightofhand in forum C Programming
    Replies: 5
    Last Post: 03-02-2008, 09:08 AM
  3. Program Terminating With Error On Exit
    By chriscolden in forum C Programming
    Replies: 19
    Last Post: 01-14-2006, 04:40 AM
  4. Program uses a lot of memory and doesnt exit properly
    By TJJ in forum Windows Programming
    Replies: 13
    Last Post: 04-28-2004, 03:13 AM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM