Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
356 views
in Technique[技术] by (71.8m points)

c++ - storing integer values in a bool variable

I'm trying to store integer values in a boolean array, however when I print the values out, they only come out as 1's and 0's which I assume represent true and false. Is there any way to store values greater than 1 or less than 0 in a bool? Thanks in advance for the help! :D

Here's a sample of what I'm trying to do:

#include <iostream>
#include <string>

using namespace std;

int main(){

    bool set[2];

    set[0] = 7;
    set[1] = 13;

    string setNotation = "{"+to_string(set[0])+", "+to_string(set[1])+"}";

    cout << setNotation << endl; //prints out as {1, 1} instead of {7, 13}

    //note: I have to use to_string to append and not cout << "{" << set[0] << ...
    //because I'm using this in a method that returns a string

    return 0;
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Nope, bools are designed to only store a 1 or a 0. Sorry about that. You might want to try an int.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...