trouble using loops

Hello guys,

I need some help with this program i created.



Last edited on
Try changing this part:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
bool canMeet=false;

if (((P1T1==P2T1) && (P2T1==P3T1) && (P3T1==P4T1) && (P4T1==P1T1)) == 1)
{
  canMeet=true;
  cout<< "All the four people can meet at the time 8:00-12:00 \n";
}

if (((P1T2==P2T2) && (P2T2==P3T2) && (P3T2==P4T2) && (P4T2==P1T2)) == 1)
{
  canMeet=true;
  cout<< "All the four people can meet at the time 12:00-18:00 \n";
}

if (((P1T3==P2T3) && (P2T3==P3T3) && (P3T3==P4T3) && (P4T3==P1T3)) == 1)
{
  canMeet=true;
  cout<< "All the four people can meet at the time 18:00-23:00 \n";
}

if (!canMeet) cout<< "The 4 people are not free for a meeting at the same time of the day. ";
I had to change it a little in case everyone types 2.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
bool canMeet=false;

if (((P1T1==P2T1) && (P2T1==P3T1) && (P3T1==P4T1) && (P4T1==P1T1)) && (P1T1==1))
{
  canMeet=true;
  cout<< "All the four people can meet at the time 8:00-12:00 \n";
}

if (((P1T2==P2T2) && (P2T2==P3T2) && (P3T2==P4T2) && (P4T2==P1T2)) && (P1T2==1))
{
  canMeet=true;
  cout<< "All the four people can meet at the time 12:00-18:00 \n";
}

if (((P1T3==P2T3) && (P2T3==P3T3) && (P3T3==P4T3) && (P4T3==P1T3)) && (P1T3==1))
{
  canMeet=true;
  cout<< "All the four people can meet at the time 18:00-23:00 \n";
}

if (!canMeet) cout<< "The 4 people are not free for a meeting at the same time of the day. ";
Thank you so much. It does work. But one more thing in the program i created iv written it in such a way so that if the person is free thry can input 1 if not free 0. Still i have to develop it so that the user is not allowed to input any other number, right? So how do i do it.
create a declaration at the top:
int getNum();
then change all of your "cin >> P1T1;" type calls to a corresponding
P1T1=getNum();

then add the function:
1
2
3
4
5
6
7
8
int getNum() {
  int num;
  do {
    cin >> num;
    if (num!=1 && num!=2) cout << "Enter 1 or 2 only\n";
  } while (num!=1 && num!=2);
  return num;
}


You used 1 and 2 in your original code. You might need to modify this as suits your needs for how you have it written now. ;)
Last edited on
sorry, maybe im doing it wrong. I coudnt get it write. I'm really new to this C++. Where should i add that function?? Sorry for the trouble.
Okay, here... you need to modify your code like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# include <iostream>
# include <stdio>
# include <stdlib>

int getNum();                // ADD THIS HERE

int main()

{

int P1T1;
int P1T2;
int P1T3;
int P2T1;
int P2T2;
int P2T3;
int P3T1;
int P3T2;
int P3T3;
int P4T1;
int P4T2;
int P4T3;

cout<< "Press 1: If the person is FREE\n\n (OR)\n\nPress 2: If the person is NOT FREE\n\n";
cout<< "Is the 1st person free from 8:00-12:00 : ";
P1T1 = getNum();

cout<< "Is the 1st person free from 12:00-18:00 : ";
P1T2 = getNum();                                             // CHANGE THESE ALL THE WAY DOWN

cout<< "Is the 1st person free from 18:00-23:00 : ";
P1T3 = getNum();

cout<< "Is the 2nd person free from 8:00-12:00 : ";
cin>> P2T1;

cout<< "Is the 2nd person free from 12:00-18:00 : ";
cin>> P2T2;

cout<< "Is the 2nd person free from 18:00-23:00 : ";
cin>> P2T3;

cout<< "Is the 3rd person free from 8:00-12:00 : ";
cin>> P3T1;

cout<< "Is the 3rd person free from 12:00-18:00 : ";
cin>> P3T2;

cout<< "Is the 3rd person free from 18:00-23:00 : ";
cin>> P3T3;

cout<< "Is the 4th person free from 8:00-12:00 : ";
cin>> P4T1;

cout<< "Is the 4th person free from 12:00-18:00 : ";
cin>> P4T2;

cout<< "Is the 4th person free from 18:00-23:00 : ";
cin>> P4T3;

if (((P1T1==P2T1) && (P2T1==P3T1) && (P3T1==P4T1) && (P4T1==P1T1)) == 1)
cout<< "All the four people can meet at the time 8:00-12:00 \n";

if (((P1T2==P2T2) && (P2T2==P3T2) && (P3T2==P4T2) && (P4T2==P1T2)) == 1)
cout<< "All the four people can meet at the time 12:00-18:00 \n";

if (((P1T3==P2T3) && (P2T3==P3T3) && (P3T3==P4T3) && (P4T3==P1T3)) == 1)
cout<< "All the four people can meet at the time 18:00-23:00 \n";

else
cout<< "The 4 people are not free for a meeting at the same time of the day. ";


getchar ();

}


int getNum() {                                                  // ADD THIS FUNCTION HERE
  int num;
  do {
    cin >> num;
    if (num!=1 && num!=2) cout << "Enter 1 or 2 only\n";
  } while (num!=1 && num!=2);
  return num;
}


I only changed a few of your cin >> P... expressions, so you need to finish that up yourself. I hope this helps.
It says that there is a possible use of getNum before definition
If you do decide to switch to using the value 1 representing they can meet, and 0 that they can't, then your logic statements can become much simpler:

1
2
  if ( P1T1 * P2T1 * P3T1 * P4T1 ) 
    cout << "All the four people can meet at the time 8:00-12:00\n";


Because if any of them cannot meet, the product of the multiplication will be zero, which evaluates to false, but if they all can, then 1*1*1*1 is 1, which evaluates to true.

If you are interested in using loops, don't use entirely separate variables. Use arrays.
Last edited on
Thankyou
Last edited on
Topic archived. No new replies allowed.