checking to see if an array has a value stored.
Dec 28, 2012 at 12:22am UTC
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
Dec 28, 2012 at 9:38am UTC
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 ~
Dec 28, 2012 at 1:55pm UTC
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 Dec 28, 2012 at 3:54pm UTC
Dec 28, 2012 at 7:36pm UTC
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.
Dec 28, 2012 at 7:44pm UTC
for rauls suggestion it says no operator matches these commands on !=
Dec 28, 2012 at 7:46pm UTC
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!!!!!!!!!" ;
}
Dec 28, 2012 at 7:49pm UTC
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?
Dec 28, 2012 at 8:01pm UTC
never mind vlads way worked thanks guys
Dec 29, 2012 at 7:58am UTC
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.