I am using VisualStudio 2013, I am having some trouble on the following code for my c++ class. I just can' t seem to figure out what to do next. I am trying to create a function that will format a string. It will take a string like this
write("The number {0} is smaller than {1}", -3, 8);
And have it output it formatted correctly. The arguments will always be doubles. So it would output to console like this:
The number -3 is smaller than 8.
My problem is I guess I don't understand the variable arguments enough or I am just doing something wrong in the code below. Everytime I try to set something like
userString[i] = var_arg(userString, arguments);
I get like an overflow error. Any guidance would be greatly appreciated.
#include <iostream>
#include <string>
#include <cstdarg>
using namespace std;
//Prototype
string write(string userString...);
void main()
{
write("This is {0} a string.{1}", 5);
system("pause");
}
string write(string userString...)
{
char target1 = '{';
char target2 = '}';
va_list arguments;
va_start(arguments, userString);
for (int i = 0; i < userString.size(); ++i)
{
if (userString[i] == target1 & userString[i + 2] == target2)
{
//Need help here...
}
}
va_end(arguments);
return userString;
}