Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to store a leading zero in an int?

Tags:

c++

I have a programming assignment where I need to encrypt a 4 digit int, input by user. I have split the int into four separate values and the encrypt and decrypt functions work. My problem is when I put the four separate ints back together, some numbers encrypt to zero (eg. in:1234 out:0189) and I want to store the output into an int for use with other functions.

Right now I have a half-baked solution that prints 0 first if the first int is 0.

void joinInt(){
    if(int1 == 0) {cout << 0;}
    joined = int1 * 1000;
    joined += int2 * 100;
    joined += int3 * 10;
    joined += int4;
    cout << joined << endl;
    }

My goal is to return joined (with the leading zero) rather than just print it within the function.

like image 891
Joe Nickerson Avatar asked Oct 17 '12 19:10

Joe Nickerson


1 Answers

Do this:

#include <iomanip>
#include <iostream>

std::cout << std::setfill('0') << std::setw(4) << joined << std::endl;
like image 73
Kerrek SB Avatar answered Sep 27 '22 20:09

Kerrek SB