Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to slice (c++)

I am learning basic c++, and I am confused on how to slice. For example, if a = 2.43231, how do I make it output only 2.43?

#include <iostream>
using namespace std;

int main(){
float a = 2.4323 ;

cout << a;

};

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

That’s not slicing. It’s just controlling the precision when you print out the number.

    #include <iomanip>

    // ...
    std::cout << std::setprecision(2) << a;

Slicing is something completely different, involving inheritance and assigning a value of a derived class to an instance of the base class.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading