c++ - Using a logical NOT, !, versus not using it -


i have function reads lines file , stores each string in each line in vector.

void openf(std::string s) { std::string line; std::string b; std::ifstream in; in.open(s); std::vector<std::string> vec; if(in.is_open()) {     std::cout << "file open\n" << std::endl;     while(std::getline(in,line)) {         for(decltype(line.size()) = 0; != line.size(); ++i) {             if(isspace(line[i]) || ispunct(line[i])) {                 vec.push_back(b);                 b = "";             }             else {                 b += line[i];             }         }     } } for(auto a:vec)     std::cout << << std::endl; in.close(); } 

this works.

but if instead this

if(!isspace(line[i]) || !ispunct(line[i])) {     b += line[i]; } else {     vec.push_back(b);     b = ""; } 

nothing prints.

if don't have logical or statements, , use !isspace , !ispunct individually program behaves expects in respective cases.

i don't think required tried putting () around each operator wouldn't interfere other each other. still won't work.

it seems same code. why won't work in 1 case , while works in another?

you have:

if (a || b)     else     

to negate that, need:

if (!(a || b))     else     

and !(a || b) works out (!a && !b)

so need write

if (!isspace(line[i]) && !ispunct(line[i])) 

see demorgan's laws more info.


Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -