Thread: The following if statement is not working!...

  1. #1
    Registered User
    Join Date
    Jun 2010
    Posts
    4

    Unhappy The following if statement is not working!...

    Hello. I am completely new to programming so bear with me. I currently have this program compiled, built, and executed, but it is not doing what I want it to do:


    Code:
    #include <stdio.h>
    main()
    {
    int a;
    for(a=0; a<26; a++)
    {
    char b;
    b = getchar();
    if (b != 'a')
    {
    a=27;
    }
    }
    }
    So I tried out the program by entering character 'a' as the value for b, but the loop ends for some reason. By entering 'a' as the value for b, the argument used by the if statement should be regarded as false, and therefore skip the reseting of the variable a to 27. Why is the for loop not continuing? Please help, and keep in mind that I'm a total noob to this.

  2. #2
    Registered User
    Join Date
    Mar 2009
    Posts
    399
    When you enter 'a', you're also entering a newline '\n'. So the next time getchar is called, it will get the newline.

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Indent properly and use int main:
    Code:
    #include <stdio.h>
    int main()
    {
    	int a;
    	for(a=0; a<26; a++)
    	{
    		char b;
    		b = getchar();
    		if (b != 'a')
    		{
    			a=27;
    		}
    	}
    }
    This is basics you must know before anything else.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    214
    Quote Originally Posted by Elysia View Post
    Indent properly and use int main:
    Code:
    #include <stdio.h>
    int main()
    {
    	int a;
    	for(a=0; a<26; a++)
    	{
    		char b;
    		b = getchar();
    		if (b != 'a')
    		{
    			a=27;
    		}
    	}
    }
    This is basics you must know before anything else.
    And don't forget to return 0; at the end.

  5. #5
    Registered User
    Join Date
    Jun 2010
    Posts
    27
    Memloop is right!You should filter the '\n'.
    Like this:
    Code:
    do{
    ............
    }while(ch=='/n');

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    This might take a minute to load because of a server slow-down, but here is detailed explanation of the issue mentioned by Steve Cao and Memloop:

    http://206.251.36.107/programming/stdin_buffer.mhtml
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 05-07-2009, 11:31 AM
  2. multiple conditions - if statement
    By dibble in forum C Programming
    Replies: 8
    Last Post: 03-28-2009, 12:41 PM
  3. crazy if statement
    By jamespond88 in forum C Programming
    Replies: 26
    Last Post: 03-19-2009, 04:02 PM
  4. Max/min function not working correctly
    By En-Motion in forum C++ Programming
    Replies: 6
    Last Post: 03-19-2009, 12:28 AM
  5. Replies: 1
    Last Post: 08-31-2004, 04:07 AM