Hi everyone,
I have searched high and low for the answer to what I am sure is a simple mistake that I just can't put my finger on!
When using the %s argument with sprintf to pass a string of characters, the resulting output is unexpected and the rest of the argument calls are also corrupted. I have tried to specify the precision using something like %.4s to read four characters but the same thing happens.
Code:
#include <string>
#include <iostream>
using namespace std;
class testclass
{
public:
testclass(string id, int firstnumber, int secondnumber, int thirdnumber);
~testclass();
};
testclass::testclass(string id, int firstnumber, int secondnumber, int thirdnumber)
{
char statement[100];
sprintf_s(statement, "%s, %i, %i, %i", id, firstnumber, secondnumber, thirdnumber);
// statement should now say: "abcd, 1, 2, 3"
// statement actually says: "¸ý$, 1684234849 1537765632 44"
cout << statement;
}
int main()
{
new testclass("abcd", 1, 2, 3);
return 0;
}
I know I am going to kick myself for this one!