c++ - Clearing stringstream in the midst of a while loop? -
so i've been looking @ answers how convert string vector double vector. seems of opinion either
ss.str(std::string()); ss.str(); ss.str("");
should work. i've tried them though , it's no go.
i've done before using stringstream time cache in stream isn't clearing , output bunch of successively concatenated numbers.
for instance, input 7 9 58 33. get:
7 79 7958 795833
this inside while loop to, i'm assuming complicating things don't think should problem. here's code. function call i'm referring real2double. problem in last loop of 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 perform\n"; std::cout<<"you can choose 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 real or imaginary numbers perform operation on.\n"; std::cout<<"enter abc 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 = 0; < 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 = 0; < 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 = 0; while (i < display1.size()) { std::cout<<display1[i]<<" "; i++; } std::cout<<std::endl; }; void comp_num::display2 (std::vector <double> display3) { unsigned int = 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 = 0; < 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 = 0; < 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); };
the problem in first loop of real2double keep appending variable
str2 += ch[r];
you must move declaration loop:
for(unsigned int = 0; < r2d.size(); ++i) { std::string str2;
later away 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 = dis.begin() ; != 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 = r2d.begin() ; != 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; }
Comments
Post a Comment