0

Here is a code in which I've been facing difficulties lately-

int main(){

ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n,k;
cin >> n >> k;
cin.ignore();
int arr[n];
fill_n(arr , n, 0);
int brr[n];
fill_n(brr , n, 1);
for(int i=1;i<=k;i++){ //Loop 1
    char str[8];
    cin.getline(str,9);
    if(str=="CLOSEALL") //block 1
        memset(arr , 0, n * sizeof(arr[0]));
    else{
        arr[str[6]-'0'-1]=1*brr[str[6]-'0'-1];
        if(brr[str[6]-'0'-1]==0)
            brr[str[6]-'0'-1]=1;
        else
            brr[str[6]-'0'-1]=0;
            }
    int c=0;
    for(int j=0;j<n;j++){
        if(arr[j]==1)
        c++;
    }

    cout << c << endl;
}

The problem I've been facing is with the input. According to the question, the input commands can be CLICK N, where N is an integer, or CLOSEALL (note the exact case and number of whitespaces). Whenever I work with an input of CLICK N, the program runs fine, but as soon as I input CLOSEALL, it doesn't execute the block 1(mentioned in the code) and the program terminates immediately, even if Loop 1(mentioned in the code) has to go on till the input value of k. I assume that it's some problem related to the getline() buffer, as CLOSEALL doesn't have any whitespace and is malfunctioning, while CLICK N has a whitespace and isn't malfunctioning. Can someone suggest how to correct this flush discrepancy, so that the code works perfectly?

PS: I haven't included the question, as I believe it's of no use here. Also, I've included the packages in my code as required. If someone needs the question, I would edit my post.

  • 2
    Whatever website you are using, it's not doing a good job at teaching. I suggest getting [a good C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) to learn from. `char[8]` can only store 7 characters, because it needs space for null terminator. `str=="CLOSEALL"` does not do any useful comparison, it will always be `false`. `int arr[n];` is a Variable Length Array and only some compilers support it as extension to C++. – Yksisarvinen Jul 23 '21 at 09:53
  • I'm using VSCODE, it does give true on comparison with `CLOSEALL`, I've tried that out. – Abhinav Tahlani Jul 23 '21 at 10:55
  • 1
    There is no way in which comparison with `==` of local `char[]` to string literal can produce `true`. It does pointer comparison, and these pointers must be pointing to different memory areas. And even if it did, it has different semantics, for example `"asdf" == "asdfghjk"` could produce `true`. For comparison of C-style strings there exists [`std::strcmp`](https://en.cppreference.com/w/cpp/string/byte/strcmp), or, if you used C++ `std::string` instead of `char[]` you can use `==` and it will correctly compare. – Yksisarvinen Jul 23 '21 at 11:10
  • Right. Previously I had worked with string, and it had worked, but then I needed getline to input the stream. Realized it now. Thanks for pointing it out. – Abhinav Tahlani Jul 23 '21 at 12:18

1 Answers1

1

you can try std::stringstream

// swapping ostringstream objects
#include <string>       // std::string
#include <iostream>     // std::cout
#include <sstream>      // std::stringstream

int main () {
  std::string str;
  std::cin >> str;
  std::stringstream ss;

  ss << str;

  int foo,bar;
  ss >> foo >> bar;

  std::cout << "foo: " << foo << '\n';
  std::cout << "bar: " << bar << '\n';

  return 0;
}
// in: 
// 100 200
// out: 
// foo: 100
// bar: 200

Yksisarvinen
  • 18,008
  • 2
  • 24
  • 52
Vincent55
  • 33
  • 3