-3
#include <iostream>
using namespace std;
int main()
{
  bool result;
  char text[1000];
  cin>>text;
  int len=sizeof(text);
  for(int i = 0 ;i<len; ++i)
  {
    if(text[i]=='t' && text[i+1]=='r' && text[i+2]=='u' && text[i+3]=='e') 
        result = true;
    else if(text[i]=='f' && text[i+1]=='a' && text[i+2]=='l' && text[i+3]=='s' && text[i+4]=='e')
        result = false;

  }
  for(int i = 0 ;i<len; ++i)
  {
      if(text[i]=='n' && text[i+1]=='o' && text[i+2]=='t')
          result = !result;// i think here is the problem
  }
  if(result == true)
      cout<<"true"<<endl;
  else if(result == false)
      cout<<"false"<<endl;
return 0;

the exercise: A boolean value can be either True or False. Given a string with less than 1000 characters with a number of space-separated not directives terminated by a True or False value, evaluate the boolean expression. but when i run the program the result is always true. please can you tell me where is the problem

Haytam Rahimi
  • 19
  • 1
  • 3
  • Be careful, `sizeof(text)` does not give you the length of the actual *input text*, that would be `strlen(text)`. Using `sizeof(text)` as the length migh cause you to go beyond the string terminator and into uninitialized memory of the array, where the elements have *indeterminate* values and that will lead to *undefined behavior*. – Some programmer dude Jul 17 '17 at 22:43
  • `text[i]=='t' && text[i+1]=='r' && text[i+2]=='u' && text[i+3]=='e'` What the hell? [Y U NO USE STD::STRING](https://cdn.meme.am/cache/instances/folder409/500x/78941409/y-u-no-y-u-no-use-stdstring.jpg) – Henri Menke Jul 17 '17 at 22:44
  • Not to mention that indexes like `i + 1` ***will*** take you *out of bounds* which is also *undefined behavior*. And once you got [*undefined behavior*](http://en.cppreference.com/w/cpp/language/ub) then your whole program is *ill-formed* and invalid. Whatever result you got can't simply be trusted. – Some programmer dude Jul 17 '17 at 22:44
  • As for solving problems like yours, once you actually have something that doesn't give you undefined behavior, please read [How to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) by Eric Lippert. – Some programmer dude Jul 17 '17 at 22:51

1 Answers1

-1

Why don't you just use what is already there?

#include <iostream>
#include <iterator>
#include <sstream>
#include <stdexcept>
#include <string>
#include <vector>

int main()
{
  bool result;

  // Read the line
  std::string line;
  std::getline(std::cin, line);

  // Split the line at spaces (https://stackoverflow.com/a/237280/1944004)
  std::istringstream iss(line);
  std::vector<std::string> tokens{std::istream_iterator<std::string>{iss}, std::istream_iterator<std::string>{}};

  // Convert last element to bool
  if (tokens.back() == "true") result = true;
  else if (tokens.back() == "false") result = false;
  else throw std::invalid_argument("The last argument is not a boolean!");

  // Remove the last element
  tokens.pop_back();

  // Loop over the nots
  for (auto const& t : tokens)
  {
    if (t == "not") result = !result;
    else throw std::invalid_argument("Negation has to be indicated by 'not'!");
  }

  // Output the result
  std::cout << std::boolalpha << result << '\n';
}

Live example

Henri Menke
  • 10,705
  • 1
  • 24
  • 42