Validation loop problem

This is a discussion on Validation loop problem within the C++ Programming forums, part of the General Programming Boards category; Im trying to add validation to my loop so that when there is no entry it comes up with an ...

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    15

    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.
    Last edited by ManiacBR; 11-16-2006 at 04:31 AM.

  2. #2
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,475
    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
    I'm just trying to be a better person - My Name Is Earl

  3. #3
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,406
    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?
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    15
    No need I solved the problem!

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> 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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Addition problem in loop
    By murjax in forum C Programming
    Replies: 3
    Last Post: 07-01-2009, 06:29 PM
  2. validation problem in a loop (newbie question)
    By Aisthesis in forum C++ Programming
    Replies: 11
    Last Post: 05-10-2009, 10:47 PM
  3. Replies: 8
    Last Post: 12-09-2008, 11:07 AM
  4. weired problem in while loop
    By avisik in forum C Programming
    Replies: 14
    Last Post: 12-01-2008, 12:41 PM
  5. while loop problem
    By tortan in forum C++ Programming
    Replies: 2
    Last Post: 09-30-2006, 11:11 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21