Thread: Help with using the return statement

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    38

    Help with using the return statement

    Code:
    #include <stdio.h>
    char ChangeToNext(char ch);
    main()
    {
        char x;
        printf("Input uppercase letter: ");
        fflush(stdin);
        scanf("%c",&x);
        ChangeToNext(x);
        getch();
        return 0;
    }
    char ChangeToNext(char ch)
    {
        if (ch == 'A' || ch == 'B' || ch == 'C' || ch == 'D' || ch == 'E' || ch == 'F' ||
        ch == 'G' || ch == 'H' || ch == 'I' || ch == 'J' || ch == 'K' || ch == 'L' ||
        ch == 'M' || ch == 'N' || ch == 'O' || ch == 'P' || ch == 'Q' || ch == 'R' ||
        ch == 'S' || ch == 'T' || ch == 'U' || ch == 'V' || ch == 'W' || ch == 'X' ||
        ch == 'Y' || ch == 'Z')
        {
            if (ch == 'Z')
            {
                return('A');
            }
            else
            {
                ch=ch+1;
                return(ch);
            }
        }
        else
        {
            printf("\nWrong character. Try again.\n");
        }
        printf("\n");
        return;
    }
    The code above should display the next alphabet letter (in the case of Z, the next letter is A), but I can't get any output. How will I get the output using the return statement?

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    if (ch == 'A' || ch == 'B' || ch == 'C' || ch == 'D' || ch == 'E' || ch == 'F' ||
        ch == 'G' || ch == 'H' || ch == 'I' || ch == 'J' || ch == 'K' || ch == 'L' ||
        ch == 'M' || ch == 'N' || ch == 'O' || ch == 'P' || ch == 'Q' || ch == 'R' ||
        ch == 'S' || ch == 'T' || ch == 'U' || ch == 'V' || ch == 'W' || ch == 'X' ||
        ch == 'Y' || ch == 'Z')
    How about:
    Code:
    if (ch >= 'A' && ch <= 'Z')
    Code:
        else
        {
            printf("\nWrong character. Try again.\n");
        }
        printf("\n");
        return;
    
    This code will ONLY be reached when you reach the else-branch, since all the other code is in the if-side of the code and this part has a return in each of it's if/else branch. So you can either remove the else part altogether, or you could move the printf + return into the else.

    Code:
        fflush(stdin);
    Don't use this - fflush on input files is "undefined" in the C standard. It may well work on the system you are using, but compiling your code in a different setup (different compiler or different OS) and it will do nothing useful, and since it's undefined, it allows the system to "do anything it likes", including crash. See the FAQ for a solution of "how to clear the input buffer" - although you probably want to put that AFTER your scanf() to remove any un-necessary input that came with the character you read.


    Finally, it's fairly clear that you don't understand how your code works. Where do you expect the output to come from?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Ugly C Lover audinue's Avatar
    Join Date
    Jun 2008
    Location
    Indonesia
    Posts
    489
    Code:
    printf("%c", ChangeToNext(x));
    ?

  4. #4
    Registered User
    Join Date
    Jul 2008
    Posts
    38
    thank you very much, now i'll just have to replace variables with functions.

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Quote Originally Posted by matsp View Post
    How about:
    Code:
    if (ch >= 'A' && ch <= 'Z')
    Better yet:
    Code:
    if (isalpha(ch) && isupper(ch))
    Because A-Z is not guaranteed to be consecutive.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C or C++
    By AcerN30 in forum Game Programming
    Replies: 41
    Last Post: 05-30-2008, 06:57 PM
  2. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  3. C++ FTP class won't work
    By lord mazdak in forum C++ Programming
    Replies: 8
    Last Post: 12-18-2005, 07:57 AM
  4. sort linked list using BST
    By Micko in forum C Programming
    Replies: 8
    Last Post: 10-04-2004, 02:04 PM
  5. opengl code not working
    By Unregistered in forum Windows Programming
    Replies: 4
    Last Post: 02-14-2002, 10:01 PM