1

So I've been looking at answers for how to convert my string vector into a double vector. Everyone seems to be of the opinion that either

ss.str(std::string());
ss.str();
ss.str("");

should work. I've tried them though and it's a no go.

I've done this before using stringstream but this time the cache in the stream isn't clearing and my output is just a bunch of successively concatenated numbers.

For instance, I input 7 9 58 33. I get:

7 79 7958 795833

This is inside a while loop to, which I'm assuming is complicating things but I don't think it should be a problem. Here's the code. The function call I'm referring to is real2double. The problem is in the last for loop of the function.

#include <iostream>
#include <String>
#include <vector>
#include <sstream>

using namespace std;

#ifndef Comp_Num
class Comp_Num
{
private:
    std::vector <double> real;
    std::vector <double> imag;
    double real_in, imag_in;
    string operation;
public:
    Comp_Num();
    Comp_Num(std::vector <string> complex_nums);
    static std::vector <string> get_input();
    static std::vector <string> get_real(std::vector <string> complexnum1);
    static std::vector <string> get_imag(std::vector <string> complexnum2);
    static void display(std::vector <string> display1);
    static void display2(std::vector <double> display3);
    static std::vector <string> set_compnum(std::vector <string> set_num);
    static std::vector <string> comp2real(std::vector <string> c2r);
    static vector <double> real2double (std::vector<string> r2d);
    ~Comp_Num();
/*
    double Comp_Num::operator+ (double add_num);
    double Comp_Num::operator- (double sub_num);
    double Comp_Num::operator* (double mul_num);
    double Comp_Num::operator/ (double div_num);
*/
};
#endif !Comp_Num

Comp_Num::Comp_Num(std::vector <string> complex_nums)
{


    /*  
    std::vector <string> input_fin;
    input_fin = Comp_Num::get_input();
    Comp_Num::display(real1);
    std::cout<<"What operation would you like to perform\n";
    std::cout<<"You can choose from addition, subtraction, multiplication, or division.\n";
    std::cin>>operation>>"\n";
    */  
};

std::vector <string> Comp_Num::set_compnum(std::vector <string> set_num)
{
    std::vector<string>comp1=Comp_Num::get_real(set_num);
    std::vector <string>comp2= Comp_Num::get_imag(set_num);
    std::vector <string>cmp2rl_fin = Comp_Num::comp2real(comp2);
    std::vector <double>finale = Comp_Num::real2double(cmp2rl_fin);
    Comp_Num::display(comp1);
    Comp_Num::display(comp2);
    Comp_Num::display(cmp2rl_fin);
    Comp_Num::display2(finale);
    system("pause");
    return(comp2);
};

std::vector <string> get_input()
{
    std::string usr_input, input1;
    std::vector <string> complex;
    std::cout<<"Enter the real or imaginary numbers you would like to perform your operation on.\n";
    std::cout<<"Enter abc for your last value.\n";

    std::getline(std::cin, usr_input);
    std::istringstream input(usr_input);
    while(input >> input1)
    {
        if(input1 == "abc")
            break;
        complex.push_back(input1);
    }
    std::cout<<'\n';
    return (complex);
};

std::vector <string> Comp_Num::get_imag(std::vector <string> complexnum2)
{
    std::vector <string> imag1;
    for(unsigned int i = 0; i < complexnum2.size(); ++i)
    {
        std::string str2 = "";
        std::vector<char> char1(complexnum2[i].begin(), complexnum2[i].end());
        for(unsigned int r = 0; r < char1.size(); ++r)
        {
            if(char1[r] >= '0' && char1[r] <= '9' || char1[r] == '-' || char1[r] == '.' || char1[r] == 'i')
            {
                str2 += char1[r];
                if(char1[r] == 'i')
                {
                    imag1.push_back(str2);  
                    continue;
                }
            }
        }
    }
    return(imag1);
};

std::vector <string> Comp_Num::get_real(std::vector <string> complexnum1)
{
    std::vector <string> real1;
    for(unsigned int i = 0; i < complexnum1.size(); ++i)
    {
        std::string str2 = "";
        std::vector<char> char1(complexnum1[i].begin(), complexnum1[i].end());
        for(unsigned int r = 0; r < char1.size(); ++r)
        {
            if(char1[r] >= '0' && char1[r] <= '9' || char1[r] == '-' || char1[r] == '.' || char1[r] == 'i')
            {
                str2 += char1[r];
                if(char1[r] == 'i')
                {
                    str2="";
                    break;
                }
            }
        }
        real1.push_back(str2);
    }
    return(real1);
};

void Comp_Num::display (std::vector <string> display1)
{
    unsigned int i = 0;
    while (i < display1.size())
    {
        std::cout<<display1[i]<<" ";
        i++;
    }
    std::cout<<std::endl;
};

void Comp_Num::display2 (std::vector <double> display3)
{
    unsigned int i = 0;
    while (i < display3.size())
    {
        std::cout<<display3[i]<<" ";
        i++;
    }
    std::cout<<std::endl;
};

std::vector <string> Comp_Num::comp2real(std::vector <string> c2r)
{
    std::vector <string> real2;
    for(unsigned int i = 0; i < c2r.size(); ++i)
    {
        std::string str2 = "";
        std::vector<char> char1(c2r[i].begin(), c2r[i].end());
        for(unsigned int r = 0; r < char1.size(); ++r)
        {
            if(char1[r] >= '0' && char1[r] <= '9' || char1[r] == '-' || char1[r] == '.')
            {
                str2 += char1[r];
            }
        }
        real2.push_back(str2);
    }
    return(real2);
}

std::vector<double> Comp_Num::real2double (std::vector<string> r2d)
{
    double y;
    std::vector<double> final_r2d;
    std::string str, str2;
    std::vector<string>str3;
    for(unsigned int i = 0; i < r2d.size(); ++i)
    {
        std::vector<char> ch(r2d[i].begin(), r2d[i].end());
        for(unsigned int r = 0; r < ch.size(); ++r)
        {
            if(ch[r] >= '0' && ch[r] <= '9' || ch[r] == '-' || ch[r] == '.')
            {
                str2 += ch[r];

            }
        }
        str3.push_back(str2);
    }
    for(unsigned int r = 0; r < str3.size(); r++)
    {
//      std::string open = "";
//      istringstream to_double(open);
        str = str3[r];
        std::istringstream to_double(str);
        while(to_double >> y)
        {
            final_r2d.push_back(y);
            to_double.str(std::string());
        }

    }
    return(final_r2d);
};
2
  • Try to_double.str("") instead of to_double.str(std::string()). Commented Mar 24, 2014 at 18:34
  • No that doesn't work either. i tried it before and just tried it again. Commented Mar 24, 2014 at 18:36

1 Answer 1

1

The problem is in the first loop of real2double where you keep appending to the variable

str2 += ch[r];

You must move the declaration into the loop:

for(unsigned int i = 0; i < r2d.size(); ++i)
{
  std::string str2;

Later Do away with code duplication:

template <class T>
static void display(std::vector <T> display);

template <class T>
void Comp_Num::display (std::vector <T> dis) {
  for( typename std::vector<T>::iterator it = dis.begin() ; it != dis.end(); ++it ){
    std::cout << *it << " ";
  }
  std::cout<<std::endl;
}

Avoid loops whenever possible:

std::vector<double> Comp_Num::real2double (std::vector<string> r2d) {
  for( std::vector<string>::iterator it = r2d.begin() ; it != r2d.end(); ++it ){
    int iPos = (*it).find_first_not_of( "0123456789-." );
    double y;
    std::istringstream( (*it).substr( 0, iPos ) ) >> y;
    final_r2d.push_back( y );
  }
  return final_r2d;
}
Sign up to request clarification or add additional context in comments.

2 Comments

The code is needlessly complex, and this is what trips you up.
Yeah, that did it. How would you recommend I make it less complex?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.