Program having unexpected exit

0

I'm having an unexpected exit problem and can not seem to find the error in my code, the output in question is the errors of all the sentences which are not "Dog walks.", a notorious observation is the fact that the user enter with "Dog" enter "walks" enter "." enter, the program will only acknowledge NOT OK at the end of the ".", but when we put any other phrases that are not the same ones mentioned, at the first enter I already get the result "Not ok". Example: "Fish" enter, (Not okay print), "Nothing", enter, (not ok print), ".", Enter, (not ok again).

#include <iostream>
#include <stdlib.h>
#include <vector>
using namespace std;

vector<string> nome;
vector<string> verbo;     
vector<string> conjuncao;

void inicializar(){     // Função pra definir valores dos vetores  
nome.push_back("cachorro");
nome.push_back("passaro");
nome.push_back("peixe");

verbo.push_back("anda");
verbo.push_back("nada");
verbo.push_back("voa");

conjuncao.push_back("e");
conjuncao.push_back("ou");
conjuncao.push_back("mas");

}

bool isName(string v){
    for(int x = 0; x<nome.size(); x++){   //Verifica se a primeira palavra é um nome
       if(nome[x] == v) return true; 
       return false;
    }
}

bool isVerb(string v){
    for(int x = 0; x<verbo.size(); x++){
        if(verbo[x] == v) return true;  //Verifica se a segunda palavra é um verbo
         return false;
    }
}
bool isConjuncao(string v){
    for(int x = 0; x<conjuncao.size(); x++){
      if(conjuncao[x]==v) return true;   //Verifica se a segunda palavra é uma conj
      return false;
    }
}

bool sentenca(){  //Insere e faz todas as verificações

 string v;
 cin >> v;
 if(!isName(v)) return false;

 string v2;
 cin >> v2;
 if(!isVerb(v2)) return false;

 string v3;
 cin >> v3;
 if(v3 == ".") return true;  //Se for ".", termina, senão, continua.
 if(!isConjuncao(v3)) return false;

 return sentenca();

}



int main()
{
inicializar();

while(cin){  //Cria um loop para repetir o processo.
  bool b = sentenca();
    if(b) cout << "OK" << endl;  //Caso true, "ok"
    else cout << "Not ok" << endl;
}
    return 0;
}
    
asked by anonymous 29.03.2016 / 19:00

1 answer

2

1) All three verification methods are wrong.

You should put the "return false" outside the for loop and not inside. Because only if it does not find it in there it will return false.

2) Judgment should not be recursive ...

For what reason is judgment a method that calls itself? You are looping with the while already in the main function. It is best to return false if you do not find a true return.

    
29.03.2016 / 19:16