I am new to C++ plz help
The below code does not work.. (printing garbage value )can any 1 explain y....
Code:
#include <iostream>
#include <string.h>
using namespace std;
struct bignum
{
char digits[1000];
int lastdigit;
bignum operator = (char *str)
{
int j=0;
bignum temp;
temp.lastdigit=strlen(str)-1;
// printf("len=%d\n",temp.lastdigit); /* working fine over here */
for(int i=temp.lastdigit;i>=0;i--)
temp.digits[i]=str[j++];
return temp;
}
};
int main()
{
bignum num;
char s[10]="12345";
num=s;
printf("%d\n",num.lastdigit); /* why printing garbage value ?? */
return 0;
}
And also when operator overload is declared outside the struct i get a compile error saying
Code:
addition.cpp:12: error: ‘bignum operator=(char*)’ must be a nonstatic member function
addition.cpp: In function ‘int main()’:
addition.cpp:32: error: no match for ‘operator=’ in ‘num = s’
addition.cpp:7: note: candidates are: bignum& bignum::operator=(const bignum&)
Following is the code that gives compile error
Code:
#include <iostream>
#include <algorithm>
#include <string.h>
using namespace std;
struct bignum
{
char digits[1000];
int lastdigit;
};
bignum operator = (char *str)
{
int j=0;
bignum temp;
temp.lastdigit=strlen(str)-1;
// printf("len=%d\n",temp.lastdigit); /* working fine over here */
for(int i=temp.lastdigit;i>=0;i--)
temp.digits[i]=str[j++];
return temp;
}
int main()
{
bignum num;
char s[10]="12345";
num=s;
printf("%d\n",num.lastdigit); /* why printing garbage value ?? */
return 0;
}
Thank u