Thread: C Programming Language Exercise

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    20

    C Programming Language Exercise

    Hey guys, I'm new to this forum and the C programming language. For some reason, I can't get this relatively simple program to print out the number of tabs, blanks and newlines I have in the input. Anybody have any ideas as to what's wrong?

    Code:
    #include "stdafx.h"
    #include <stdio.h>
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    	int c=0;
    	int nl=0;
    	int b=0;
    	int t=0;
    	while((c=getchar())!=EOF){
    		if (c== '\n')
    			nl++;
    		if (c==' ')
    			b++;
    		if (c=='\t')
    			t++;
    	}
    	printf("Newlines = %d Blanks = %d Tabs = %d", nl,b,t);
    }

  2. #2
    Make Fortran great again
    Join Date
    Sep 2009
    Posts
    1,413
    int _tmain? Shouldn't that be int main?

    Also, you're missing a return statement. Elysia or laserlight will come in and correct me but I think it may be an implicit return 0 by the C99 standard, but it's still good to have the return statement there anyway.

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Worked for me, altho I removed "stdafx.h" and used int main():

    Code:
    [root~/C] ./a.out
    asdagss
    as		
    	asdf[Ctrl-D]
    Newlines = 3 Blanks = 2 Tabs = 3
    Last edited by MK27; 02-20-2010 at 09:51 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    Registered User
    Join Date
    Feb 2010
    Posts
    20
    The reason for the int _tmain and "stdafx.h" is because I copy pasted it directly from Visual C++, and that seems to be it's convention of setting up the code and precompiled headers. I might be doing something really stupid, but when the command prompt comes up and I put in say, a tab, the prompt just jumps a new line, expecting more input (without outputting anything). Any ideas?

  5. #5
    Registered User
    Join Date
    Feb 2010
    Posts
    20
    I inserted the return 0; statement by the way, alas to no avail.

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by serg_yegi View Post
    The reason for the int _tmain and "stdafx.h" is because I copy pasted it directly from Visual C++, and that seems to be it's convention of setting up the code and precompiled headers.
    You copied it from what? Anyway, forget that. Just use:
    Code:
    int main(int argc, char *argv[])
    without the wacky header.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  7. #7
    Registered User
    Join Date
    Feb 2010
    Posts
    20
    Visual C++ is a GUI and compiler from Microsoft. I changed what you asked to no avail.

  8. #8
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    I know what Visual C++ is, I was kind of questioning the veracity of what you are saying (but since I don't use VS, I can't say any more).

    Anyway: do you know how to signal EOF in the terminal? That's what the [Ctrl-D] indicated in my post. Witness, your loop will not end until then:
    Code:
    	while((c=getchar())!=EOF){
    You could simplify by using:
    Code:
    while((c=getchar())!='Q'){
    So this will end when a capital Q is encountered. However, the input will not get processed until you hit return after the Q.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  9. #9
    Registered User
    Join Date
    Mar 2007
    Posts
    416
    VS likes to use unicode character sets. Changing the code to the following works with codeblocks 8.02 (mingw compiler).
    Code:
    #include <stdio.h>
    
    int main(int argc, char* argv[])
    {
    	int c=0;
    	int nl=0;
    	int b=0;
    	int t=0;
    	while((c=getchar())!=EOF){
    		if (c== '\n')
    			nl++;
    		if (c==' ')
    			b++;
    		if (c=='\t')
    			t++;
    	}
    	printf("Newlines = %d Blanks = %d Tabs = %d", nl,b,t);
    }
    Better yet, write your code like this... (style points, ya know )
    Code:
    #include <stdio.h>
    #include <conio.h>
    
    int main(int argc, char* argv[])
    {
    	int c = 0;
    	int nl = 0;
    	int b = 0;
    	int t = 0;
    	
    	do{
    	        c = getchar();
    		if (c == '\n')
    			nl++;
    		if (c == ' ')
    			b++;
    		if (c == '\t')
    			t++;
    	} while(c != EOF);
    	
    	printf("Newlines = %d Blanks = %d Tabs = %d", nl, b, t);
    }

  10. #10
    Registered User
    Join Date
    Feb 2010
    Posts
    20
    Ah, there you go. I in fact did not know how to signal EOF in the terminal. I used the ='Q' trick and the code worked fine, but how do I signal EOF exactly?

  11. #11
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Okay, so _tmain is for wide char and unicode support.
    http://www.coderjoe.net/archive/2007...ain-vs-_tmain/

    Quote Originally Posted by scwizzo View Post
    Better yet, write your code like this... (style points, ya know )
    Since when is the long-winded do...while() better style than the more succinct while()?

    Quote Originally Posted by serg_yegi View Post
    Ah, there you go. I in fact did not know how to signal EOF in the terminal. I used the ='Q' trick and the code worked fine, but how do I signal EOF exactly?
    I'm not sure what it is in windows; try ctrl-d or ctrl-z. You may or may not need to press return.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  12. #12
    Registered User
    Join Date
    Mar 2009
    Posts
    399
    CTRL+D in *nix and CTRL+Z in Windows.

  13. #13
    Registered User
    Join Date
    Feb 2010
    Posts
    20
    Strange, ctrl+z just seems to put ^Z into the command prompt and does nothing after I press return.

  14. #14
    Registered User
    Join Date
    Mar 2009
    Posts
    399
    Try doing CTRL+Z twice.

  15. #15
    Registered User
    Join Date
    Feb 2010
    Posts
    20
    Ah there you are. Ctrl- Z twice worked. Thanks a lot everyone for the help. Do I need to mark this thread resolved or something along those lines?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 01-21-2010, 04:40 PM
  2. Line of data input method
    By larry_2k4 in forum C Programming
    Replies: 2
    Last Post: 04-28-2009, 11:34 PM
  3. The C programming language exercise
    By refuser in forum C Programming
    Replies: 10
    Last Post: 11-20-2008, 03:57 PM
  4. Language of choice after C++
    By gandalf_bar in forum A Brief History of Cprogramming.com
    Replies: 47
    Last Post: 06-15-2004, 01:20 AM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM