replacing the character in a string without another string.

I have string like this:

 
  5.46875,0.0249472,?,?,?,-1,?,186688,?,?,92 ........


and i want to replace every "?" with a double value which i m converting as string. But I am facing a problem as it only picks up the first character.
for example:
I have to replace "?" with "0.987654321" but its only picking the first character "0".


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
#include <string>
#include <cstring>
#include <sstream>

using namespace std;

template <typename T>
string ToString(T val)
{
    stringstream stream;
    stream << val;
    return stream.str();
}

int main()
{
string line = "5.46875,0.0249472,?,?,?,-1,?,186688,?,?,92";
double random = 0.987654321;

for(unsigned int i=0; i<line.length;i++)
{
  if (line[i] == '?')
  {
        string strNum = ToString(random);
        line[i] = strNum[0];      //<------I am facing problem here
        cout<<line<<endl;
   }
 }
}

Last edited on
You are using strNum[0] which gets only the first character. You probably want to use string's insert() method.
http://www.cplusplus.com/reference/string/string/insert/
line[i] is a single char of your string
i=0 (line 20)

In line 26, line[i] = line[0] which is the first char or 5.
it does not =='?' so you'll never get any output.

string is an array of char .
Make sure to read about why an array cannot have different type (double and char ???)
you are right, line[i] is a single character of string.

This code is just a sample, actual code is very big file, cant fit in here.(i have modified the code)
So i am trying to explain the problem....

I am reading from the file line by line.....and every line is similar to what i have shown above (containing "?"). I have to replace every "?" with a double value.
So i am converting every double value into string and then trying to replace "?" with that string value(converted from double value).
the problem i am facing is... it is only reading the first character.


zhuge, i dont know if string's insert method can solve the problem, as I have to replace "?" with a double value.
closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <string>

int main()
{
    std::string line = "5.46875,0.0249472,?,?,?,-1,?,186688,?,?,92";
    std::string temp = "";

    double random = 0.987654321;

    std::string replace = std::to_string(random);

for( int i = 0; i < line.length(); i++ )
{
  if (line[i] == '?')
    temp += replace;
  else
    temp += line[i];
 }
 
 std::cout << temp;
}


There's a small problem with the number of decimal places but that can probably be solved with sprintf instead of to_string().
Thanks Kemort,

I have tried this solution but the string line is actually coming from another file, there are many rows like that.
I cant copy it to temp as i am using the same string in the program further as well.

Any other solution?
1
2
3
4
5
for( size_t i = 0; i < line.size(); ++i)
    if(line[i] == '?')
        line.replace(i, 1, std::to_string(random));
//Overload (1) from
//http://en.cppreference.com/w/cpp/string/basic_string/replace 
http://coliru.stacked-crooked.com/a/272508361eab24c7
You may precalculate and store std::to_string(random) to increase perfomance. And modify i to point to the end of pasted substring.
Last edited on
Thank you MiiniPaa,
It worked! :-)
closed account (48T7M4Gy)
i am using the same string in the program further as well.


@theseus - I thought that might be the case but, like the number of digits, I didn't follow it up as it seems that both are just refinements.

'temp' can be copied back to replace 'line' if necessary, or maybe I am missing your point?

Better still, use MiiNiPaa suggestion - my choice of 'replace' was purely coincidental. Number of digits is still not solved though. :(
I thank you all for your kind help! :-)
Topic archived. No new replies allowed.