Thread: Problem with DO and nested IF inside it

  1. #1
    Registered User
    Join Date
    Feb 2018
    Posts
    8

    Exclamation Problem with DO and nested IF inside it

    Hello, guys.

    I have a problem with this code:

    Code:
    /*
    
    Write a program “CASE” which reads an upper case character from the keyboard and prints it
    out in lower case.
    
    */
    
    #include <stdio.h>
    
    void main ()
    {
        char upperCase, lowerCase;
    
        printf("This program changes and upper case character into a lower case character.\n");
    
        do
    
            scanf("%c", &upperCase);
    
            if (upperCase < 65 || upperCase > 90)
                printf("The entered value is not an upper case character. Please, enter another value.");
    
        while (upperCase < 65 || upperCase > 90);
    
        lowerCase = upperCase + 32;
    
        printf ("Result => %c", lowerCase);
    }
    It's not working.

    This is the error I'm getting:

    main.c: In function 'main': main.c:18:9: error: expected 'while' before 'if' if (upperCase < 65 || upperCase > 90) ^~

    exit status 1
    Any idea what's the problem? I have no idea.

    Thanks in advanced.

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    3,882
    Code:
        do
     
            scanf("%c", &upperCase);
     
            if (upperCase < 65 || upperCase > 90)
                printf("The entered value is not an upper case character. Please, enter another value.");
     
        while (upperCase < 65 || upperCase > 90);
    You must use braces when doing a multi-line statement inside a control statement.


    Code:
        do {
     
            scanf("%c", &upperCase);
     
            if (upperCase < 65 || upperCase > 90)
                printf("The entered value is not an upper case character. Please, enter another value.");
     
        } while (upperCase < 65 || upperCase > 90);
    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Registered User
    Join Date
    Feb 2018
    Posts
    8
    Oh, my Gosh... How in Heavens I didn't see that...! Hahaha... Thanks, brother!

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,907
    Tim got your problem fixed, but I'd like to point out a few other improvements:

    • Getting a good editor/IDE with syntax highlighting and auto-indent features is very helpful. In fact, an auto-indenter would have caught this problem for you, as it would have unindented the if (to line up with the do), which would have been a big clue.
    • Generally main should return an int (exceptions for some embedded systems and the like). See this explanation.
    • As a general rule, you want to avoid magic numbers in your code (e.g. 65, 90, 32 in the code you posted). They make it harder to tell what your code is supposed to do.
    • Your code assumes the upper case letters are stored contiguously as numbers 65-90. This will be true for the vast majority of systems out there (anything using ASCII or compliant character encodings like unicode/utf8), but it is not actually guaranteed. Be aware that there are other character encodings for which this doesn't hold true; it's better to get in the habit of writing more portable code now.
    • There are existing functions in the standard C library that do what you want. Look into the isupper() and tolower() functions.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need to use gotoxy() inside a nested for loop
    By abujuguluy in forum C++ Programming
    Replies: 3
    Last Post: 12-20-2017, 12:28 AM
  2. Nested if Statement problem
    By Cochise in forum C Programming
    Replies: 3
    Last Post: 08-03-2016, 02:19 AM
  3. Nested while loop inside for loop
    By Sonny in forum C Programming
    Replies: 71
    Last Post: 07-31-2011, 08:38 PM
  4. Problem in Nested Loops
    By umair_crash in forum C++ Programming
    Replies: 12
    Last Post: 09-16-2008, 08:35 AM
  5. A nested if inside the switch
    By Extropian in forum C Programming
    Replies: 20
    Last Post: 08-15-2005, 01:23 AM

Tags for this Thread