cin is not taking value

Hi everybody,
I have just started learning c++ and I am trying to write a singly linked list program. The problem I am facing is when user chooses to insert it will go to the insert function where after adding one node , it will ask if you want to add another node, if the user says no it goes back to the main function and asks "Do u wanna make another choice?", after that it's not taking the choice neither it's throwing any error, simply doing nothing. and it prints the next line "Thanks!". I tried to print choice , the screen is blank. Kindly tell me where is my mistake?

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
  int main()
{ 
    link_list ll;
   // ll.print_list();
   
    //ll.display_options();
    char choice;
    do{
    cout << "select one"<< endl;
    cout <<  "press 1 for insert ." << endl;
    cout << "press 2 for insert at beginning ." << endl;
    cout << "press 3 for insert at the middle ." << endl;
    cout << "press 4 delete ." << endl;
    cout << "print 5 the linked list :" << endl;
    cout << "print 6 the linked list :" << endl;
   
         int no;
         cin >> no;
         switch (no)
         {
                case 1:
                     ll.insert();
                     break;
                case 4:
                     ll.print_list();
                case 6:
                     return 0;
                    
                default:
                        cout <<"oops wrong choice"<< endl;
                    
         }
     cout << "Do u wanna make another choice?" << endl;
     cin >> choice;
     cout<< choice << endl;
     }while(choice=='Y'||choice=='y');
    cout<<"Thanks!"<< endl;
    system("pause");
    return 0;
} 
closed account (z1CpDjzh)
Oh my, oh my... Have you heard of STD. here is the proper code to make it actually compile
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
//if your not going to put 'std::' everywhere why did you not put:
//using namespace std
//but that is recommended

#include <iostream>// no iostream... seriously

class link_list
{
public:
 void insert(); // i am assuming void
 void print_list();
};

void link_list::insert()
{
	return;
}
void link_list::print_list()
{

 return;
}//the functions.... which you did not give us!

int main()
{ 
    link_list ll;
   // ll.print_list();
   
    //ll.display_options();
    char choice;
    do{
    std::cout << "select one"<< std::endl;
    std::cout <<  "press 1 for insert ." << std::endl;
    std::cout << "press 2 for insert at beginning ." << std::endl;
    std::cout << "press 3 for insert at the middle ." << std::endl;
    std::cout << "press 4 delete ." << std::endl;
    std::cout << "print 5 the linked list :" << std::endl;
    std::cout << "print 6 the linked list :" << std::endl;
     //You forgot 'std::' everywhere

         int no;
         std::cin >> no;
         switch (no)
         {
              case 1:
                     ll.insert();	
		std::cout << "Insert";
		     std::cin.get();

                     break;
                case 4:
                     ll.print_list(); 
		std::cout << "print_list";
		 std::cin.get();
                case 6:
                    return 0;
                    
                default:
                        std::cout <<"oops wrong choice"<< std::endl;
                    
         } std::cin.get();
     std::cout << "Do u wanna make another choice?" << std::endl;
     std::cin >> choice;
     std::cout<< choice << std::endl;
     }while(choice=='Y'||choice=='y');
    std::cout<<"Thanks!"<< std::endl;
    system("pause");
    return 0;
} 


-------------------------
so the new code is :
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>

class link_list
{
public:
 void insert();
 void print_list();
};

void link_list::insert()
{
	return;
}
void link_list::print_list()
{

 return;
}

int main()
{ 
    link_list ll;
   // ll.print_list();
   
    //ll.display_options();
    char choice;
    do{
    std::cout << "select one"<< std::endl;
    std::cout <<  "press 1 for insert ." << std::endl;
    std::cout << "press 2 for insert at beginning ." << std::endl;
    std::cout << "press 3 for insert at the middle ." << std::endl;
    std::cout << "press 4 delete ." << std::endl;
    std::cout << "print 5 the linked list :" << std::endl;
    std::cout << "print 6 the linked list :" << std::endl;
   
         int no;
         std::cin >> no;
         switch (no)
         {
                case 1:
                     ll.insert();	
		std::cout << "Insert";
		     std::cin.get();

                     break;
                case 4:
                     ll.print_list(); 
		std::cout << "print_list";
		 std::cin.get();
                case 6:
                    return 0;
                    
                default:
                        std::cout <<"oops wrong choice"<< std::endl;
                    
         } std::cin.get();
     std::cout << "Do u wanna make another choice?" << std::endl;
     std::cin >> choice;
     std::cout<< choice << std::endl;
     }while(choice=='Y'||choice=='y');
    std::cout<<"Thanks!"<< std::endl;
    system("pause");
    return 0;
} 

Lets look at the switch:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
         switch (no)
         {
                case 1:
                     ll.insert();	
					 std::cout << "Insert";
					 std::cin.get();

                     break;
                case 4:
                     ll.print_list(); 
					 std::cout << "print_list";
					 std::cin.get();
                    //NO BREAK
                case 6:
                    return 0;
                 //NO BREAK

                default:
                        std::cout <<"oops wrong choice"<< std::endl;
                    //NO BREAK
                    }
     std::


your ' //NO BREAK' pattern cuases when you imput 4 it does whats in 4, 6 and defualt and when you imput 6 it does whats in 6 and defualt.... yadayadyada.

so lets fix that and now the code is:

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
#include <iostream>

class link_list
{
public:
 void insert();
 void print_list();
};

void link_list::insert()
{
	return;
}
void link_list::print_list()
{

 return;
}

int main()
{ 
    link_list ll;
   // ll.print_list();
   
    //ll.display_options();
    char choice;
    do{
    std::cout << "select one"<< std::endl;
    std::cout <<  "press 1 for insert ." << std::endl;
    std::cout << "press 2 for insert at beginning ." << std::endl;
    std::cout << "press 3 for insert at the middle ." << std::endl;
    std::cout << "press 4 delete ." << std::endl;
    std::cout << "print 5 the linked list :" << std::endl;
    std::cout << "print 6 the linked list :" << std::endl;
   
         int no;
         std::cin >> no;
         switch (no)
         {
                case 1:
                     ll.insert();	
					 std::cout << "Insert";
					 std::cin.get();

                     break;
                case 4:
                     ll.print_list(); 
					 std::cout << "print_list";
					 std::cin.get();  
					    break;
                case 6:
                    return 0;
                       break;
                default:
                        std::cout <<"oops wrong choice"<< std::endl;
                    	   break;
         } std::cin.get();
     std::cout << "Do u wanna make another choice?" << std::endl;
     std::cin >> choice;
     std::cout<< choice << std::endl;
     }while(choice=='Y'||choice=='y');
    std::cout<<"Thanks!"<< std::endl;
    system("pause");
    return 0;
} 

It works......... I see a big security hole and i would point it out but i am gonna let you find it yourself :p
@TheGentlmen : Thanks a lot for your reply... but actually this is not the whole code... this is only in the main function where i am having problem... I added "#include<iostream>" and "using namespace std"
try putting a getchar() before cin>>choice and see if it works.If it does,post it here.I'll tell you the background details later.
Can you just post the insert function?

Just a stab in the dark: try putting cin.clear(); right before your cin >> choice; line.
long double main: cin.clear(), is it to clear input buffer?
@Pterodactyl

when I put getchar() before cin>> choice , it doesn't do anything else, I mean it doesn't take "choice value" and prints the next value.

@ long double main

below is my code , I haven't written the whole code, I am just going step by step, in the insert function I have just written code to add one node and next time if the user doesn't want to add another node it should go back to the main function and display all the lists again. and also when I add cin.clear it prints "n" for choice which was given in the insert function.
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#include<iostream>

using namespace std;

struct node
{
      int num;
      node *next;
} ;

class link_list
{
      node *head;
      node *list;
      public:
             link_list()
             {
                   head = NULL;
                   list = NULL;
                   //cout << list ;
             }
            // void display_options(link_list &b);
             void print_list();
             void insert();
             //void insert_at_beg();
            // void insert_at_middle();
};  

void link_list :: print_list()
{
     if (list==NULL)
     {
                    cout<< "Empty list"<< endl;
                    return;
     }
     else
     {
      int count =0;      
      while(list->next!=NULL)
      {
         list=list->next;
         count++;    
         cout << "Node        " << "value " << endl;
         cout << count << "         " << list->num <<endl ;       
      }
     }
}  

void link_list :: insert ()
{
      head = new node;
      list = head;
      int y;
      int a;
      cout << "enter number for 1st node" << endl;
      while(cin >> y)
      {
      list->num=y;
      list->next=list;
      
      cout << "do you wish to add another node" << endl;
      cin >> a;
      if(a=='y'||a=='Y')
      {
                      //  break;
      }
      else
      {
       cout<< "protima" <<endl;
      }
      }
      
      
}


int main()
{  
    link_list ll;
   // ll.print_list();
    
    //ll.display_options();
    char choice;
    do{
    cout << "select one"<< endl;
    cout <<  "press 1 for insert ." << endl;
    cout << "press 2 for insert at beginning ." << endl;
    cout << "press 3 for insert at the middle ." << endl;
    cout << "press 4 delete ." << endl;
    cout << "print 5 the linked list :" << endl;
    cout << "print 6 the linked list :" << endl;
    
         int no;
         cin >> no;
         switch (no)
         {
                case 1:
                     ll.insert();
                     break;
                case 4:
                     ll.print_list();
                case 6:
                     return 0;
                     
                default:
                        cout <<"oops wrong choice"<< endl;
                     
         }
     cout << "Do u wanna make another choice?" << endl;
     //getchar();
     cin.clear();
     cin >> choice;
     cout<< choice << endl;
     }while(choice=='Y'||choice=='y');
    cout<<"Thanks!"<< endl;
    system("pause");
    return 0;
}  
@ all I found my mistake ... it's working now... thanks everyone for your time :)
The problem was in insert function i declared "a" as int and was expecting to take char value.
Topic archived. No new replies allowed.