简体   繁体   中英

C++ replacing part of string

I have this code that replaces the {0} and {1} in the string "The number {0} is greater than {1}" with 0 and 1 being the index of a list of variable arguments, but if inside the { } their is a specifier added, {0:[specifier]}, c for currency, f for fixed, i for int, or e for scientific, the number also gets formatted that way. The code is only working if the specifiers are the same, "The number {0:[c]} is greater than {1:[c]}", if they are different, "The number {0:[c]} is greater than {1:[f]}", then the result for some reason is both are formatted in whichever way the second index is, in the example i gave both would be formatted as fixed. I don't know why the second specifier is affecting both numbers, the values don't change but the second specifier affects both values. any one have any ideas?

I don't understand the issue, because when the program reaches the line

if(prompt.find(c2) != std::string::npos)

prompt has been modified once already so there should only be one {index:[specifier]} to be found because the first one was already replaced. Am i off on this line of thinking?

#include <iostream>
#include <iomanip>
#include <string>
#include <cstdarg>
#include <sstream> 

using namespace std;

void write(string prompt, ...);
int ConvertCharToInt(char c);

void main()
{
    write("The number {0:[i]} is greater than {1:[i]}.\n", 5.9, 1.3, 4.7, 6.8);
}

void write(string prompt, ...)
{
char c1, c2;
int pos1, pos2, num1, num2, temp;
double arg1, arg2;

va_list arguments;
va_start(arguments, prompt);

pos1 = prompt.find_first_of("{");
c1 = prompt.at(pos1+1);
pos2 = prompt.find_last_of("{");
c2 = prompt.at(pos2+1);

num1 = ConvertCharToInt(c1);
num2 = ConvertCharToInt(c2);


for(int i = -1; i < num1; i++)
{
    arg1 = va_arg(arguments, double);
}   
for(int i = num1; i < num2; i++)
{
    arg2 = va_arg(arguments, double);
}

if(prompt.find(c1) != std::string::npos)
{
    if(prompt.find("[c]") != std::string::npos)
    {
        ostringstream a;
        a << fixed << setprecision(2) << arg1;
        string r1 = "$" + a.str();

        size_t pos = prompt.find_first_of("{");
        prompt = prompt.replace(pos, 7, r1);
    }
    else if(prompt.find("[e]") != std::string::npos)
    {
        ostringstream a;
        a << scientific << arg1;
        string r1 = a.str();

        size_t pos = prompt.find_first_of("{");
        prompt = prompt.replace(pos, 7, r1);
    }
    else if(prompt.find("[f]") != std::string::npos)
    {
        ostringstream a;
        a << fixed << setprecision(6) << arg1;
        string r1 = a.str();

        size_t pos = prompt.find_first_of("{");
        prompt = prompt.replace(pos, 7, r1);
    }
    else if(prompt.find("[i]") != std::string::npos)
    {
        ostringstream a;
        a << (int)(arg1+.5);
        string r1 = a.str();

        size_t pos = prompt.find_first_of("{");
        prompt = prompt.replace(pos, 7, r1);
    }
    else
    {
        ostringstream a;
        a << arg1;
        string r1 = a.str();

        size_t pos = prompt.find_first_of("{");
        prompt = prompt.replace(pos, 3, r1);
    }
}

if(prompt.find(c2) != std::string::npos)
{
    if(prompt.find("[c]") != std::string::npos)
    {
        ostringstream s;
        s << fixed << setprecision(2) << arg2;
        string r2 = "$" + s.str();

        size_t pos2 = prompt.find_last_of("{");
        prompt = prompt.replace(pos2, 7, r2);
    }
    else if(prompt.find("[e]") != std::string::npos)
    {
        ostringstream s;
        s << scientific << arg2;
        string r2 = s.str();

        size_t pos2 = prompt.find_last_of("{");
        prompt = prompt.replace(pos2, 7, r2);
    }
    else if(prompt.find("[f]") != std::string::npos)
    {
        ostringstream s;
        s << fixed << setprecision(6) << arg2;
        string r2 = s.str();

        size_t pos2 = prompt.find_last_of("{");
        prompt = prompt.replace(pos2, 7, r2);
    }
    else if(prompt.find("[i]") != std::string::npos)
    {
        ostringstream s;
        s << (int)(arg2+.5);
        string r2 = s.str();

        size_t pos2 = prompt.find_last_of("{");
        prompt = prompt.replace(pos2, 7, r2);
    }
    else
    {
        ostringstream s;
        s << arg2;
        string r2 = s.str();

        size_t pos2 = prompt.find_last_of("{");
        prompt = prompt.replace(pos2, 7, r2);
    }
}

cout << prompt << "\n";
va_end(arguments);
}

int ConvertCharToInt(char c)
{
int num;

switch(c)
{
    case '0':
        num = 0;
        break;
    case '1':
        num = 1;
        break;
    case '2':
        num = 2;
        break;
    case '3':
        num = 3;
        break;
    case '4':
        num = 4;
        break;
    case '5':
        num = 5;
        break;
    case '6':
        num = 6;
        break;
    case '7':
        num = 7;
        break;
    case '8':
        num = 8;
        break;
    case '9':
        num = 9;
        break;
    default:
        exit(EXIT_FAILURE);
}
return num;
}

The problem is the consecutive search for your identifiers like [i], [f] and so on.

If u have at the end a [f] string.find() will ALWAYS find [f] and then leave your if-else-branches. So because the [f] branch is before the [i] branch you find in your example always [f] and print the corresponding format.

The solution is to quit seeking the whole [f] expression but to seek only for "[".

I actually got it to work with this, its extremely long and tedious but it works.

if(prompt.find("[c]") != std::string::npos)
{
    ostringstream a;
    a << fixed << setprecision(2) << arg1;
    string r1 = "$" + a.str();
    size_t pos = prompt.find_first_of("{");
    prompt = prompt.replace(pos, 7, r1);
    if(prompt.find("[c]") != std::string::npos)
    {
        ostringstream s;
        s << fixed << setprecision(2) << arg2;
        string r2 = "$" + s.str();
        size_t pos2 = prompt.find_last_of("{");
        prompt = prompt.replace(pos2, 7, r2);
    }
    else if(prompt.find("[e]") != std::string::npos)
    {
        ostringstream s;
        s << scientific << arg2;
        string r2 = s.str();
        size_t pos2 = prompt.find_last_of("{");
        prompt = prompt.replace(pos2, 7, r2);
    }
    else if(prompt.find("[f]") != std::string::npos)
    {   
        ostringstream s;
        s << fixed << setprecision(6) << arg2;
        string r2 = s.str();
        size_t pos2 = prompt.find_last_of("{");
        prompt = prompt.replace(pos2, 7, r2);
    }
    else if(prompt.find("[i]") != std::string::npos)
    {
        ostringstream s;
        s << (int)(arg2+.5);
        string r2 = s.str();
        size_t pos2 = prompt.find_last_of("{");
        prompt = prompt.replace(pos2, 7, r2);
    }
    else
    {
        ostringstream s;
        s << arg2;
        string r2 = s.str();
        size_t pos2 = prompt.find_last_of("{");
        prompt = prompt.replace(pos2, 7, r2);
    }
}

else if(prompt.find("[e]") != std::string::npos)
{
    ostringstream a;
    a << scientific << arg1;
    string r1 = a.str();
    size_t pos = prompt.find_first_of("{");
    prompt = prompt.replace(pos, 7, r1);
    if(prompt.find("[c]") != std::string::npos)
    {
        ostringstream s;
        s << fixed << setprecision(2) << arg2;
        string r2 = "$" + s.str();
        size_t pos2 = prompt.find_last_of("{");
        prompt = prompt.replace(pos2, 7, r2);
    }
    else if(prompt.find("[e]") != std::string::npos)
    {
        ostringstream s;
        s << scientific << arg2;
        string r2 = s.str();
        size_t pos2 = prompt.find_last_of("{");
        prompt = prompt.replace(pos2, 7, r2);
    }
    else if(prompt.find("[f]") != std::string::npos)
    {   
        ostringstream s;
        s << fixed << setprecision(6) << arg2;
        string r2 = s.str();
        size_t pos2 = prompt.find_last_of("{");
        prompt = prompt.replace(pos2, 7, r2);
    }
    else if(prompt.find("[i]") != std::string::npos)
    {
        ostringstream s;
        s << (int)(arg2+.5);
        string r2 = s.str();
        size_t pos2 = prompt.find_last_of("{");
        prompt = prompt.replace(pos2, 7, r2);
    }
    else
    {
        ostringstream s;
        s << arg2;
        string r2 = s.str();
        size_t pos2 = prompt.find_last_of("{");
        prompt = prompt.replace(pos2, 7, r2);
    }
}

else if(prompt.find("[f]") != std::string::npos)
{
    ostringstream a;
    a << fixed << setprecision(6) << arg1;
    string r1 = a.str();
    size_t pos = prompt.find_first_of("{");
    prompt = prompt.replace(pos, 7, r1);
    if(prompt.find("[c]") != std::string::npos)
    {
        ostringstream s;
        s << fixed << setprecision(2) << arg2;
        string r2 = "$" + s.str();
        size_t pos2 = prompt.find_last_of("{");
        prompt = prompt.replace(pos2, 7, r2);
    }
    else if(prompt.find("[e]") != std::string::npos)
    {
        ostringstream s;
        s << scientific << arg2;
        string r2 = s.str();
        size_t pos2 = prompt.find_last_of("{");
        prompt = prompt.replace(pos2, 7, r2);
    }
    else if(prompt.find("[f]") != std::string::npos)
    {   
        ostringstream s;
        s << fixed << setprecision(6) << arg2;
        string r2 = s.str();
        size_t pos2 = prompt.find_last_of("{");
        prompt = prompt.replace(pos2, 7, r2);
    }
    else if(prompt.find("[i]") != std::string::npos)
    {
        ostringstream s;
        s << (int)(arg2+.5);
        string r2 = s.str();
        size_t pos2 = prompt.find_last_of("{");
        prompt = prompt.replace(pos2, 7, r2);
    }
    else
    {
        ostringstream s;
        s << arg2;
        string r2 = s.str();
        size_t pos2 = prompt.find_last_of("{");
        prompt = prompt.replace(pos2, 7, r2);
    }
}

else if(prompt.find("[i]") != std::string::npos)
{
    ostringstream a;
    a << (int)(arg1+.5);
    string r1 = a.str();
    size_t pos = prompt.find_first_of("{");
    prompt = prompt.replace(pos, 7, r1);
    if(prompt.find("[c]") != std::string::npos)
    {
        ostringstream s;
        s << fixed << setprecision(2) << arg2;
        string r2 = "$" + s.str();
        size_t pos2 = prompt.find_last_of("{");
        prompt = prompt.replace(pos2, 7, r2);
    }
    else if(prompt.find("[e]") != std::string::npos)
    {
        ostringstream s;
        s << scientific << arg2;
        string r2 = s.str();
        size_t pos2 = prompt.find_last_of("{");
        prompt = prompt.replace(pos2, 7, r2);
    }
    else if(prompt.find("[f]") != std::string::npos)
    {   
        ostringstream s;
        s << fixed << setprecision(6) << arg2;
        string r2 = s.str();
        size_t pos2 = prompt.find_last_of("{");
        prompt = prompt.replace(pos2, 7, r2);
    }
    else if(prompt.find("[i]") != std::string::npos)
    {
        ostringstream s;
        s << (int)(arg2+.5);
        string r2 = s.str();
        size_t pos2 = prompt.find_last_of("{");
        prompt = prompt.replace(pos2, 7, r2);
    }
    else
    {
        ostringstream s;
        s << arg2;
        string r2 = s.str();
        size_t pos2 = prompt.find_last_of("{");
        prompt = prompt.replace(pos2, 7, r2);
    }
}

else
{
    ostringstream a;
    a << arg1;
    string r1 = a.str();
    size_t pos = prompt.find_first_of("{");
    prompt = prompt.replace(pos, 3, r1);
    if(prompt.find("[c]") != std::string::npos)
    {
        ostringstream s;
        s << fixed << setprecision(2) << arg2;
        string r2 = "$" + s.str();
        size_t pos2 = prompt.find_last_of("{");
        prompt = prompt.replace(pos2, 7, r2);
    }
    else if(prompt.find("[e]") != std::string::npos)
    {
        ostringstream s;
        s << scientific << arg2;
        string r2 = s.str();
        size_t pos2 = prompt.find_last_of("{");
        prompt = prompt.replace(pos2, 7, r2);
    }
    else if(prompt.find("[f]") != std::string::npos)
    {   
        ostringstream s;
        s << fixed << setprecision(6) << arg2;
        string r2 = s.str();
        size_t pos2 = prompt.find_last_of("{");
        prompt = prompt.replace(pos2, 7, r2);
    }
    else if(prompt.find("[i]") != std::string::npos)
    {
        ostringstream s;
        s << (int)(arg2+.5);
        string r2 = s.str();
        size_t pos2 = prompt.find_last_of("{");
        prompt = prompt.replace(pos2, 7, r2);
    }
    else
    {
        ostringstream s;
        s << arg2;
        string r2 = s.str();
        size_t pos2 = prompt.find_last_of("{");
        prompt = prompt.replace(pos2, 7, r2);
    }
}

cout << prompt << "\n";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM