why this code not respond

im wondering why this code does not work.

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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <cstdlib>
#include <iostream>
#include <vector>

using namespace std;

class bigNum
{
public:
       void setSum(string a)
       {     
            sum=a;
       }
       void setNum(string n)     
       {
            for (int i=n.size();i>=0;i--)            
                num.push_back (n[i]-'0');                  
       }
       bigNum operator+ (bigNum a)
       {       
               for (int i=0;i<num.size()+1;i++)
                   {
                   sum[i]+=(getNum(i)+a.getNum(i))%10;
                   sum[i+1]+=(((getNum(i)+a.getNum(i))%100)-((getNum(i)+a.getNum(i))%10))/10;
                   }
                   bigNum ret;
                   ret.setSum(sum);
                   return ret;
       }
       string getNum ()
       {
              return sum;       
       }
private:
        int getNum(int a)
        {
            return num[a];    
        }
        vector <int> num;        
        string static sum;
              
};
string bigNum::sum=0;

int main(int argc, char *argv[])
{   
    bigNum b1,b2,b3;
    string a,b;
    cin >> a >> b;
    b1.setNum(a);
    b2.setNum(b);
    b3=b2+b1;
    cout << b3.getNum() << endl;
    
    system("PAUSE");
    return EXIT_SUCCESS;
}
Difficult to say without you giving us the errors. I think one prob is that you are assigning ints directly to chars in operator+, which should lead to some odd and undesired results - something you seem to be aware of since you include the "- '0'" in setNum.
there isn't any errors .
when i run it suddenly it stops working.
closed account (zb0S216C)
What compiler are you using (not the IDE. Some users tend to specify the IDE instead of the compiler)?

A few things:
1)
danialgood wrote:
sum[i+1]+=(((getNum(i)+a.getNum(i))%100)-((getNum(i)+a.getNum(i))%10))/10;
Who're you trying to impress? These sort of compound expressions can confuse anybody that reads it. Instead, break it down to simpler and smaller compound expression statements. Also, add comments to indicate what the expression is for and what it's intended to do.

2) std::cin doesn't accept string objects (not in VC++E '10, at least).

Wazzak
Last edited on
@Framework

What compiler are you using

Do the various degrees functionality of your program call for any particular kind of compiler? Or are they used based on a level of preference. Im using the GCC Compiler, just wondering compilers are something that is chosen like choosing a favorite IDE, or certain compilers compliment certain abilities...
closed account (zb0S216C)
AppDevTrainee wrote:
Do the various degrees functionality of your program call for any particular kind of compiler?

Generally, it's common to choose a compiler that best suits your project. Some compilers perform better optimizations than others, for example. So yes, I guess it's a personal preference.

AppDevTrainee wrote:
just wondering compilers are something that is chosen like choosing a favorite IDE

It's common to find IDEs such as Code::Blocks, Dev-C++ (apologies for speaking the forsaken IDE's name), and VC++ with a default compiler. Some IDEs, however, can support multiple compilers, such as Code::Blocks.

Wazzak
lol its all good. And thanks i really didnt know that. For now im not developing anything that beckons the use of a different compiler other than GCC. But later in the future when i start developing strong and more specified compilers i will definitely start considering different compilers.

However, on the topic of personal preference, whats the best IDE??
closed account (zb0S216C)
The best IDE for GCC, in my opinion, is Code::Blocks. The best IDE for Microsoft's compiler is Visual C++ Express 2010 (free). I have them both installed; that way, I can test for code compatibility with different compilers.

Wazzak
Framework wrote:
std::cin doesn't accept string objects (not in VC++E '10, at least).
¿eh? http://www.cplusplus.com/reference/string/operator%3E%3E/

@danialgood: Integer division returns an integer.
Also string static sum;. ¿do you realise what that means? That string is shared for all the objects.
IIRC string bigNum::sum=0; you are trying to dereference a NULL pointer there.
Later you try to access the string. But you never set it to anything (so, out of bounds)
thank you Framework, ive been using Code::Blocks right now. I think im gonna give Visual C++ Express a whirl also
closed account (zb0S216C)
Ne555, I forgot to include <string> :)

Wazzak
Topic archived. No new replies allowed.