Thread: character help

  1. #1
    Registered User
    Join Date
    Mar 2013
    Posts
    31

    character help

    2. Write a program that asks for a character in main, passes it to a function which then prints the character and returns the next character in the alphabet to main. In main print the new character.

    can you please help me how to start my program?

    Thank you.

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    I usually like to start my programs like this:

    Code:
    #include <stdio.h>
    
    int main(void)
    {
        //
    
        return 0;
    }
    If you want more help, please provide your attempt and tell us where exactly you're having difficulties.

    (P.S. If you haven't already, you'd best read the homework policy.)

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    The start of your program is up to you. You have attended the class, read the book, or whatever. You need to show that you have made a REASONABLE effort to solve your problem, and then ask specific questions, so we can give specific and helpful, answers.

  4. #4
    Registered User
    Join Date
    Mar 2013
    Posts
    31
    Code:
    #include<stdio.h>
    char fun3 (char char);
    main()
    {
        char a;
        char b;
        char final;
        printf("Please enter a character: ");
        scanf("%c",&a);
        printf("\nPlease enter another character: ");
        scanf("%c",&b);
        final = fun3 (a);
        printf("The character is %c ",final);
        getch();
    }
    
    char fun3 (char a, char b)
    {
        
    }
    can you please help me?

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Write a program that asks for a character in main, passes it to a function which then prints the character
    You've finished the first part, asking for the character in main. But your function is both incomplete and incorrect. First you only need one parameter, not two. Second you then need to print this character in the function, not main. Then you need to return the next character to main().

    Jim

  6. #6
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    [edit] Jim beat me!

    Let's break down the assignment, shall we?

    > "Write a program that asks for a character in main..."

    You have this part. Why are you asking for two characters though?

    > "...passes it to a function..."

    You have this part, too. To stick to the assignment as stated in the first post, you should only be passing one character to the function, not two.

    Note that if you're declaring the function to receive two characters, only passing one is a no-no.

    Also, your function declaration (up top) should match the first line of the function definition (at the bottom of the code). I suggest you change your function declaration/definition to "char fun3(char a);"

    > "...which then prints the character..."

    This is easy - you're doing it in "main()", so just do the same thing in your function.

    > "...and returns the next character in the alphabet to main."

    Here you need to:
    1. Determine the next character in the alphabet
    2. Send it back to main

    I'm assuming this is where your trouble lies - please confirm.

    > "In main print the new character."

    You have this part done.

    --------

    See if you can neaten up your program a bit, add the stuff I explained to the best of your understanding, and if you still can't get it finished, post your updated code with specific questions about where you're stuck.
    Last edited by Matticus; 03-13-2013 at 12:12 PM.

  7. #7
    Registered User
    Join Date
    Mar 2013
    Posts
    31
    How to pass character to the function?

    Code:
    char fun (char);
    main()
    {
    	char x;
    	int answer;
    	printf("Please enter a character: ");
    	scanf("%c",&x);
    	answer = fun(x);
    	printf("%c",answer);
    	getch();
    }
    
    
    char fun (char a)
    {
    
    
    	return(0);
    }

  8. #8
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Just like you did on line 8.

  9. #9
    Registered User
    Join Date
    Mar 2013
    Posts
    31
    Thank you very much sir. The program is working fine. Thank you so much.

    Code:
    #include<stdio.h>
    char fun (char);
    main()
    {
        char x;
        int answer;
        printf("Please enter a character: ");
        scanf("%c",&x);
        answer = fun(x);
        printf("\nThe new character is %c.",answer);
        getch();
    }
     
     
    char fun (int a)
    {
       int b =1;
       int c;
       c = a + b;
        return(c);
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Printing from an opened file character by character
    By SneakySnake in forum C Programming
    Replies: 1
    Last Post: 12-07-2012, 01:05 PM
  2. Multi-character character constant warning
    By visuthan in forum C Programming
    Replies: 3
    Last Post: 07-16-2012, 12:43 PM
  3. Replies: 10
    Last Post: 07-05-2011, 08:21 PM
  4. wide character (unicode) and multi-byte character
    By George2 in forum Windows Programming
    Replies: 6
    Last Post: 05-05-2007, 12:46 AM
  5. about wide character and multiple byte character
    By George2 in forum C Programming
    Replies: 3
    Last Post: 05-22-2006, 08:11 PM