Thread: compare question

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    10

    compare question

    Hey all, is it possible to use an if statement or strcmp to compare more than one thing?
    IE
    I want to compare a variable (count) to 5, 10, 15, 20, 25, 30, 35, 40, --> 100
    and pause if the variable is equal to any one of them.
    something like this
    Code:
    void displayall()
    {
    	int i;
    	int count=0;
    	system("cls");
    	for (i=0; i<99; i++)
    	{
    		if (!strcmp ((entry[i].name), "*"))
    		{
    		}
    		else 
    		{
    			count++;
    			cout << (entry[i].name) << "\n" << flush;
    			cout << (entry[i].street) << "\n" << flush;
    			cout << (entry[i].streetii) << "\n" << flush;
    			cout << (entry[i].postcode) << "\n\n" << flush;
    			if (count==5,10,15,20,25) // <------ can you do something like this?? obviously this wont work but you get the picture.
    			{
    				system("pause");
    			}
    		}
    	}
    }//end function
    or perhaps use strcmp ??? if so how would you format the statement?
    any help greatly appreciated. I am using VC++ 6.
    thanks
    DigiAcid

  2. #2
    Registered User
    Join Date
    Jan 2009
    Posts
    31
    Well, there are probably several ways to accomplish such, though none nearly as convenient as what you're trying to do.

    By the way, you don't even need such a method for what you're trying to do... as you can use the modulus operator.

    Code:
    if(count % 5 == 0)
    No matter, here are some ways of doing such:

    *) A switch statement:

    Code:
    switch(count) {
       case 5: case 10: case 15: case 20:
       case 25: ....
           // Do something
    }
    *) Use std::set<T> or std::map<T, bool>
    Code:
    std::set<int> set_of_ints;
    set_of_ints.insert(5);
    set_of_ints.insert(10);
    ...
    set_of_ints.insert(95);
    
    if(set_of_ints.find(count) != set_of_ints.end()) {
       // Do something
    }
    The nice thing about this method is that it can work with any data type with reasonable effort (unlike the switch).

    You could also implement the same idea with an array, or even a std::bitset, if your data type is only an integer.

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    10
    thanks for your reply m8 I must say I dont really understand the code youve put at the bottom as my lecturer is teaching us an arcaic form of c++ , and I can't understand anything he says (hes iranian with little grasp of the english language), and namespace std is lost on me

    if(count % 5 ==0) ??

    could you explain in longer terms what this means as I need to understand everything in my code when asked.

    I get this much: if ( conditions in here are true then do whats in the brackets below)

    is it if count is devisable by 5 then do whats in the brackets???

    thanks for your help its much appreciated

  4. #4
    Registered User
    Join Date
    Jan 2009
    Posts
    31
    Quote Originally Posted by DigiAcid View Post
    if(count % 5 ==0) ??

    could you explain in longer terms what this means as I need to understand everything in my code when asked.
    % is the modulus operator. It gives one the remainder of the division of the two operands. So 5 divides 5 with a remainder of 0, as it does 10, 15, 20, and so on.

    is it if count is devisable by 5 then do whats in the brackets???
    Exactly. In the brackets would be the code to execute if count is a multiple of 5.

  5. #5
    Registered User
    Join Date
    Mar 2009
    Posts
    10
    Ah excellent thanks very much for your help it is greatly appreciated

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  2. Replies: 7
    Last Post: 04-13-2003, 10:53 PM
  3. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 09:22 PM
  4. Question about linked lists.
    By cheeisme123 in forum C++ Programming
    Replies: 6
    Last Post: 02-25-2003, 01:36 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM