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
을 인쇄하기 위해 대신
이미 부울을 반환합니다. 질문은 무엇입니까? – IdeaHat
멤버 변수 이름과 동일한 매개 변수 이름은 사용하지 마십시오. 합법적이지만 혼란 스럽습니다. – Logicrat
그것은 부울을 반환해야하지만, 아무것도하지 내 질문은 : 내 코드에 대답을 반환하지 않는 문제가 있습니까? –