Thread: Do while loops

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    7

    Question Do while loops

    In the program I am making I'm trying to make sure that the user does not select each option more than once. The code goes in order of the first number compares with the others then second number with comparisons to the others and finally the third number. When I run the program it only runs correctly for the first number. The others just accept the value regardless if it fits within the parameters or not.

    Code:
    bool SkillCompare(int& skillone, int& skilltwo)
    {
    	if (skillone != skilltwo && skillone <= 10 && skillone >= 0)
    	{
    		return true;
    	}
    	else
    	{
    		return false;
    	}
    }
    
    if ((SkillCompare(intskill1, intskill2)) == true && ((SkillCompare(intskill1, intskill3))) == true)
    	{
    		intskill1 = intskill1;
    	}
    	else
    	{
    		do 
    		{
    		cout << "You have selected an invalid skill, please select your first skill again using the correct number:" << endl;
    		cin >> intskill1;
    		cout << endl;
    		} while ((SkillCompare(intskill1,intskill2) == false) && (SkillCompare(intskill1, intskill3) == false));
    	}
    
    
    	if ((SkillCompare(intskill2, intskill1)) == true && ((SkillCompare(intskill2, intskill3))) == true)
    	{
    		intskill2 = intskill2;
    	}
    	else
    	{
    		do 
    		{
    		cout << "You have selected an invalid skill, please select your second skill again using the correct number:" << endl;
    		cin >> intskill2;
    		cout << endl;
    		} while ((SkillCompare(intskill2,intskill1) == false) && (SkillCompare(intskill2, intskill3) == false));
    	}
    
    
    	if ((SkillCompare(intskill3, intskill1)) == true && ((SkillCompare(intskill3, intskill2))) == true)
    	{
    		intskill3 = intskill3;
    	}
    	else
    	{
    		do 
    		{
    		cout << "You have selected an invalid skill, please select your third skill again using the correct number:" << endl;
    		cin >> intskill3;
    		cout << endl;
    		} while ((SkillCompare(intskill3,intskill1) == false) && (SkillCompare(intskill3, intskill2) == false));
    	}

  2. #2
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    If your skills only range from 0 to 10, then you could use an array to store flags as to whether or not that skill has been selected.

    Initialize the array with zeros, then if the user selects a skill, then you would do
    Code:
    skills[selectedSkill] = 1;

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Code:
    bool SkillCompare(int& skillone, int& skilltwo)
    {
    	if (skillone != skilltwo && skillone <= 10 && skillone >= 0)
    	{
    		return true;
    	}
    	else
    	{
    		return false;
    	}
    }
    
    if ((SkillCompare(intskill1, intskill2)) == true && ((SkillCompare(intskill1, intskill3))) == true)
    ...and your function ends at the red parenthesis. The rest of the code does not appear to be part of any valid program structure.
    Last edited by 7stud; 12-06-2005 at 09:14 AM.

  4. #4
    Registered User
    Join Date
    Nov 2005
    Posts
    7
    Im a little confused as to what you mean Dog...please elaborate a bit more with code, i understand that better than words for some reason

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Im a little confused as to what you mean Dog...please elaborate a bit more with code, i understand that better than words for some reason
    If you think of an array filled with 0's then the elements theArray[0], theArray[1], theArray[2]....theArray[9] all are equal to 0. Now, if someone selects option 0, you can set theArray[0] to 1. If someone selects option 8, then you can set theArray[8] to 1, and if someone selects option 3, you can set theArray[3] = 1. Can you see the pattern? The option number the user selects is the same index value in the array that is set to 1. So, if someone selects option 2, you just have to check the value of theArray[2] to know if that option has been selected or not. If the value is 0, the option hasn't been selected. If the value is 1, the option has been selected. In other words, 0 means not selected, and 1 means selected.
    Last edited by 7stud; 12-06-2005 at 08:25 PM.

  6. #6
    Registered User
    Join Date
    Nov 2005
    Posts
    7
    Thanks, that helped out alot as far as clarifying it. I'm going to try and figure out how to implement it, if i get stuck, i'll be back. Thanks again to both of you

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Too many loops D:
    By F5 Tornado in forum C++ Programming
    Replies: 6
    Last Post: 12-03-2007, 01:18 AM
  2. loops with incrementing strings .... ?
    By twomers in forum C++ Programming
    Replies: 1
    Last Post: 12-12-2005, 11:29 AM
  3. strings and loops...
    By twomers in forum C Programming
    Replies: 5
    Last Post: 12-12-2005, 11:28 AM
  4. help with arrays and loops
    By jdiazj1 in forum C Programming
    Replies: 4
    Last Post: 11-24-2001, 04:28 PM
  5. for loops - newbie q's
    By Narciss in forum C Programming
    Replies: 8
    Last Post: 09-26-2001, 02:44 AM