Error with cin in while

Hello, I'm kind of new in C++ programming and I'm starting with data structures (in this case binary trees). But when I read information from console I keep getting an error and I don't really know why. I'd apreciate your help, thank you very much.
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
#include <iostream>

using namespace std;

struct node
{
    int value;
    struct node* right;
    struct node* left;
};

void ins_left(node*, int);
void ins_right(node*, int);
void insert_node(node**, int);

int main()
{
    int p;
    struct node* root;
    int n;
    root = new node;
    cout<<"Write root value: "<<endl;
    cin>>n;
    insert_node(&root, n);
    return 0;
}

void insert_node(struct node **root, int n)
{
    int h;
    int i = 0;
    node *q;
    q = new node;
    (*root) -> value = n;
    (*root)->right = 0;
    (*root)->left = 0;
    q = *root;
    cout<<"Insert number of nodes: "<<endl;
    cin>>n;
    cout<<"Insert number: "<<endl;
    cin>>h;
    while(i < n){
       do{
            if(q->value>h)
               q = q->left;
            else if(q->value<h)
               q = q->right;
        }while(q!=0 && q->value!=h);

        if(q->value == h)
            cout<<"Repeated number"<<endl;
        else if(h < q->value)
            ins_left(q,h);
        else
            ins_right(q,h);
        cout<<"Write number: "<<endl;
        cin>>h;
    }
}

void ins_right(node* p, int n)
{
    node* new_n;
     new_n= new node;
    new_n->value = n;
    new_n->right = 0;
    new_n->left = 0;
    p->right = new_n;
}

void ins_left(node* p, int n)
{
    node* new_n;
    new_n = new node;
    new_n->value = n;
    new_n->right = 0;
    new_n->left = 0;
    p->left = new_n;
}
Last edited on
It's very hard for me to understand what variables do,, :'(
Is there translator -..-
Last edited on
Sorry, I forgot about that... ¿What about now?
Changed something, check it.
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
void insert_node(struct node **root, int n)
{
	int h;
	int i = 0;
	node *q;
	//q = new node; -> you don't need this
	(*root) -> value = n;
	(*root)->right = 0;
	(*root)->left = 0;
	//q = *root; -> q must start from root every insertion
	cout<<"Insert number of nodes: "<<endl;
	cin>>n;
	//cout<<"Insert number: "<<endl;
	//cin>>h;
	while(i < n){
		cout<<"Insert number: "<<endl;
		cin>>h;
		// here's q
		q = *root; 
		do{
		// beware the NULL pointer !
			if(q->value>h)
				if(q->left == NULL) break;
				else q = q->left;
			else if(q->value<h)
				if(q->right == NULL) break;
				else q = q->right;
		}while(q!=0 && q->value!=h);

		if(q->value == h)
			cout<<"Repeated number"<<endl;
		else if(h < q->value)
			ins_left(q,h);
		else
			ins_right(q,h);
		i++;	//How you forget this!
	}
}


In fact, you don't have to use void insert_node(struct node **root, int n).
Just make it more complicated. You can use just void insert_node(struct node *root, int n), and in main just insert_node(root, n);

Btw, nothing wrong with cin. It's all because if your null pointer is processed.
Last edited on
Wow! Thank you so much... I feel stupid now.. ;) Jaja Thanks.
Topic archived. No new replies allowed.