c++ - How would I go on as to "return" string arrays between functions? -
hey experimenting knew , realized when tried passing string value return wasn't supported, idea? sorry if code noob style (i have 2 months of experience), planning on splitting code between functions can't seem because returning array of strings cant done return :( here's code:
#include <iostream> #include <math.h> #include <sstream> using namespace std; int itemlist=0; int c=0; int r = 0; int itemlistdec() { cout<<"how many items input?"; cin>>itemlist; return itemlist; } int main() { itemlistdec(); string item[4][itemlist];//declares item , columns equal itemlist content declared right above (int c=0;c<itemlist;c++) { (int r=0;r<3;r++) //determines each record goes { if (r==0) { cout<<"please enter name of item "; } if (r==1) { cout<<"please input buying price\n"; } if (r==2) { cout<<"please input selling price\n"; } cin>>item[r][c]; } } int calc[3][itemlist];//declaring calc , itemlist (int r = 0;r<itemlist;r++) { istringstream(item[1][r])>>calc[0][r]; //takes buying price int array calculation } (int r = 0;r<itemlist;r++) { istringstream(item[2][r])>>calc[1][r]; //takes selling price int array calculation } (int fart = 0;fart<itemlist;fart++) { calc[2][fart] = calc[1][fart] - calc[0][fart]; //repeats calculation profit until itemlist reached } (int r = 0;r<itemlist;r++) { stringstream ss; ss<<calc[2][r]; //converts both profit values strings item[3][r] = ss.str(); } cout<<"______________________________________________\n"; //displays output in table form cout<<"item\t\tbuying price\t\tselling price\t\tprofit\n"; (int c=0;c<itemlist;c++) { (int r=0;r<4;r++) { cout<<item[r][c]<<"\t\t"; if (r==1) { cout<<"\t"; } if (r==2) { cout<<"\t"; } if (r==3) { cout<<"\n"; } } } return 0; }
i think can use vector, it's powerful. that:
std::vector<std::vector<std::string> > mysecondvector; std::vector<std::string> myfirstvector; myfirstvector.push_back("mystring"); mysecondvector.push_back(myfirstvector); mysecondvector[i][j]; // access
and add, access element watch on http://www.cplusplus.com/reference/vector/vector/
Comments
Post a Comment