-
Validation loop problem
Im trying to add validation to my loop so that when there is no entry it comes up with an error. However the program justs skips the loop when it should loop again.
Code:
struct music{
char artist[50];
};
int valid = 0;
music song[100];
int index;
index = 0;
do
{
cout << endl << "Please enter the name of the artist: ";
cin.ignore();
cin.getline(song[ index ].artist, 100);
cout << song[ index ].artist;
if (song[ index ].artist == " ")
{
cout << "Please enter the name of an artist: ";
}
else
{
valid ++;
}
}while (valid < 1);
I've taken a lot of my code so it's easier o read for you. That's why it may look a little rough.
It's nothing to do with syntax errors or anything it just skips the validation part.
if (song[ index ].artist == " ")
I've treid so many variations of this, but nothing seems to work.
Anyone help?
Thanks in advance.
-
Code:
while (valid <= 0);
Try that. But without all the code and proper indentation it is difficult to tell what
is going on. You assigned valid to zero, so you want the if statement to execute when valid
is less or equal to zero
-
What do you mean "skips the validation part"? It doesn't even go there or always validates true or false no matter what you do?
-
No need I solved the problem!
-
>> song[ index ].artist == " "
song[ index ].artist is a C style string, and you cannot compare C style strings with ==. You should be using the C++ string class. If you cannot, then use strcmp to compare C style strings.