5

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.

ThomasMcLeod
  • 7,603
  • 4
  • 42
  • 80
Joe Nickerson
  • 73
  • 2
  • 2
  • 4
  • Do those carry through into an int? – Joe Nickerson Oct 17 '12 at 20:06
  • I would assume that there is a possibility of having an encryption that would result in more than one leading zero something like 0012 or 0009 or maybe even 0000. You are only handling the case of if the first digit is zero. Take a look at my suggestion below for a function that will handle these cases. – Richard Chambers Oct 17 '12 at 20:43
  • An int in its natural state does not carry leading zeroes (technically not true, but for the sake of argument let's say it doesn't). `1` is an integer. `10` is an integer. `0010` is a *representation* of an integer. It's not the int you need to change, but the way you print it. – MPelletier Oct 17 '12 at 20:53
  • @RichardChambers I completely missed that possibility. The example you gave looks perfect, its a little bit out of my range but i'm going to study what you did. Thanks! – Joe Nickerson Oct 17 '12 at 21:19
  • @ThomasMcLeod Please note that [the homework tag is now being phased out and must no longer be used](http://meta.stackexchange.com/questions/147100/the-homework-tag-is-now-officially-deprecated). – Gilles 'SO- stop being evil' Oct 17 '12 at 22:04

5 Answers5

5

Do this:

#include <iomanip>
#include <iostream>

std::cout << std::setfill('0') << std::setw(4) << joined << std::endl;
Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
4

An int contains a number. It does not contain any particular representation information, like whether it was input from text containing one leading zero or two, or whether it was written in hexadecimal, octal, or chicken scratches, or even if it was computed from adding a bunch of numbers. It is just a value.

If you want to display an int with leading zeros, then you have to explicitly convert it that way:

char buf [20];
snprintf (buf, sizeof buf, "%04d", myint);  // output base 10, + leading zeros
                                           // for a field width of 4
wallyk
  • 56,922
  • 16
  • 83
  • 148
  • +1 for explaining about leading zeros not being relevant to how an int is represented. – bames53 Oct 17 '12 at 20:41
  • That depends. If there was an application that relied heavily on displaying ints and always needed them to be a fixed width, and in fact the display actions were far more important than actual int arithmetic (though this also needed support, so they can't just be strings), it could be important to actually make the data type have fixed width. I could see this mattering for some primitive, small electronics, for example. We're all just so used to the common use cases of the int type that its displayed is (rightfully) highly decoupled from the type's specs. But that's not a universal truth... – ely Oct 17 '12 at 21:17
  • I guess I'm saying it's reasonable sometimes for someone to demand the data type `int` have extra, application-specific properties that a mathematical integer need not have. For general computing this is highly undesirable, but there could exists domain-specifics where it matters. So I am hesitant to endorse the idea that "an int contains a number." Yes, a *math* integer contains a representation of a number. But an `int` instance ought to contain whatever you need in your system. Usually this is a decoupled object as faithful to a math-int as possible. But not always. – ely Oct 17 '12 at 21:20
  • @EMS: That is a unusual point of view for a question tagged `c++`. Your perspective is more appropriate for data processing systems engineering or some branches of mathematics. – wallyk Oct 18 '12 at 05:44
  • I worked on implementing a real-time embedded system (in Python, actually, but a lot of the work was done with Cython) where we needed exactly that. It was more efficient to store ints differently because they were just needed for display purposed and some very minor int arithmetic. I don't think it's unusual at all for C++, nor any language. My point of view is mean to be language agnostic. The `int` data type doesn't have to be the same thing as the standard `int` data type. – ely Oct 18 '12 at 13:07
3

This should do the trick.

cout << setw(4) << setfill('0') << joined << endl;

In order to use these manipulators, you'll need to:

#include <iomanip>
John Dibling
  • 99,718
  • 31
  • 186
  • 324
3

An int basically stores leading zeros. The problem that you are running into is that you are not printing the leading zeros that are there.

Another, different approach is to create a function that will accept the four int values along with a string and to then return a string with the numbers.

With this approach you have a helper function with very good cohesion, no side effects, reusable where you need something similar to be done.

For instance:

char *joinedIntString (char *pBuff, int int1, int int2, int int3, int int4)
{
    pBuff[0] = (int1 % 10) + '0';
    pBuff[1] = (int2 % 10) + '0';
    pBuff[2] = (int3 % 10) + '0';
    pBuff[3] = (int4 % 10) + '0';
    pBuff[4] = 0;                    // end of string needed.
    return pBuff;
}

Then in the place where you need to print the value you can just call the function with the arguments and the provided character buffer and then just print the character buffer.

With this approach if you have some unreasonable numbers that end up have more than one leading zero, you will get all of the zeros.

Or you may want to have a function that combines the four ints into a single int and then another function that will print the combined int with leading zeros.

int createJoinedInt (int int1, int int2, int int3, int int4)
{
    return (int1 % 10) * 1000 + (int2 % 10) * 100 + (int 3 % 10) * 10 + (int4 % 10);
}

char *joinedIntString (char *pBuff, int joinedInt)
{
    pBuff[0] = ((joinedInt / 1000) % 10) + '0';
    pBuff[1] = ((joinedInt / 100) % 10) + '0';
    pBuff[2] = ((joinedInt / 10) % 10) + '0';
    pBuff[3] = (joinedInt % 10) + '0';
    pBuff[4] = 0;                    // end of string needed.
    return pBuff;
}
Richard Chambers
  • 16,643
  • 4
  • 81
  • 106
1

C++ stores int as a binary number. However all IO is as string. So, to display an int there must be a conversion from an int to a string. It's in the conversion process that you can set the with from the displayed number. Use the streams manipulators setw and setfill for this purpose.

ThomasMcLeod
  • 7,603
  • 4
  • 42
  • 80