SPRINTF help please.

This is a discussion on SPRINTF help please. within the C Programming forums, part of the General Programming Boards category; OK, my compiler is giving two warnings: >>>> warning C4047: 'function' : 'const char *' differs in levels of indirection ...

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

    Arrow SPRINTF help please.

    OK, my compiler is giving two warnings:

    >>>> warning C4047: 'function' : 'const char *' differs in levels of indirection from 'unsigned int '
    >>>>warning C4024: 'sprintf' : different types for formal and actual parameter 2


    I was under the impression i could use sprintf to convert a string to an int?

    I have used strtol in the past and it has always worked, but i want an INT not a LONG INT.
    Here's the code:

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    int main(void)
    {
    	int iTempr, iMaxTempr, iMinTempr, iInputError;
    	char cBuff01[BUFSIZ];
    
    	do
    	{
    		printf("> Please enter the temperature:");
    		fgets(cBuff01, sizeof(cBuff01), stdin);
    		if (sscanf(cBuff01, "%d", &iTempr) == 1);
    
    		if (cBuff01[0] != '\n')
    		{
    			if (iTempr > iMaxTempr)
    			{
    				iMaxTempr = iTempr;
    			}
    			else if (iTempr < iMinTempr)
    			{
    				iMinTempr = iTempr;
    			}
    			iInputError = 0;
    		}
    		else 
    		{
    			printf("\n\tInvalid Input. Please try again.\n\n");
    			iInputError = 1;
    		}
    
    	
    	}while(iInputError == 1);
    	
    	printf("%d", iTempr);
    
    	return 0;
    }
    Last edited by Astra; 11-02-2006 at 05:16 AM.

  2. #2
    Registered User SKeane's Avatar
    Join Date
    Sep 2006
    Location
    England
    Posts
    234
    Looks like you are getting snprintf and sprint confused.

    I think (in your code) you meant
    Code:
    snprintf(cBuff01, sizeof(cBuff01), "%d", iTempr);

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,443
    > sprintf(cBuff01, sizeof(cBuff01), "%d", iTempr);
    If you're trying to get an integer OUT of the string (seems likely, given the previous line is fgets), then you need
    if ( sscanf(cBuff01, "%d", &iTempr) == 1 )

    > iTempr = cBuff01[BUFSIZ];
    Remove this line, it does nothing useful, and is an out-of-bound array access anyway.
    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
    changing the line
    Code:
    sprintf(cBuff01, sizeof(cBuff01), "%d", iTempr);
    to
    Code:
    sprintf(cBuff, "%d", iTempr);
    gave me no warnings, but no matter what my result came out as '-52'

    :S

  5. #5
    Registered User SKeane's Avatar
    Join Date
    Sep 2006
    Location
    England
    Posts
    234
    Good point(s).

  6. #6
    Registered User
    Join Date
    Oct 2006
    Posts
    55
    I've edited my first post (the code), is this how you meant to use it? I will read up about sscanf now too
    Last edited by Astra; 11-02-2006 at 05:16 AM.

  7. #7
    Registered User
    Join Date
    Oct 2006
    Posts
    55
    Sorry for double post, how would i change my code so that if they enter a letter or string, it loops again. I am not sure how to do this error checking method involving sscanf and fgets.
    Last edited by Astra; 11-02-2006 at 05:30 AM.

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,443
    By adding an 'else' to the if statement you've managed to neuter.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sprintf overflows my buffer -- why?
    By Lasston in forum C Programming
    Replies: 26
    Last Post: 06-20-2008, 04:33 PM
  2. sprintf : garbage appended
    By yeller in forum C Programming
    Replies: 9
    Last Post: 12-17-2007, 09:21 AM
  3. sprintf in C and C++
    By usu_vlsi in forum C++ Programming
    Replies: 2
    Last Post: 03-14-2005, 03:14 AM
  4. sprintf and sscanf
    By tommy69 in forum C Programming
    Replies: 10
    Last Post: 04-22-2004, 08:00 PM
  5. Sprintf
    By Trauts in forum C++ Programming
    Replies: 10
    Last Post: 01-15-2003, 12:35 PM

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