0

The language i'm trying to do this in is c++ let's say i have a number 123456 , i want to divide this up into partitions of two so i would have: 12,34,and 56. I want to add the partitions up and it would equal 102. How can this be done in c++? Thanks for the feed back :D:D

3
Contributors
3
Replies
4
Views
5 Years
Discussion Span
Last Post by raptr_dflo
1

You could convert the number into a string, then creating substrings of every two characters, and then parse them back to int and add them together?

Another approach would be using modulus, adding the rests of your value x mod 100, 10000 (then split by 100) and so on.

Good luck to you!
Emil Olofsson

0
#include<iostream>
#include<cstring>
#include<string>
#include<cstdlib>
using namespace std;
int main()
{

    char myArray10];
    string first;
    string second;
    string third;
    char alpha = 'a';

    itoa(1234,myArray,10);
    cout << myArray;

first.assign(myArray,2);
second.assign(myArray,2,2);

cout << first;
cout << second;





    return 0;




    }

    So how would i convert the strings back into an integer and add them up?
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.