condition

closed account (SECMoG1T)
How do i express this condition correctly

Condition : i need data.size() to be either divisible by 2 or 3 and greater than 0,
can't understand why my solution here ain't working, yet i think i have done it right.

1
2
3
4
5
6
7
8
9
10
  while((data.size()%2!=0&&data.size()%3!=0)&&data.size()>0)
    {
      std::cout<<"\n\tAdd data : ";std::cin>>data;
    }

///note : using (data.size()%2!=0||data.size()%3!=0) doesn't work either because
/// true||false== true  meaning that it would require both %2!=0 and %3!=0 to be satisfied 

//for - (data.size()%2!=0&&data.size()%3!=0)
/// it also requires that both conditions be satisfied if am right 


I believe that this one is the only one of it's kind, it's so confusing,what do you think should be done.
thanks.
Last edited on
You seem to have the logic of the modulus incorrect. 3 % 3 should equal zero, meaning the number is divisible by 3.

Also you should consider only calling that function once:

1
2
int d;
while(( d = data.size()) > 0 && d %3 == 0 && d % 3 == 0)
Last edited on
closed account (SECMoG1T)
@jib i din't get that very clearly please, what i needed was
data.size() to be either divisible by 2 or 3 and data.size() be greater than 0,

So whenever those conditions are violated the loop should run again: so i believe my modulus logic is in-fact correct.

i would like my function to accept input whose sizes would include
2,3,4,6,8,9,10,12,14,15,16,18,20..
but now my function can only accept input with sizes==
6,12,18
.

What type of variable is data?

Edit:

Does it have a size() member function?

What does size() return?
Last edited on
closed account (SECMoG1T)
Ooh i forgot to mention data is a string variable used to hold character sequences where the size of those sequences in required before evaluation, sorry.
Last edited on
So why are you trying to see if the length of the string is divisible by two or three? Are you sure a string is the correct data type?

What are you inputting into the program?
This:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <string>
using namespace std;

int main()
{
    string data;
    
  while((data.size()%2!=0&&data.size()%3!=0)||data.size()==0) // Note ||
    {
      std::cout<<"\n\tAdd data : ";std::cin>>data;
    }
    return 0;
}
works for
2,3,4,6,8,9,10,12,14,15,16,18,20..
closed account (SECMoG1T)
Yeah @coder777 that worked thanks, also Thank @jlb for your time pal.
Last edited on
closed account (SECMoG1T)
A question please @coder7777 , it's true that the function works perfectly now but i wouldn't mind a short explanation how this

(data.size()%2!=0&&data.size()%3!=0) is working ,an lost somehow.
thanks.
I'm not sure where your problem is.

The result of data.size()%2!=0 is true when data.size() is not divisible by 2 and that is the required condition for the loop to run. The same applies to data.size()%3!=0.

If you're unsure whether to use && or ||. 'and' excludes while 'or' includes:

1,2,3 and 3,4,5 -> 3
1,2,3 or 3,4,5 -> 1,2,3,4,5

To figure out the behavior you may simulate what a certain expression or a combination thereof does:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
using namespace std;

int main()
{
  for(int i = 0; i < 10; ++i)
    {
      std::cout << "i->" << i <<": i%2=" << i%2 <<  "  i%3=" << i%3 << endl;
    }
    return 0;
}
//---
i->0: i%2=0 i%3=0
i->1: i%2=1 i%3=1 // Note: Both are not divisible
i->2: i%2=0 i%3=2
i->3: i%2=1 i%3=0
i->4: i%2=0 i%3=1
i->5: i%2=1 i%3=2 // Note: Both are not divisible
i->6: i%2=0 i%3=0
i->7: i%2=1 i%3=1
i->8: i%2=0 i%3=2
i->9: i%2=1 i%3=0
...
closed account (SECMoG1T)
Ooh yeah now I see my ill logic, thank you very much am good now.
Topic archived. No new replies allowed.