C ++ if(Array [x] .length> 0)

时间:2016-05-20 07:45:16

标签: c++ arrays if-statement

我试图创建一个简单的数据库程序,使用户能够添加,删除和编辑字符串变量。

我遇到的问题:将每个添加的变量分配给下一个空数组元素。

-I'我现在使用静态数组,直到我了解有关动态数组的更多信息。

- 由于这已经脱离了上下文,我在这里重新创建了代码以使其更有意义,如果我错过了任何内容,对不起。

//static array declaration
std::string names[5];

//string selection from main function
std::string stringSel = "Item";

//boolean condition for loop
bool completed = false;

//if (names[x].length > 0) then increment x by 1 until names[x].length == 0 and then set that array element to the value from stringSel
int x = 0
while(completed == false)
{
    //if the length of the element is greater than zero characters, increment the element
    if (names[x].length > 0)
    {
        x++;
    }
    //if the length of the element is not greater than zero characters, set the string to that element
    else
    {
        //The empty element is assigned the string and the entire array prints out
        names[x] = stringSel;
        std::cout << stringSel << " was added to the database.\nThis is the printout of all current items: " << names;
        completed = true;
    }
}

我得到的错误来自我的if语句条件。 &#39;。&#39;带下划线的红色

1智能感知:指向绑定功能的指针只能用于调用函数

1 个答案:

答案 0 :(得分:4)

names[x].length

std::string::length()是一个成员函数,而不是数据成员,因此要访问它,您需要进行如下函数调用:

names[x].length()