-1

I have been trying to solve a codechef competition question. In my program logic, everything works fine for the 1st and 2nd iteration and gives the desired result but during the second iteration the cin statement is completely getting ignored. However the next line having cout<<" ss "; works just fine.

I have already searched for possible workarounds on stack overflow, they said to use cin.clear() and cin.ignore() and I used it but still no effect. Am i doing something wrong?

Here is the link to the problem https://www.codechef.com/problems/DIET

#define REP(limit) for(int i = 0;  i < limit; i++)
#define REP2(limit2) for(int j = 0; i < limit2; j++)


int main()
{
    int testcases;
    cin>>testcases;
    while(testcases--){
        int days, diet, current  = 0;
        cin>>days>>diet;
        bool res = true;

        REP(days){
            int x;
            cin>>x;
            cout<<"  ss "; 
            current += x;

            if(current < diet){
                cout<<"NO "<<i+1;
                res = false;
                break;
            }

            current -= diet;
        }
        if(res == true){
            cout<<"YES";
        }
    }
    return 0;
}
deqyra
  • 734
  • 1
  • 7
  • 23
jrseinc
  • 1
  • 2
  • Possible duplicate of https://stackoverflow.com/questions/10553597/cin-and-getline-skipping-input – deqyra Nov 19 '19 at 10:26
  • _"the cin statement is completely getting ignored."_ What do you mean with this? – Lukas-T Nov 19 '19 at 10:26
  • Typo in `#define REP2(limit2) for(int j = 0; i < limit2; j++)` - should be : `#define REP2(limit2) for(int j = 0; j < limit2; j++)`. If this fixes your issue, please close the question as off-topic (simple typo's do not make good questions to keep). – Sander De Dycker Nov 19 '19 at 10:28
  • @SanderDeDycker True, but REP2 isn't used in this piece of code. – Lukas-T Nov 19 '19 at 10:29
  • @SanderDeDycker Thank you for pointing out that mistake in REP2 Macro I defined. But I'm not using it in the program anywhere so correcting this doesn't solve my issue. – jrseinc Nov 19 '19 at 10:34
  • I see none of the usual reasons for which input may appear to fails, thus I think your issue is in the code logic. For instance, I find strange that you sometimes exit the `days` loop with a break. – AProgrammer Nov 19 '19 at 10:35
  • @churill The program runs fine for the first two iterations and gives the desired result for the respective test cases but during the 3rd iteration, it doest allow be to input x. It skips the cin statement and moves on to the next line which is cout<<" sss"; – jrseinc Nov 19 '19 at 10:37
  • @churill : whoops ... guess I need to get my eyes checked. – Sander De Dycker Nov 19 '19 at 11:00

1 Answers1

0

Nothing is being ignored - you actually have the opposite problem, of ignoring too little.

If you reach the conclusion "NO", you break out of the loop, leaving the rest of that line in the input buffer.

With the posted test cases, you will read 3 in the second case and print "NO", which means that the third test case will read 10 10 as the "days" and "diet", and 3 4 8 1 1 as the intakes.
(You would have noticed this fairly quickly if you had printed the values you read instead of just "ss".)

Read one entire line at a time or use std::ignore.

molbdnilo
  • 64,751
  • 3
  • 43
  • 82