Jump to content

Interesting Addition Problem -NEWB-

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
5 replies to this topic

#1
RavenE

RavenE

    Newbie

  • Members
  • Pip
  • 8 posts
I have an interesting addition problem, it probably sounds simple but i was wondering how i could actually add eg. 1 + 1 and not get 11

as in:
a = 1
b = 1

c = a + b;
c= 11

I don't understand, sry if this is basic, i just started c++
Hope you can help me
Thanks :)
----Raven Studios----
-Programming Central-

#2
v0id

v0id

    Retired

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,936 posts
You can not do that using normal integers, but you can solve it using C++ strings.

#include <string>
// ...
std::string a = "1";
std::string b = "1";
std::string c = a + b;


#3
MeTh0Dz

MeTh0Dz

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,119 posts
Ewww... Strings...

#4
v0id

v0id

    Retired

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,936 posts

"MeTh0Dz" said:

Ewww... Strings...
At least you could come with arguments against the usage of C++ strings instead of just Ewww'ing at them. Personally, I think they're the best to use in this situation. If you really would like a solution with integers, here you are:
int append(int first, int second)
{
    first *= 10;
    first += second;
    return first;
}
// ...
int a = 1;
int b = 1;
int c = append(a, b);


#5
MeTh0Dz

MeTh0Dz

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,119 posts
It was somewhat of a joke, lol I did say ewww. But on the other hand C++ strings do suck.

The best use? LOL C++ string are ****, personally I'll use arrays and integers and just write a function that will do what I need to do as opposed to using strings which can't be used for anything. Basically you use the string class when you can't figure out how to do it with arrays.

At least that's how I view it.

#6
MeTh0Dz

MeTh0Dz

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,119 posts
This is how you can do it using integers.

It may or may not help you out.......

/
* This will do the adding thing - MeTh0Dz */


#include <windows.h>

#include <conio.h>

#include <iostream>

#include <cmath>


using namespace std;


void Math_It();


int main() {

    cout << "This is how MeTh0Dz would do it.\n\n";

    Sleep(200);

    cout << "First number will be in the ones place, second\n"

            "in the tens place, etc.\n\n";

    Math_It();

    getch();

    return 0;

}


void Math_It() {

     int amount;

     int sum = 0;

     int i;

     int the_ten = 10;

     cout << "How many numbers do you want to enter: ";

     cin >> amount;

     for (i = 0; i < amount; i++) {

         int temp;

         cout << "Enter " << i + 1 << "st Digit: ";

         cin >> temp;

         sum = (temp * (pow((double)the_ten, i))) + sum;

     }

     cout << sum;

}