Thread: sprintf output error

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    8

    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?

    Code:
    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
    	printf("\n");
    	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(text);
    			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);
    			center(text);
    			sprintf_s(text, "Is F ( x ) = %d x ^ 2 + %d x ^ 1 + %d x ^ 0 the correct equation? Y/N? ", a, b, c);
    			center(text);
    			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.

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