sum
Hey guys.Just did a program using pointers that adds two numbers but it doesnt work for negative numbers.Whats missing?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
|
#include<iostream>
using namespace std;
void sum(char *a,char *b)
{int i,j,sum=0;
int sum1=0;
int len = strlen(a);
int len1=strlen(b);
for(i=0;i<len;i++)
sum = 10 * sum + (a[i] -'0');
for(j=0;j<len1;j++)
sum1= 10 * sum1 + (b[j]-'0' );
printf("\nSum = %d \n",sum+sum1);
}
main()
{
char *a=new char[19];
char *b=new char[19];
cout<<"a=";cin.getline(a,19);
cout<<"b=";cin.getline(b,19);
sum(a,b);
system("pause");
}
|
You didn't check for '-' before each number.
I dont understand what you mean
"-100" + "1000"
What are you doing with '-' symbol?
Well i read a string of numbers and without that -0 i cant display correctly the result.
Topic archived. No new replies allowed.