Thread: Help with trying to kill repeat function

  1. #1
    Student otchster's Avatar
    Join Date
    Oct 2005
    Posts
    30

    Help with trying to kill repeat function

    I'm a bit new to C, and I supposed to code a program that prompts for a character, and then will echo the same character, ascii and hex equiv. of that character. It is supposed to repeat the process, prompting again, and will keep going until the user inputs a ! character. I can't figure out why the ! isant killing the program.

    all help is much appreciated, this is my first post here, and look forward to being an active, responseable member. Thanks!

    my code:

    Code:
    #include <stdio.h>
    #define FLAG '!'
    
    int instruct();
    int getval();
    int displayval();
    int repeat();
    
    int main()
    {
      char car;
      instruct();
      car = getval();
    
      displayval();
      while (car != FLAG)
        {
          repeat();
        }
      return (0);
    }
    
    int instruct()
    {
      printf("Please enter a (character) value: \n");
      return (0);
    }
    
    int getval()
      {
        char car;
        car = getc(stdin);
        fpurge(stdin);
        return (0);
      }
    
    int displayval()
      {
        char car;
        printf("Character: %c\t ascii: %u\t hex: %#X\n", car, car, car);
        return (0);
      }
    
    int repeat()
      {
        instruct();
        getval();
        displayval();
    
        return (0);
      }
    Last edited by otchster; 10-25-2005 at 06:27 PM.

  2. #2
    Captain - Lover of the C
    Join Date
    May 2005
    Posts
    341
    Your getval() always returns 0 which is not equal to '!'. This is a better getval() function:

    Code:
    int getval()
    {
        return getc(stdin);
    }
    That little change worked for me.
    Don't quote me on that... ...seriously

  3. #3
    Student otchster's Avatar
    Join Date
    Oct 2005
    Posts
    30
    hmmmm, so i tried two things in response to your reply.

    first I changed my:
    Code:
    int getval()
      {
        char car;
        car = getc(stdin);
        fpurge(stdin);
        return (0);
      }
    to:
    Code:
    int getval()
      {
        char car;
        car = getc(stdin);
        fpurge(stdin);
        return getc(stdin);
      }
    which made the program run inncorrectly, if wouldnt echo the new vaules unless i hit the return key twice, and it still didnt fix the ! kill problem..

    then i tried a different way, changing:
    Code:
    int getval()
      {
        char car;
        car = getc(stdin);
        fpurge(stdin);
        return (0);
      }
    to:
    Code:
    int getval()
    {
        return getc(stdin);
    }
    that also made the program comply incorrectly, run incorrectly, and didnt solve the ! kill problem.

    i am new at this, maybe you meant somthing i didnt quite understand. lemme know what you think i did wrong, or what i can do to cure the initial problem.

    thanks again =)

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >> char car;
    >> car = getc(stdin);
    getc() returns an int, not a char. You compiler should be warning you of such errors, if it isn't, turning up the warning levels!

    The displayval() functions returns an arbituary value (ie always 0). There's no point in returning a hardcoded value in this way, I'd suggest you either return nothing (change to void function), or return the value from the printf() function.


    [edit]
    Out of interest, can you post the source for the fpurge() function.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Code:
    int displayval()
      {
        char car;
        printf("Character: %c\t ascii: %u\t hex: %#X\n", car, car, car);
        return (0);
      }
    And what value do you expect car to have when printf() does it's work in this function? You haven't assigned it any value!

    [edit]
    There's a whole load of "passing the variable around" that you need to do. The value of car needs to be passed between getval(), repeat(), displayval(). For example, when you leave repeat() to go back to main, the value of the local car variable in the main is untouched.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  6. #6
    Captain - Lover of the C
    Join Date
    May 2005
    Posts
    341
    I suggest that you make
    Code:
    car
    a global variable. If you don't, you need to send car to each function and return it. Do one of those two options then show me your code so that I can help you get it all right.
    Don't quote me on that... ...seriously

  7. #7
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>I suggest that you make [car] a global variable.
    I don't. Learn how to program properly from day one, don't bodge your program just to be lazy.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  8. #8
    Captain - Lover of the C
    Join Date
    May 2005
    Posts
    341
    >> I don't.

    Then would you suggest sending car to every function and returning it?
    Or would you suggest sending a pointer to car?
    Or neither?
    Don't quote me on that... ...seriously

  9. #9
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    ... Sending car around where needed. The variable is too small and the program requirements are too simplistic to warrant a pointer at this stage.

    Global variables should be your last choice. I'm not saying never use them, they do have their place in life, it's just not in this particular program.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  10. #10
    Captain - Lover of the C
    Join Date
    May 2005
    Posts
    341
    You had so many errors that I wanted to just give you the proper code:

    Code:
    #include <stdio.h>
    #define FLAG '!'
    
    int instruct();
    int getval();
    int displayval(int car);
    int repeat();
    
    int main()
    {
    	int car;
    
    	instruct();
    
    	car = getval();
    
    	displayval(car);
    
    	while (car != FLAG)
    	{
    		car = repeat();
    	}
    	return (0);
    }
    
    int instruct()
    {
    	printf("Please enter a (character) value: ");
    	return (0);
    }
    
    int getval()
    {
    	int car = getchar();
    	getchar();
    	return car;
    }
    
    int displayval(int car)
    {
    	printf("Character: %c\t ascii: %u\t hex: %#X\n\n", car, car, car);
    	return (0);
    }
    
    int repeat()
    {
    	int car;
    
    	instruct();
    	car = getval();
    	displayval(car);
    	return car;
    }
    Note that many of the functions now recieve car as one of their parameters. Many also return car.
    Don't quote me on that... ...seriously

  11. #11
    Student otchster's Avatar
    Join Date
    Oct 2005
    Posts
    30
    thanks all who helped, thanks brad for the full corrected code, i believe i understand now.

    but just so i know..
    in the below excerpt:
    Code:
    #include <stdio.h>
    #define FLAG '!'
    
    int instruct();
    int getval();
    int displayval(int car);
    int repeat();
    
    int main()
    {
    	int car;
    
    	instruct();
    the varbiles outside the main function, like [int instruct();] are global, and varibles inside certain function are local, like [int car;] ? (whereas [ ] are seperaters for visual purpose.

    thansk all =)

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by otchster
    thanks all who helped, thanks brad for the full corrected code, i believe i understand now.

    but just so i know..
    in the below excerpt:
    Code:
    #include <stdio.h>
    #define FLAG '!'
    
    int instruct();
    int getval();
    int displayval(int car);
    int repeat();
    
    int main()
    {
    	int car;
    
    	instruct();
    the varbiles outside the main function, like [int instruct();] are global, and varibles inside certain function are local, like [int car;] ? (whereas [ ] are seperaters for visual purpose.

    thansk all =)
    Yes and no. If they were variables, yes, they'd be global. However, what you have above main are actually function prototypes. They're up there so you can call those functions before you've actually written the function body in the code. But if they were variables, yes, they'd be global.


    Quzah.
    Hope is the first step on the road to disappointment.

  13. #13
    Captain - Lover of the C
    Join Date
    May 2005
    Posts
    341
    int instruct(); is a prototype to a function. You do no have any global variables in this program. If you had a global variable it would look like:

    Code:
    #include <stdio.h>
    
    //This is not in any function so it is global
    int GlobalIntegar;
    
    //This is a function prototype
    int MyFunction();
    int main()
    {
        // This is inside a function so it is a local variable
        int LocalInteger;
        // This is a function call
        MyFunction();
        return 0;
    }
    
    // This is a funtion
    int MyFunction()
    {
        printf("Does this answer your question?\n");
    
        // I don't have anything important to return here so I will just return zero
        return 0;
    }
    I hope that little program clears up any questions you have about local and global variables.
    Don't quote me on that... ...seriously

  14. #14
    Student otchster's Avatar
    Join Date
    Oct 2005
    Posts
    30
    Quote Originally Posted by Brad0407
    int instruct(); is a prototype to a function. You do no have any global variables in this program. If you had a global variable it would look like:

    Code:
    #include <stdio.h>
    
    //This is not in any function so it is global
    int GlobalIntegar;
    
    //This is a function prototype
    int MyFunction();
    int main()
    {
        // This is inside a function so it is a local variable
        int LocalInteger;
        // This is a function call
        MyFunction();
        return 0;
    }
    
    // This is a funtion
    int MyFunction()
    {
        printf("Does this answer your question?\n");
    
        // I don't have anything important to return here so I will just return zero
        return 0;
    }
    I hope that little program clears up any questions you have about local and global variables.

    it very much did, thank you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. Beginner Needs help in Dev-C++
    By Korrupt Lawz in forum C++ Programming
    Replies: 20
    Last Post: 09-28-2010, 01:17 AM
  3. dllimport function not allowed
    By steve1_rm in forum C++ Programming
    Replies: 5
    Last Post: 03-11-2008, 03:33 AM
  4. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  5. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 06:44 PM