Thread: fgets assigned to a char

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    55

    Arrow fgets assigned to a char

    Okay, so i have been writing a program, and to my joy i find i only have two warnings and no errors .

    '' '=' char differs in levels of indirection from char * ''

    here is the section of code where the error is:
    Code:
    			printf("\nHas the client claimed in the last 12 months? (Y or N):");
    
    			fgets(cBuff02, sizeof(cBuff02), stdin);
    
    			cAnsYN = fgets(cBuff02, sizeof(cBuff02), stdin);
    
    			if (cBuff02[0] != '\n' && (*endptr == '\n' || *endptr == '\0'))
    			{
    				do
    				{
    					if (cAnsYN == 'Y')
    					{
    						iClaimNum++;
    cAnsYN i declared as a char
    cBuff02 i declared as a char array of size [BUFSIZ]
    iClaimNum i declared as an int (but isnt relative to this problem)

    Basically, its asking Y or N, if they put in Y, i want to be able to manipulate and use that data, as you can see where i assign the fgets part to cAnsYN, and where i write if(cAnsYN == 'Y').

    Help much appreciated .

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Keep in mind that fgets returns a pointer to the string it modified. This means that in order for

    cAnsYN = fgets(cBuff02, sizeof(cBuff02), stdin);

    to be a valid assignment, cAnsYN needs to be decared as a pointer to char.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Like this
    Code:
    			printf("\nHas the client claimed in the last 12 months? (Y or N):");
    			fgets(cBuff02, sizeof(cBuff02), stdin);
    			cAnsYN = cBuff02[0];
    fgets() reads the line into a buffer.
    You then look at that buffer in a variety of ways (in this case, just the first character) to decide whether the input was meaningful.

    > && (*endptr == '\n' || *endptr == '\0')
    Where'd this come from?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    55
    >> && (*endptr == '\n' || *endptr == '\0')
    >Where'd this come from?

    I thought i had to do that to check that the last piece of data is either pressing enter or end of array bits? Can i not use this method to stop an error occuring incase they put in more string than my BUFSIZ?

    EDIT: LOL, it seems when i pasted the do while loop from elsewhere in my program i added the part i needed for strtol by accident! 00ps.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sorting Linked Lists
    By DKING89 in forum C Programming
    Replies: 6
    Last Post: 04-09-2008, 07:36 AM
  2. Conversion Char To Char * Problem
    By ltanusaputra in forum Windows Programming
    Replies: 3
    Last Post: 03-01-2008, 02:06 PM
  3. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 08:54 PM
  4. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 05:26 PM