checking to see if an array has a value stored.

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
47
48
49
50
51
#include <iostream> 
#include <string>
#include <math.h> 
#include <fstream>
using namespace std;

int main()
{
	int i;
	string information[10][7];
	
	//input
	cout << "\nWhich Slot would you like to store the informaton in ?(1-10)";
	cin >> i;
	i--;
	for (int j=0;j<7;j++)
	{
	switch(j+1)
	{case 1: cout << "\nFirst Name: "; break;
	case 2: cout << "\nLast Name: "; break;
	case 3: cout << "\nAge: "; break;
	case 4: cout << "\nEmail: "; break;
	case 5: cout << "\nDoor Number: "; break;
	case 6: cout << "\nRoad Name: "; break;
	case 7: cout << "\nPost Code: "; break;
	default:;}		

	cin >> information[i][j];}

	// output
	for (int j=0;j<7;j++)
	{	
	switch(j+1)
	{case 1: cout << "\nFirst Name: "; break;
	case 2: cout << "\nLast Name: "; break;
	case 3: cout << "\nAge: "; break;
	case 4: cout << "\nEmail: "; break;
	case 5: cout << "\nDoor Number: "; break;
	case 6: cout << "\nRoad Name: "; break;
	case 7: cout << "\nPost Code: "; break;
	default:;}		

	cout << information[i][j];}


	system("PAUSE");



return 0;
}



Id like to check if theres any data already stored in the user selected array so data doesn't get over written?

any ideas. Yes the code is pretty basic at the moment
I think this would work:

1
2
3
4
5
6
if (information[i][j] != NULL){
     // Already written
}
else{
     // Empty
}


Hope I could help,
~ Raul ~
To check whether a string is empty you can use method empty()

For example

1
2
if ( information[i][j].empty() ) std::cout << "You should enter a data\n";
else std::cout << information[i][j];

Edit: to check whether an array contains non-empty string you can use standard algorithm std::any_of. For example

1
2
3
4
5
if ( std::any_of( std::begin( information[i] ), std::end( information[i] ),
	[]( std::string s ){ return ( !s.empty() ); } ) )
{
	std::cout << "The array is not empty\n";
}
Last edited on
thanks for the info . can I use the method from vlad from Moscow. The only headers I can use are the ones I currently have and nothing else.

Haven't tried out Rauls code yet will do.
for rauls suggestion it says no operator matches these commands on !=
1
2
3
4
5
6
7
8
9
10
11
//This bit should check if theres anything stored currently.
		cout << "\nWhich Slot would you like to store the informaton in ?(1-10)";
		cin >> i;
		i--;
		if (information[i] != NULL){
     // Already written 
			cout << "THERES SOMETHING HERE";
		}
	else{
     cout << "\nEMPTY!!!!!!!!!";
}
im having another problem now as im entering something into I to select the array it should go into so when it checks ofcourse there is because I just entered something there?
never mind vlads way worked thanks guys
Hmm, I said NULL, because I didn't think that std::string::empty would work on a matrix of strings as I had never used it o.O
But uhm.. okay, I guess it does.
Topic archived. No new replies allowed.