this program uses the string prompt and replaces "{0}" with 5.651 and "{1}" with 3.427 and it works fine. when i change the string to "{0:c}" and "{1:c}" to replace the substring and format them as currency, "{0:f}" and "{1:f}" to replace the substring and format them as fixed point notation, "{0:e}" and "{1:e}" to replace the substring and format them as scientific notation, "{0:i}" and "{1:i}" to replace the substring and round to an integer, they all work independently. but when i try and put them into cases like if("{0}"), else if("{0:c}") it only works if i am looking for "{0}" anything else it doesnt work. I get a debug error. How do i fix this, what am i doing wrong? thanks in advance.
void main()
{
write("The number {0} is greater than {1}.\n", 5.651, 3.427);
}
void write(string prompt, ...)
{
double small, large, temp;
va_list arguments;
va_start(arguments, prompt);
large = va_arg(arguments, double);
small = va_arg(arguments, double);
if(small > large)
{
temp = large;
large = small;
small = temp;
}
if(prompt.find("{0}"))
{
ostringstream a;
a << large;
string arg1 = a.str();
ostringstream s;
s << small;
string arg2 = s.str();
size_t pos = prompt.find("{0}");
prompt = prompt.replace(pos, 3, arg1);
size_t pos2 = prompt.find("{1}");
prompt = prompt.replace(pos2, 3, arg2);
}
else if(prompt.find("{0:c}") != string::npos)
{
ostringstream a;
a << large;
string arg1 = "$" + a.str();
ostringstream s;
s << small;
string arg2 = "$" + s.str();
size_t pos = prompt.find("{0:c}");
prompt = prompt.replace(pos, 3, arg1);
size_t pos2 = prompt.find("{1:c}");
prompt = prompt.replace(pos2, 3, arg2);
}
else if(prompt.find("{0:e}"))
{
ostringstream a;
a << scientific << large;
string arg1 = a.str();
ostringstream s;
s << scientific << small;
string arg2 = s.str();
size_t pos = prompt.find("{0:e}");
prompt = prompt.replace(pos, 3, arg1);
size_t pos2 = prompt.find("{1:e}");
prompt = prompt.replace(pos2, 3, arg2);
}
else if(prompt.find("{0:f}"))
{
ostringstream a;
a << fixed << setprecision(6) << large;
string arg1 = a.str();
ostringstream s;
s << fixed << setprecision(6) << small;
string arg2 = s.str();
size_t pos = prompt.find("{0:f}");
prompt = prompt.replace(pos, 3, arg1);
size_t pos2 = prompt.find("{1:f}");
prompt = prompt.replace(pos2, 3, arg2);
}
else if(prompt.find("{0:i}"))
{
ostringstream a;
a << (int)large;
string arg1 = a.str();
ostringstream s;
s << (int)small;
string arg2 = s.str();
size_t pos = prompt.find("{0:i}");
prompt = prompt.replace(pos, 3, arg1);
size_t pos2 = prompt.find("{1:i}");
prompt = prompt.replace(pos2, 3, arg2);
}
cout << prompt;
cout << endl;
va_end(arguments);
}
Aucun commentaire:
Enregistrer un commentaire