Like Tree1Likes
  • 1 Post By BillyTKid

help with program

This is a discussion on help with program within the C Programming forums, part of the General Programming Boards category; hey guys so i was doing what commontater said and i remade the program i was suppose to modify(not make). ...

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    41

    help with program

    hey guys so i was doing what commontater said and i remade the program i was suppose to modify(not make). its working to an extent that it prints my error message even if the input is within the bounds. the program should print the correct statement if it is from a-z or A-Z anything else gets the error message except for the ";". i think the return(enter) key is making the error output even if the input is within the bounds. how do i make it ignore the return key as an error?


    Code:
    #include <stdio.h>
    
    
    int main()
    {
    int ch;
    	while((ch=getchar())!=EOF)
    	{
    		if((ch>='a' && ch<='z')||(ch>='A' && ch<='Z')&&(ch=='\r'))
    		printf("The command is: %c\n",ch);
    		else if(!(ch>='a' && ch<='z')||(ch>='A' && ch<='Z')
    		{
    		printf("Error: %c is not a letter.\n", ch);
    		}
    	}
    }

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Have you tried using using chr != '\n', instead of chr == '\r' ?

  3. #3
    Registered User
    Join Date
    Sep 2011
    Posts
    41
    hey adak, thanks for the suggestion.
    i have tried that and it does something similar to my current problem.
    this is how i corrected it:
    *also i was wondering if i should make another "if" statement and use that one for A-Z
    Code:
    #include <stdio.h>
    
    int main()
    {
    int ch;
        while((ch=getchar())!=EOF)
        {
            if((ch>='a' && ch<='z')||(ch>='A' && ch<='Z')&&(ch!='\n'))
            printf("The command is: %c\n",ch);
            else if(!(ch>='a' && ch<='z')||(ch>='A' && ch<='Z'))
            {
            printf("Error: %c is not a letter.\n", ch);
            }
        }
    }

  4. #4
    Registered User
    Join Date
    Aug 2010
    Posts
    231
    The C standard doesn't know charactersets, only the '0'..'9' char-sequence is guaranteed.
    Therefore your 'A'..'Z'/'a'..'z' code will crash if the actual characterset is not ASCII.
    Salem likes this.

  5. #5
    Registered User
    Join Date
    Sep 2011
    Posts
    41
    ok i kind of have an idea of what you meant but what does this have to do with crashing?

  6. #6
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    You could put another "else if" case to handle the newline.
    Code:
    while(!asleep) {
       sheep++;
    }

  7. #7
    Registered User
    Join Date
    Sep 2011
    Posts
    41
    hey thanks dude! that worked. if i wanted the program to print another message but the conditions are there must be a letter before a ';' like a ; b instead of ; b
    is there a way to test if the first input is a letter?

    Quote Originally Posted by TheBigH View Post
    You could put another "else if" case to handle the newline.

  8. #8
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    1,625
    To sample on what @BillyTKid said, you could use the "islower()" and "isupper()" standard functions instead. Just a suggestion.

    EDIT: You need "#include <ctype.h>" for those.
    Devoted my life to programming...

  9. #9
    Registered User
    Join Date
    Sep 2011
    Posts
    41
    i forgot to include the new code so here it is:

    Code:
    #include <stdio.h>
    
    
    
    
    int main()
    {
    int ch;
    	while((ch=getchar())!=EOF)
    	{
    		if((ch>='a' && ch<='z')||(ch>='A' && ch<='Z'))
    		printf("The command is: %c\n",ch);
    		else if(ch=='\n' || ch==';')
    		{
    		printf("");
    		}
    		else if(!(ch>='a' && ch<='z')||!(ch>='A' && ch<='Z'))
    		{
    		printf("Error: %c is not a letter.\n", ch);
    		}
    	}
    }

  10. #10
    Registered User
    Join Date
    Sep 2011
    Posts
    41
    I am not that familiar with those two functions but all i need is if I input the semicolon then a letter i want an error message to appear and then display the letter after that. sort of like instead of this: h;e which is okay, something like this ;i should make an error message because there should be a letter before the semicolon. been trying to implement different ifelse statements but they fail to a point where the program doesnt work normal. could you give me some advice?

    Quote Originally Posted by GReaper View Post
    To sample on what @BillyTKid said, you could use the "islower()" and "isupper()" standard functions instead. Just a suggestion.

    EDIT: You need "#include <ctype.h>" for those.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 03-16-2010, 11:17 AM
  2. Replies: 1
    Last Post: 03-03-2009, 04:47 PM
  3. Replies: 5
    Last Post: 08-17-2007, 12:43 AM
  4. Replies: 18
    Last Post: 11-13-2006, 01:11 PM
  5. making a program leave a msg for background program when it closes
    By superflygizmo in forum Windows Programming
    Replies: 2
    Last Post: 02-06-2006, 07:44 PM

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