Thread: I need some help on this program.

  1. #1
    Registered User
    Join Date
    Oct 2017
    Posts
    36

    I need some help on this program.

    The aim of this program is to learn more about the ctype.h library.
    The program should give the user five attempts to enter lower case letters. It then checks the letter entered. if it is lower it will convert it into an upper case letter. This works fine so far.

    Where i do have difficulties is that after the first time through the loop, i do not enter the second state of the loop but rather the third. I have looked into the code but found no errors hence i need assistance here.

    Code:
    #include <stdio.h>
    #include <ctype.h>
    #include <stdbool.h>
    
    
    
    
    //Write a simple application which makes sure every letter entered a lower case and then converted to upper case
    
    
    int main(void)
    {
        char letter;
        unsigned short int count_max = 5U;
        unsigned short int count_min = 1u;
    
    
        do
        {
            letter = 0;
            printf("%d. Enter Alphabet : ", count_min);
            scanf("%c", &letter);
    
    
            //check if the letter entered is lower case. if so proceed and convert to upper case
            if(islower(letter))
            {
                printf("You entered a lower case letter %c\n\n", toupper(letter));
            }
            else
            {
                printf("You entered an upper case letter. I only need lower case letters.\n\n");
            }
    
    
            printf("\n\n");
    
    
            ++count_min;
        }while(count_min <= count_max);
    
    
        //end of program
        printf("This is the end of this program.\n\n");
    
    
        //return control over to the os
        return(0);
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Remember that when you enter a character, the enter itself is read as a newline sequence.
    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

  3. #3
    Registered User
    Join Date
    Oct 2017
    Posts
    36
    Ok. So i guess i should remove the trailing newline in the next printf statement. I'm only guessing, please can you point out how to go about that. I am new using C and i am currently learning.

    Thanks for your response.

  4. #4
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Going with the ctype.h theme, you could change
    Code:
    else
    {
      ...
    
    
    To
    
    else if (isupper (letter ))
    {
      ..
    And maybe add...
    Code:
    else if( isspace( letter ))
    {
      /* Do Nothing */
    }
    Fact - Beethoven wrote his first symphony in C

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 03-15-2016, 03:29 PM
  2. Replies: 7
    Last Post: 03-15-2016, 06:35 AM
  3. Replies: 2
    Last Post: 09-09-2014, 02:36 PM
  4. Replies: 2
    Last Post: 12-11-2012, 12:25 AM
  5. Replies: 1
    Last Post: 03-03-2009, 04:47 PM

Tags for this Thread