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 :)
Interesting Addition Problem -NEWB-
Started by RavenE, May 30 2008 02:13 AM
5 replies to this topic
#1
Posted 30 May 2008 - 02:13 AM
----Raven Studios----
-Programming Central-
-Programming Central-
|
|
|
#2
Posted 30 May 2008 - 02:47 AM
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
Posted 30 May 2008 - 03:05 AM
Ewww... Strings...
#4
Posted 30 May 2008 - 03:20 AM
"MeTh0Dz" said:
Ewww... Strings...
int append(int first, int second)
{
first *= 10;
first += second;
return first;
}
// ...
int a = 1;
int b = 1;
int c = append(a, b);
#5
Posted 30 May 2008 - 03:52 AM
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.
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
Posted 30 May 2008 - 01:43 PM
This is how you can do it using integers.
It may or may not help you out.......
/
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;
}


Sign In
Create Account

Back to top









