C ++打印出bool类型变量

时间:2014-09-12 19:48:34

标签: c++

在那里,我是C ++的新手,我的问题很小。有一个名为" bool checkIfPersonAlreadyExists(){...}"我希望这个函数返回一个bool类型的变量,但它没有。谁能帮我这个? (答案应该是" true"或" false"但是以这种形式:"返回IfPersonAlreadyExists")。

#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;
}

3 个答案:

答案 0 :(得分:2)

为了打印truefalse而不是10分别使用std::boolalpha,如下例所示:

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

成员函数Library::checkIfPersonAlreadyExists已经返回bool

编辑:

还包括string标题(即#include <string>)。

答案 1 :(得分:0)

cout&lt;&lt; library.getIfPersonAlreadyExists()&lt;&lt; ENDL;

我认为应该返回0或1。

只做cout&lt;&lt; (library.getIfPersonAlreadyExists()== true?&#34; true&#34;:&#34; false&#34;)&lt;&lt; ENDL;

答案 2 :(得分:0)

如果对本地化,全局变量和参数使用不同的大小写,您将会变得更加困惑。

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

总是如此!