Thread: Code giving undesired output..

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    12

    Code giving undesired output..

    /*Toggle-Case : If lower-case character entered, then change to upper-case and vice-verse :*/

    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<ctype.h>
    
    void main()
    
    {
    
       clrscr();
    
       int a,b;
       char ch;
    
       do
    
       {
         printf("\n\n Enter any alphabet : ");
         a=getchar();
    
         if(a>=65&&a<=90)
    
          {
    
        b=tolower(a);
        printf("\n\n The lower-case for %c is : %c ",a,b);
    
          }
    
         else if(a>=97&&a<=122)
    
          {
    
        b=toupper(a);
        printf("\n\n The upper-case for %c is : %c ",a,b);
    
          }
    
        else
    
          printf("\n\n Wrong Input..!!! Alphabet ONLY ");
    
        printf("\n\n Do you want to continue ? (y/n) : ");
    
        ch=getche();
    
       }while(ch=='y'||ch=='Y');
    
        getch();
    
     }

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by somali.cc View Post
    /*Toggle-Case : If lower-case character entered, then change to upper-case and vice-verse :*/

    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<ctype.h>
    
    void main()
    
    {
    
       clrscr();
    
       int a,b;
       char ch;
    
       do
    
       {
         printf("\n\n Enter any alphabet : ");
         a=getchar();
    
         if(a>=65&&a<=90)
    
          {
    
        b=tolower(a);
        printf("\n\n The lower-case for %c is : %c ",a,b);
    
          }
    
         else if(a>=97&&a<=122)
    
          {
    
        b=toupper(a);
        printf("\n\n The upper-case for %c is : %c ",a,b);
    
          }
    
        else
    
          printf("\n\n Wrong Input..!!! Alphabet ONLY ");
    
        printf("\n\n Do you want to continue ? (y/n) : ");
    
        ch=getche();
    
       }while(ch=='y'||ch=='Y');
    
        getch();
    
     }
    Welcome to the forum, somali.cc!

    I like that you used an int variable for getchar() - nice.

    But there is an easier way to do this, and technically, you haven't changed the entered letter to the other case -- you have made another variable, into the other case.

    That is, "a" is still in the original (wrong) case.

    I know, picky, picky!

    if you were to use:
    Code:
    a=toupper(a);
    on lower case letter 'a', what would that do?

    And lastly, using the ascii values for the letters, is not preferred. Use 'A' and 'Z' or 'a' and 'z', instead. Same meaning, (usually), but it's more portable, and it makes our code more clear.

  3. #3
    Registered User
    Join Date
    Oct 2012
    Posts
    12
    Hi Adak, thnx for replying me.

    Using a=tolower(a); cud have been used, but then i cudnt have printed :- The lower-case for A is : a

    And yes, i wl definitely go by ur suggestion of using 'a' /'A' instead of ASCII values..

    But the code that i have provided is giving me an errorneous output when I m entering 'y'/'Y' in ' Do You want to continue ? (y/n) ' statement. Any suggestion on that ?

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    When you enter a value, you put two things into the keyboard buffer (the input stream in most cases): the value, AND the '\n' (newline) char, (when you hit the enter key). With integers, it's usually not a problem, or with strings, but with a char it's a BIG problem! The newline char stays in the keyboard buffer, until it reaches the getch(), and THEN you get the newline char, see? And that's NOT what you want.

    There are many ways to get a newline off the keyboard buffer. In your case, a simple getchar(), before the getche(), will do it for you. I know it sounds "weird", but try it.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Since you've already got ctype.h included, why not

    Code:
    if ( isupper(ch) ) {
      ch = tolower(ch);
    }
    Oh, and main returns an int, not void.
    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.

  6. #6
    Registered User
    Join Date
    Oct 2012
    Posts
    12
    Thank U Adak..Thanx soo much for the explanation and solution...U rock man..!!

  7. #7
    Registered User
    Join Date
    Oct 2012
    Posts
    12
    @Salem : True, main() always returns integer value.

    What if I want a function not to return anything ?

    Shud it not be preceede by the keyword void or add another statement : return(0);

    pls correct me if i'm wrong as I m new to C-Programming.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Sure, any function that you write, which doesn't return a result, can be declared void.

    But main is special - it's the interface between all your code (which main calls), and the environment (your OS), which calls main.

    In order for the environment to call main properly, you need to declare it as returning an int, and make sure you return an int. Zero is good if you've got nothing particular to say about your program.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Why is this giving me this output?
    By mgracecar in forum C Programming
    Replies: 4
    Last Post: 03-16-2012, 12:51 PM
  2. Random Walk Simulation in 1D - Undesired output
    By SalamuB in forum C Programming
    Replies: 2
    Last Post: 01-30-2012, 07:03 AM
  3. Replies: 4
    Last Post: 12-23-2011, 08:56 AM
  4. structure giving weird output
    By bluetxxth in forum C Programming
    Replies: 7
    Last Post: 02-14-2010, 11:44 PM
  5. structure woes equals undesired output
    By skeptik in forum C Programming
    Replies: 3
    Last Post: 07-23-2004, 02:20 AM