extract every digit for arithmetic calculation


I'm trying to display a c++ output with the image link. this was a sample question given before anything was taught. so I was hoping for someone to give me a sample of how to get this using the for loop and without the for loop in order to have a head start. accordingly to the lecturer they said this is the most basic of all.
http://imgur.com/54HeYtn
Hi,
Can you give us more details about your assignment?

Is this image your program output? Or a sample output? Do you bring your code with you?
Last edited on
I'll go pretty much from scratch here.

Everything you need for now is a main() function. Main functions in C++ always return an int (usually 0, if everything went well).

First you need to declare two integer variables, simply by int n;.

To write on your console, use std::cout << "n is " << n;. std is the standard library, cout is the standard output stream and << is the operator to write on it. For a new line, use "\n" or std::endl.

To read from the command line, use std::cin >> n;. This will save the input on the variable n.

To make sure your input has 4 digits, you can just repeadetly check whether your variable is <1000 or >9999. Do this with a while loop. Repeat for second value.

To get the single digits, there's probably a fancy way by converting the number to a string and then splitting it up, but I prefer the mathematical way.

int n1 = n/1000 will give you the first digit, as n and 1000 are integers and / only does a whole number division. (Keep this in mind, when you say double x = 1/2;, x will be zero, not one half!)
Then you can continue by making your 4 digit number a 3 digit number, by subtracting the 1000s like this:
n = n - n1 * 1000
You can keep on going like this for n2, n3 and n4.

The other variables are pretty straight forward, just int sum1 = n1 + m1; etc.
For division you need to cast a variable on double: double div1 = (double) n1 / m1;

The output is pretty straight forward as well. You just need to adjust the number of spaces to (8 - number of digits).
From http://stackoverflow.com/questions/22648978/c-how-to-find-the-length-of-an-integer
1
2
3
4
5
6
7
8
9
10
int len = 1;

// and for numbers greater than 0:
if (i > 0) {
    // we count how many times it can be divided by 10:
    // (how many times we can cut off the last digit until we end up with 0)
    for (len = 0; i > 0; len++) {
        i = i / 10;
    }
}
gives you the length of an integer. You might want this in a seperate function. Don't forget to add 1, if your number is negative.

To output x spaces, do a for-loop that starts with i=0, runs while i<x and where you increase i by 1 in every iteration. In every iteration you output a " ".

For the second method with the for loop you need vectors. Those are just like lists or arrays mostly.

First you need to #include <vector> .
Declare a vector of integers: vector<int> digits_of_n;
Then, instead of saving n1, n2, n3, n4, you use a for loop that runs 4 times and does the same thing, but uses digits_of_n.push_back(digit);
You'll end up with a vector that contains 4 ints, which are the 4 digits of n. To call a digit use digits_of_n.at(position);. Note that vectors start counting at 0, so .at(0) gives the first digit, .at(1) gives the second etc.
Output is the same.
Last edited on
Topic archived. No new replies allowed.