Problem with presenting output all at once:(

Hi Programmers,

May I know why my code cannot input then output everything all at once just like the question below?

There seems to be no output when I key in 0. Can someone please advice?

Thanks.

Question:
Sample Input 2
2
AND 1 1
OR 1 0
AND 1 0
0
Sample Output 2
1
1
0

The function will input until 0. When 0 is detected, it will output.

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
    void Input2(Lines arr[100])
 57 {
 58     int i=0, N=0;
 59
 60    
 61
 62     
 63     for(i=0; arr[i].Oper!="0"; i++)
 64     {
 65         cin>>arr[i].Oper>>arr[i].s1>>arr[i].s2;
 66         N++;
 67     }
 68     for(i=0;i<N-1;i++)
 69
 70         if(arr[i].Oper=="AND")
 71         {
 72             if(arr[i].s1*arr[i].s2 == 0)
 73                 cout<<0<<endl;
 74             else
 75                 cout<<1<<endl;
 76         }
 77
 78     if(arr[i].Oper=="OR")
 79     {
 80         if(arr[i].s1+arr[i].s2>0)
 81             cout<<1<<endl;
 82         else
 83             cout<<0<<endl;
 84     }
 85     
 86     
 87
 88
 89     return;
 90 }

Last edited on
> There seems to be no output when I key in 0
I don't understand, please rephrase.


The condition in line 78 is outside the loop of line 68
The input loop (63--67) is incorrect, note its equivalent form
1
2
3
4
5
6
i = 0;
while(arr[i].Oper!="0"){ //uninitialized
   cin>>arr[i].Oper>>arr[i].s1>>arr[i].s2; //here you put some value
   N++;
   i++; //but you increased the index, so you'll check the next element (uninitialized)
}
Hi,

Thanks for your reply.

What I mean is when I key in "0" as my last input, there is no output for my code above.

I understand your method, but is there any ways I can input everything first before I output all at once?

Hope to get your advice once again:)




Input
AND 1 1
OR 1 0
AND 1 0
0

Output
1
1
0

There is no output because your reading is incorrect, the loop in line 63 never ends.
The snip that I've posted is equivalent to yours, it has the same error. I thought that it would be easier to see it for you.


1
2
3
4
5
6
7
8
9
int count = 0;
while(
   std::cin
      >>arr[count].Oper
      >>arr[count].s1
      >>arr[count].s2
   and arr[count].Oper not_eq "0"
)
   ++count;

Last edited on
Topic archived. No new replies allowed.