I need help with linked list

What I'm trying to do is make a calculator that takes two large numbers in a linked list then ask the user if they wish to add them or subtract them.
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include<iostream>
#include<string>
using namespace std;

class Node
{
public:
int value;
Node * next;
Node();
Node(int);
};
Node::Node():value(0),next(NULL{}
Node::Node(int v):value(v),next(NULL){}

void Print(Node* h)
{
while(h)
{
cout<<" -> "<<h -> value;
h = h -> next;
}
cout<<endl;
}

void Create(Node *& head, int n)
{
head = new Node(1);
Node * tmp = head;
string s; char c;
cin>>s
s=""
for(int i =0; i < s.length; i++)
c = s.charat(i)
v = c = '0';
}

void DeleteAll(Node*& head)
{
Node* tmp;
while(head)
{
tmp = head;
head = head->next;
delete tmp;
}
}

int main()
{
char operation;
int result;

Node * h1 = NULL;
Node * h2 = NULL;

cout<<"Enter first number:";
Create(h1);
cout<<endl;
cout<<"Enter second number";
Create(h2);
cout<<endl;

cout<<"What operation you want, + ot -";
cin>>operation;
if( operation == '+' )
{
h1 + h2 = result;
cout<<"Result="<<result<<endl;
}
else if( operation == '-' )
{
h1 - h2 = result;
cout<<"Result="<<result<<endl;
}
return 0;
}
Last edited on
If you tried to compile your code...

Missing closing parenthesis.
Node::Node():value(0),next(NULL{}

This isn't how you assign to a variable.
h1 + h2 = result;

What must this do?
1
2
3
4
s=""
for(int i =0; i < s.length; i++)
c = s.charat(i)
v = c = '0';


Also, please use code tags. Otherwise your code is harder to read.
closed account (18hRX9L8)
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include<iostream>
#include<string>
using namespace std;

class Node
{
      public:
            int value;
            Node * next;
            Node();
            Node(int);
};

Node::Node():value(0),next(NULL){}
Node::Node(int v):value(v),next(NULL){}

void Print(Node* h)
{
      while(h)
      {
            cout<<" -> "<<h -> value;
            h = h -> next;
      }
      cout<<endl;
}

void Create(Node *& head, int n)
{
      head = new Node(1);
      Node * tmp = head;
      string s; char c;
      cin>>s;
      s="";
      for(int i =0; i < s.length; i++)
            c = s.charat(i);
      char v = c = '0';
}

void DeleteAll(Node*& head)
{
      Node* tmp;
      while(head)
      {
             tmp = head;
             head = head->next;
             delete tmp;
      }
}

int main()
{
      char operation;
      int result;

      Node * h1 = NULL;
      Node * h2 = NULL;

      cout<<"Enter first number:";
      Create(h1);
      cout<<endl;
      cout<<"Enter second number";
      Create(h2);
      cout<<endl;

      cout<<"What operation you want, + ot -";
      cin>>operation;
      if( operation == '+' )
      {
            result = h1 + h2;
            cout<<"Result="<<result<<endl;
      }
      else if( operation == '-' )
      {
            result = h1 - h2;
            cout<<"Result="<<result<<endl;
      }
}


This is as far as I could get without completely altering your code. Here are the errors:


---- In function `void Create(Node*&, int)':
34 invalid use of member (did you forget the `&' ?)
35 'struct std::string' has no member named 'charat'
---- In function `int main()':
28 too few arguments to function `void Create(Node*&, int)'
59 at this point in file
28 too few arguments to function `void Create(Node*&, int)'
62 at this point in file
69 invalid operands of types `Node*' and `Node*' to binary `operator+'
Thanks but I tried your code and still it doesn't work and I made some changes.
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include<iostream>
#include<string>
using namespace std;

class Node
{
      public:
            int value;
            Node * next;
            Node();
            Node(int);
};

Node::Node():value(0),next(NULL){}
Node::Node(int v):value(v),next(NULL){}

void Print(Node* h)
{
      while(h)
      {
            cout<<" -> "<<h -> value;
            h = h -> next;
      }
      cout<<endl;
}

void Create(Node *& head, int n)
{
      head = new Node(1);
      Node * tmp = head;
      string s; char c;
      cin>>s;
      s="";
      for(int i =0; i < s.length(); i++)
            c = s.at(i);
      char v = c = '0';
}

void DeleteAll(Node*& head)
{
      Node* tmp;
      while(head)
      {
             tmp = head;
             head = head->next;
             delete tmp;
      }
}

int main()
{
      char operation;
      int result;

      Node * h1 = NULL;
      Node * h2 = NULL;

      cout<<"Enter first number:";
      Create(h1);
      cout<<endl;
      cout<<"Enter second number";
      Create(h2);
      cout<<endl;

      cout<<"What operation you want, + ot -";
      cin>>operation;
      if( operation == '+' )
      {
            result = h1 ->value + h2 ->value;
            cout<<"Result="<<result<<endl;
      }
      else if( operation == '-' )
      {
            result = h1 ->value - h2 ->value;
            cout<<"Result="<<result<<endl;
      }
}


the errors:
1>c:\users\documents\visual studio 2010\projects\project\project\1.cpp(34): warning C4018: '<' : signed/unsigned mismatch
1>c:\users\documents\visual studio 2010\projects\project\project\1.cpp(59): error C2660: 'Create' : function does not take 1 arguments
1>c:\users\documents\visual studio 2010\projects\project\project\1.cpp(62): error C2660: 'Create' : function does not take 1 arguments
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
closed account (18hRX9L8)
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include<iostream>
#include<string>
using namespace std;

class Node
{
      public:
            int value;
            Node * next;
            Node();
            Node(int);
};

Node::Node():value(0),next(NULL){}
Node::Node(int v):value(v),next(NULL){}

void Print(Node* h)
{
      while(h)
      {
            cout<<" -> "<<h -> value;
            h = h -> next;
      }
      cout<<endl;
}

void Create(Node *& head)
{
      head = new Node(1);
      Node * tmp = head;
      string s; char c;
      cin>>s;
      s="";
      for(int i =0; i < s.length(); i++)
            c = s.at(i);
      char v = c = '0';
}

void DeleteAll(Node*& head)
{
      Node* tmp;
      while(head)
      {
             tmp = head;
             head = head->next;
             delete tmp;
      }
}

int main()
{
      char operation;
      int result;

      Node * h1 = NULL;
      Node * h2 = NULL;

      cout<<"Enter first number:";
      Create(h1);
      cin.ignore();
      cout<<endl;
      cout<<"Enter second number";
      Create(h2);
      cin.ignore();
      cout<<endl;

      cout<<"What operation you want, + or -";
      cin>>operation;
      if( operation == '+' )
      {
            result = h1 ->value + h2 ->value;
            cout<<"Result="<<result<<endl;
      }
      else if( operation == '-' )
      {
            result = h1 ->value - h2 ->value;
            cout<<"Result="<<result<<endl;
      }
      cin.ignore();
      cin.ignore();
}


Okay, fixed up your program so it compiles. Then I ran it, it took in values but the result was either 2 or 0. So, first problems I spotted:

1. In function Create(Node &* head), line 31-32: You take in string s, but then you throw away its value by adding the s="";. What are you trying to do there?

2. In the program, all lines: You aren't using the function Print(node* h); anywhere in your program, so make use of it or throw it out. You also aren't using the function DeleteAll(Node*& head); so use it or delete it.

That's all I got for now, maybe someone with a bit more expertise can help you.
What I'm trying to do is to write a program that can add or subtract very large integers as much as the user wants to using linked list.
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
58
59
60
61
62
63
64
#include<iostream>
#include<string>
using namespace std;

class Node
{
      public:
            int value;
            Node * next;
            Node();
            Node(int);
};

Node::Node():value(0),next(NULL){}
Node::Node(int v):value(v),next(NULL){}

void Create(Node *& head)
{
      head = new Node(1);
      Node * tmp = head;
	  //what i'm trying to do here is to convert the string int inger
	  //and put it in a linked list so the user can add or subtract
	  //very large numbers.
      string s; char c;
      cin>>s;
      for(int i =0; i < s.length(); i++)
            c = s.at(i);
      char v = c = '0';
}

int main()
{
      char operation;
      int result;

      Node * h1 = NULL;
      Node * h2 = NULL;

      cout<<"Enter first number:";
      Create(h1);
      cin.ignore();
      cout<<endl;

      cout<<"Enter second number";
      Create(h2);
      cin.ignore();
      cout<<endl;

      cout<<"What operation you want, + or -";
      cin>>operation;
      if( operation == '+' )
      {
            result = h1 ->value + h2 ->value;
            cout<<"Result="<<result<<endl;
      }

      else if( operation == '-' )
      {
            result = h1 ->value - h2 ->value;
            cout<<"Result="<<result<<endl;
      }
      cin.ignore();
      cin.ignore();
}
Topic archived. No new replies allowed.