Error

There is a error in this code whenever i enter a character in 'choice' which is a integer the loop goes infinitely.

int main()

{

int choice, value, ch;

struct node *start = NULL;

//struct stack *top = NULL;

void addnode(struct node **q, int num);

void display(struct node **q);

void rec_display(struct node *q);

while(1)

{

fflush(stdout);

fflush(stdin);

choice = 0;

getch();

printf("\nBinary Search Tree Demonstration");

printf("\n Main Menu");

printf("\n\n 1. Insert a new node");

printf("\n 2. Display the current tree using recursion");

printf("\n 3. Display the current tree using stack");

printf("\n 4. Exit");

scanf("%d", &choice);

printf("\nYou entered %d", choice);

ch = (int)choice;

switch(choice)

{

case 1: printf("You Choose to insert a new node");

printf("\n\n Enter New Value");

scanf("%d", &value);

addnode(&start, ((int)value));

break;



case 2: printf("You Choose to display the tree\n\n");

rec_display(start);

break;



case 3: printf("You Choose to display the tree with stack\n\n");

display(&start);

break;



case 4: printf("You Choose to exit\n\n");

exit(1);

break;



default: break;

}

}

return 0;

}
Please use [code][/code] tags. And I don't really understand your problem. Could you be more specific?
It's most likely because you're using C instead of C++
guys this a full working code given in here.

#include<iostream>
#include<stdio.h>
#include<stdlib.h>

using namespace std;

int main()

{

int choice;

while(1)

{

fflush(stdin);

choice = 0;

printf("\nError Demonstration");

printf("\n Main Menu");

printf("\n\n 1. Insert a new node");

printf("\n 2. Display the current tree using recursion");

printf("\n 3. Display the current tree using stack");

printf("\n 4. Exit");

printf("\n\n Kindly enter the number associated with the choice..");

cin>>choice;

printf("\nYou entered %d", choice);

switch(choice)

{

case 1: printf("You Choose to insert a new node");

break;

case 2: printf("You Choose to display the tree\n\n");

break;

case 3: printf("You Choose to display the tree with stack\n\n");

break;

case 4: printf("You Choose to exit\n\n");

exit(1);

default: break;

}

}

return 0;

}

note that I am working with this code in GNU compiler g++... and

when prompted

Kindly enter the number associated with the choice


if you enter a character for example 'a' .

the loop goes infinitely....

please tell the error in this as according to code this is not supposed to happen..
Last edited on
closed account (z05DSL3A)
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
#include<iostream>
#include<stdio.h>
#include<stdlib.h>

using namespace std;

int main()

{

    int choice;

    while(1)
    {

        fflush(stdin);

        choice = 0;

        printf("\nError Demonstration");

        printf("\n Main Menu");

        printf("\n\n 1. Insert a new node");

        printf("\n 2. Display the current tree using recursion");

        printf("\n 3. Display the current tree using stack");

        printf("\n 4. Exit");

        printf("\n\n Kindly enter the number associated with the choice.."); 

        cin>>choice;

        printf("\nYou entered %d", choice);

        switch(choice)

        {
        case 0: printf("\n\nI guess something went wrong with how I clear the input stream!");
            printf("\n\nPulling rip-cord and bailing out! ");
            exit(1);
            break;

        case 1: printf("You Choose to insert a new node");

            break;

        case 2: printf("You Choose to display the tree\n\n");

            break;

        case 3: printf("You Choose to display the tree with stack\n\n");

            break;

        case 4: printf("You Choose to exit\n\n");

            exit(1);

        default: break;

        }

    }

    return 0;

}
You loop is infinite: while (1) and there are no break statements associated with it. How do you expect execution to leave the loop?

You cannot break from the loop from within the switch statement, as you will only break out of the switch and not the loop.
Last edited on
closed account (z05DSL3A)
Xander314,
the OP wrote:
if you enter a character for example 'a' .

the loop goes infinitely....

This is just a case of not cleaning the input stream correctly.

OP, try something like:
1
2
3
4
5
6
7
8
9
10
11
12
#include<limits>
//...
void Clear_cin()
{
    std::cin.clear();
    std::cin.ignore(std::numeric_limits<streamsize>::max(),'\n');
}
//...
        cin>>choice;
        Clear_cin();
//...


Last edited on
Grey Wolf thanks very much...
this worked

ashish

Xander 314 you havn't tried to code i think sir, this actually (your solution) is what i thought first but it prints all those lines under main menu... back and this solution does not solve the problem... thanks anyway you all

ashish
Topic archived. No new replies allowed.