fgets assigned to a char

This is a discussion on fgets assigned to a char within the C Programming forums, part of the General Programming Boards category; Okay, so i have been writing a program, and to my joy i find i only have two warnings and ...

  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
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,625
    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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,422
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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, 01: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, 04:26 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21