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?
Thanks guys! I'm new to C++, just started 2 weeks ago
void center (char* w)
int l, c;
char length ; //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("%s",w); //inserts the string, then creates a new line
int main ()
double a, b, c;
char text ;
center("Enter coefficient of x^2: ");
center("Enter coefficient of x^1: ");
center("Enter coefficient of x^0: ");
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;
> 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.
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.
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!!
disc = b * b - 4 * a * c;
d = (sqrt (disc));
x1 = ( -1 * b + d ) / ( 2 * a ) ;
x2 = ( -1 * b - d ) / ( 2 * a ) ;
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...");
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