2014-09-12 2 views
-2

Hy, 저는 C++을 처음 접했고, 작은 문제가 있습니다. "bool checkIfPersonAlreadyExists() {...}"라는 함수가 있는데이 함수가 bool 유형의 변수를 반환하지만 필요하지 않습니다. 아무도 이것으로 나를 도울 수 있습니까? 답은 "true"또는 "false"여야하지만, "return IfPersonAlreadyExists"와 같은 형식이어야합니다.C++에서 bool 타입 변수를 출력합니다.

#include <iostream> 
#include <cctype> 
#include <cstdlib> 

using namespace std; 

class Library { 
    private: 
     string name, surname; 
     int LSP; 
     bool IfPersonAlreadyExists; 

     //toString metodas ir kaip ji pasiekti is private????? 
     string toString (string name) { 
      cout << name << " " << surname << " " << LSP << endl; 
      return name; 
     } 
    public: 
     //setters 
     int setName (string name) { 
      if (!name.empty() && isalpha(name[0]) && isupper(name[0])) 
       this -> name = name; 
      else { 
       cout << "Wrong entry!" << endl; 
       return 0; 
      } 
     } 
     int setSurname (string surname) { 
      if (!surname.empty() && isalpha(surname[0]) && isupper(surname[0])) 
       this -> surname = surname; 
      else { 
       cout << "wrong entry!" << endl; 
       return 0; 
      } 
     } 
     int setLSP (int LSP) { 
      if (LSP > 0) 
       this -> LSP = LSP; 
      else { 
       cout << "Wrong entry!" << endl; 
       return 0; 
      } 
     } 
     void setIfPersonAlreadyExists (bool IfPersonAlreadyExists) { 
      this -> IfPersonAlreadyExists = IfPersonAlreadyExists; 
      IfPersonAlreadyExists = true; 
     } 
     //getters 
     string getName() { 
      return name; 
     } 
     string getSurname() { 
      return surname; 
     } 
     int getLSP() { 
      return LSP; 
     } 
     bool getIfPersonAlreadyExists() { 
      return IfPersonAlreadyExists; 
     } 

     //Kaip padaryti kad return true or false?????/? 
     bool checkIfPersonAlreadyExists (bool IfPersonAlreadyExists, int LSP) { 
      int LSPCheck; 
      cout << "Enter LSP number to check" << endl; 
      cin >> LSPCheck; 
      if (LSPCheck != LSP) { 
       IfPersonAlreadyExists = false; 
       //cout << boolalpha << IfPersonAlreadyExists << endl; 
       return IfPersonAlreadyExists; 
      } 
      else { 
       IfPersonAlreadyExists = true; 
       cout << boolalpha << IfPersonAlreadyExists << endl; 
       return IfPersonAlreadyExists; 
      } 
     } 
}; 


int main() { 
    Library library; 
    string name, surname; 
    int LSP; 
    bool IfPersonAlreadyExists = true; 
    cout << "Enter your name: " << endl; 
    cin >> name; 
    library.setName(name); 
    //cout << library.getName() << endl; 

    cout << "Enter your surname: " << endl; 
    cin >> surname; 
    library.setSurname(surname); 
    //cout << library.getSurname() << endl; 

    cout << "Enter your LSP number: " << endl; 
    cin >> LSP; 
    library.setLSP(LSP); 
    //cout << library.getLSP() << endl; 
    //library.toString(name); 

    library.setIfPersonAlreadyExists(IfPersonAlreadyExists); 
    // cout << library.getIfPersonAlreadyExists() << endl; 

    library.checkIfPersonAlreadyExists(IfPersonAlreadyExists, LSP); 

    return 0; 
} 
true을 인쇄하기 위해 대신
+1

이미 부울을 반환합니다. 질문은 무엇입니까? – IdeaHat

+0

멤버 변수 이름과 동일한 매개 변수 이름은 사용하지 마십시오. 합법적이지만 혼란 스럽습니다. – Logicrat

+0

그것은 부울을 반환해야하지만, 아무것도하지 내 질문은 : 내 코드에 대답을 반환하지 않는 문제가 있습니까? –

답변

1

1false0 각각 아래 예와 같이 std::boolalpha을 사용

std::cout << std::boolalpha << library.checkIfPersonAlreadyExists(IfPersonAlreadyExists, LSP) << std::endl;

구성원 함수 Library::checkIfPersonAlreadyExists은 이미 bool을 반환합니다.

편집 :

는 또한 string 헤더를 포함한다 (즉, #include <string>).

+0

나는 boolalpha를 사용했다. 내 함수가 bool 유형 변수를 반환하지 않습니다. 그리고 나는 무엇이 문제인지 모르겠다. –

+0

나는 그것을 시험해 보았고,'string' 헤더를 포함 시켰을 때 "잘"실행되었다. – 101010

0

cout < < library.getIfPersonAlreadyExists() < < endl;

0 또는 1을 반환해야한다고 생각합니다.

그냥 (== library.getIfPersonAlreadyExists()가 true "true"를 "false"로?) < < cout을 할 < < ENDL을;

+0

'std :: boolalpha'가 존재하므로 그렇게 할 필요가 없습니다. –

0

지역, 전역 및 매개 변수에 다른 대문자 사용을 사용하면 혼동을 덜 받게됩니다.

void setIfPersonAlreadyExists (bool IfPersonAlreadyExists) { 
     this -> IfPersonAlreadyExists = IfPersonAlreadyExists; 
     IfPersonAlreadyExists = true; 
    } 

항상 사실입니다!

관련 문제