Test if Array is Empty

Jan 18, 2014 at 6:09am
I'm trying to test an element of an array to see if it has information stored in it. If it does I'm going to skip that element and move on to the next one, which I will test again, and so on.

How do I finish that if statement so that if information[0] doesn't have anything stored in it (besides the default constructor's information) I can consider that empty and allow the user to input information?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include "Friend.h"
#include <iostream>
const int SIZE = 5;

Friend::Friend (){
	screenName = " ";
	interests = " ";
	age = 0;
}

Friend::Friend(std::string inputScreenName, std::string inputInterests, int inputAge){
	screenName = inputScreenName;
	interests = inputInterests;
	age = inputAge;
}

std::string Friend::getScreenName(){
	return screenName;
}

std::string Friend::getInterests(){
	return interests;
}

int Friend::getAge(){
	return age;
}

void Friend::AddFriend(Friend information[], int SIZE){
bool empty = false;


cout << "*** Add a new friend profile" << endl;


// This is where I'm running into trouble	
for (int i = 0; i < SIZE-1; i++){
	
	if(information[i] == 0){
}


}


}
Jan 18, 2014 at 6:19am
It's probably better if you make a function that checks if a Friend object holds valid information and returns true or false.
Jan 18, 2014 at 8:13am
Do you mean like adding tests for valid input values in my non-default constructor? Either way, do you mind showing me an example.

I had another question. Why doesn't the following code work?
 
information[0] = {"John Doe", "Sky Diving", 30};

----------------------------^---------
Edit: Forgot to mention the error it gives me, it says, "Error: expected an expression." The error is occurring where the carrot above is pointing to.
Last edited on Jan 18, 2014 at 8:16am
Jan 18, 2014 at 8:39am
> Why doesn't the following code work?
> information[0] = {"John Doe", "Sky Diving", 30};

Needs C++11 (and a suitable non-explicit constructor).


> showing me an example.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>
#include <string>

struct friend_info
{
    friend_info() = default ;
    friend_info( std::string n, std::string i = "", int a = 0 )
            : name(n), interests(i), age(a) {}

    std::string name ;
    std::string interests ;
    int age = 0 ;

    bool nothing() const { return name.empty() && interests.empty() && age==0 ; }
};

void foo( const friend_info info[], std::size_t sz )
{
    for( std::size_t i = 0 ; i < sz ; ++i )
    {
        if( !info[i].nothing() )
        {
            // do something with it
            std::cout << "name: " << info[i].name << '\n' ;
        }
        else std::cout << "(nothing)\n" ;
    }
}

int main()
{
    friend_info information[] = { {}, {"xxxx", "y", 23}, {}, {"zzz"}, {} } ;
    information[0] = {"John Doe", "Sky Diving", 30} ;
    foo( information, sizeof(information) / sizeof( information[0] ) ) ;
}

http://coliru.stacked-crooked.com/a/3d61567aafe27cdc
Jan 18, 2014 at 9:00am
Do you mean like adding tests for valid input values in my non-default constructor?
I suppose that could work too. Eliminating the possibility of creating invalid objects eliminates the need to check their validity. But that requires every object to be valid from contruction to destruction, which may or may not be doable.

What I meant was to do something like
1
2
3
4
5
6
bool Friend::IsValid() const
{
    if(OBJECT_CONTAINS_VALID_DATA_CONDITIONS)
        return true;
    return false;
}

1
2
3
4
if(information[0].IsValid())
{
    //etc
}


-----
I didn't know brace assignment worked, I thought it only worked on initialization.
Anyway, according to an answer on stackoverflow, brace assignment works only for aggregate data types.
Last edited on Jan 18, 2014 at 9:16am
Jan 18, 2014 at 9:31am
Thanks for the reply's!
Topic archived. No new replies allowed.