c++ - 我将如何继续处理函数之间的 "return"字符串数组?

标签 c++

嘿,当我尝试通过不支持返回的字符串值时,我正在试验我所知道和意识到的,知道吗?抱歉,如果我的代码是菜鸟风格(我只有 2 个月的经验),我正计划在函数之间拆分代码,但我似乎做不到,因为返回我的字符串数组不能用 return :( 这是代码:

    #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 would you like to input?";
cin>>itemlist;  
return itemlist;
}


int main() {


itemlistdec();
string item[4][itemlist];//declares item and that columns equal to itemlist whose content is declared right above

    for (int c=0;c<itemlist;c++)
    {

        for (int r=0;r<3;r++) //DETERMINES WHERE EACH RECORD GOES
        {
        if (r==0)
        {

        cout<<"Please enter the name of the item ";
        }

        if (r==1)
        {
            cout<<"Please input the buying price\n";
        }

        if (r==2)
        {
            cout<<"Please input the selling price\n";
        }

            cin>>item[r][c];

        }
    }

    int calc[3][itemlist];//declaring calc and itemlist

    for (int r = 0;r<itemlist;r++)
    {
        istringstream(item[1][r])>>calc[0][r]; //TAKES BUYING PRICE INTO INT ARRAY FOR CALCULATION

    }

    for (int r = 0;r<itemlist;r++)
    {
        istringstream(item[2][r])>>calc[1][r]; //TAKES SELLING PRICE INTO INT ARRAY FOR CALCULATION

    }

    for (int fart = 0;fart<itemlist;fart++)
    {
        calc[2][fart] = calc[1][fart] - calc[0][fart]; //REPEATS CALCULATION FOR PROFIT UNTIL ITEMLIST IS REACHED
    }


    for (int r = 0;r<itemlist;r++)
    {

    stringstream ss;
    ss<<calc[2][r]; //CONVERTS BOTH PROFIT VALUES INTO STRINGS
    item[3][r] = ss.str();

    }


    cout<<"______________________________________________\n"; //DISPLAYS OUTPUT IN TABLE FORM
    cout<<"Item\t\tBuying Price\t\tSelling Price\t\tProfit\n";

        for (int c=0;c<itemlist;c++)
    {


        for (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;
}

最佳答案

我认为你可以使用 vector ,它非常强大。像那样:

std::vector<std::vector<std::string> > mySecondVector;
std::vector<std::string> myFirstVector;
myFirstVector.push_back("MyString");
mySecondVector.push_back(myFirstVector);
mySecondVector[i][j]; // to access

对于添加,访问 http://www.cplusplus.com/reference/vector/vector/ 上的元素 watch

关于c++ - 我将如何继续处理函数之间的 "return"字符串数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31353036/

相关文章:

c++ - 是否可以创建指向函数的 `new` 运算符/构造函数的函数指针?

c++ - C++ 的 HTTP 和 HTTPS 服务器库

c++ - 如何按元素出现次数将多重集排序到容器

c++ - 如何将 boost::msm 状态机的实现传播/拆分为多个文件?

c++ - 在这种情况下是否有必要使用 unique_ptr?

c# - 从 C# COM 方法返回对象和内存所有权

c++ - 引用传递 VS 值传递

c++ - 循环枚举值

c++ - 测试 QQuickView (QWindow) 在 Qt 5.0.x 中是否全屏

C++语法帮助,使用模板