Thread: sprintf output error

  1. #1
    Registered User
    Join Date
    Sep 2010

    sprintf output error

    Hey guys, new to the forum here.
    EDIT: Alright guys, sorry about this whole edit, i'm doing more research and tests on this. The code basically is designed to center my text. Now I want to be able to insert double variables into this equation. I've tested the sprintf function outside of my program using %.0f and this works good, i can print the double variables. Now when I use it in my program I get a "buffer = 0, its too small error". How can i get around this?

    void center (char* w)
    	int l, c;
    	char length [80]; //sets a character array to 80
    	l = sprintf_s(length,"%s", w); //calculates length of string
    	l = ((80-l)/2); //calculates amount of spaces on the left side
    	for (c = 0; c < l; c++) //inserts spaces before the string
    		printf(" "); 
    	printf("%s",w); //inserts the string, then creates a new line
    int main ()
                            double a, b, c;
                            char text [80];
    			sprintf_s(text,"Equation #%d\n\n",count);
    			center("Enter coefficient of x^2: ");
    			scanf_s("%d", &a);
    			center("Enter coefficient of x^1: ");
    			scanf_s("%d", &b);
    			center("Enter coefficient of x^0: ");
    			scanf_s("%d", &c);
                            sprintf_s(text,"Equation #%d\n\n",count);
    			sprintf_s(text, "Is F ( x ) = %d x ^ 2 + %d x ^ 1 + %d x ^ 0 the correct equation? Y/N? ", a, b, c);
    			cin >> reply; 
    			cin.get ();
    return (0)
    Thanks guys! I'm new to C++, just started 2 weeks ago
    Last edited by Ji33my; 09-16-2010 at 10:50 PM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    > Thanks guys! I'm new to C++, just started 2 weeks ago
    So why is all your code C then?

    Well apart from one solitary cin statement.

    > scanf_s("%d", &a);
    If you had used cin here, you would have gotten the correct type-safe conversion.
    scanf REQUIRES you to read the manual and get the correct conversion format (this you have got wrong)

    Likewise on printing, %d is NOT for doubles.
    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.

  3. #3
    Registered User
    Join Date
    Sep 2010
    EDIT: Alright, I changed up the code and everything works in the beginning, but once I get to solving the equations at the end of my program, the buffer is too small.
                                           disc = b * b - 4 * a * c;
                                            d = (sqrt (disc));
    					x1 = ( -1 * b + d ) / ( 2 * a ) ;
    					x2 = ( -1 * b - d ) / ( 2 * a ) ;		
    					sprintf_s(text,"Equation #%d\n\n",count);
    					sprintf_s(text,"The solution for your quadratic problem is real distinct roots X1 = %lg and X2 = %lg", x1, x2);
    					center("Press Enter to Continue...");
    					cin.get ();
    Alright, I was using %g and that failed so I changed them all to %lg, most of my equations work except this one, it says the buffer is too small. Could use some help on this one. Thanks!!

    EDIT: Again, after constantly going over the code, I realized the reason this last equation didn't work is because the string holds more than 80 characters, so it couldn't pass through my char array Thanks for anyone who read the post to try to help
    Last edited by Ji33my; 09-17-2010 at 12:23 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  2. Errors including <windows.h>
    By jw232 in forum Windows Programming
    Replies: 4
    Last Post: 07-29-2008, 01:29 PM
  3. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  4. C++ compilation issues
    By Rupan in forum C++ Programming
    Replies: 1
    Last Post: 08-22-2005, 05:45 AM
  5. pointer to array of objects of struct
    By undisputed007 in forum C++ Programming
    Replies: 12
    Last Post: 03-02-2004, 04:49 AM