Thread: switch function

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    38

    Unhappy switch function

    hi my program keeps skiping my switch function any ideas??

    Code:
    int main (void)
    {
      people datab;
      store tdata;
      char any;
      
      char choice;
      char choice2;
      float count = 0;
      float count2 = 0;
      int c; 
      
      tdata.clearary();
      
      cout<<"\nThis program will allow the user to create and then control a database. \n";
      
       
      cout<<"what would you like to do? \n To enter a new record press n \nTo delete a record press d \nTo view a full list of the users with details press f \nTo view serch options press s\n to quit press q :- ";
      cin>>choice;
       
    
     while(count == 0) 
     {
          choice = getchar ();
    	  switch(choice)
    	   {
    	    case 'n':
    		 {    
    		      system("cls");
    			  tdata.load();
    		      tdata.enter(1);
    			  tdata.save();
    		  count = 1;
    		  break;
    		 } 
    		 
        	case 'd':
    		 {
    		  //delete a record ??
    		  count = 1;
    		  break;
    		 }
    		 
    		 case 'f':
    		 {
    		  system("cls");
    		  tdata.list();
    		  count = 1;
    		  break;
    		 }
    p.s this is just a snipit also using quincy

  2. #2
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Choice will be '\n' the first time in the loop. You're not handling that case.
    Kurt

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    38
    could you explain how

  4. #4
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Code:
      cin>>choice;
    will read a char and leave '\n' in the buffer.
    Code:
    choice = getchar ();
    getchar will return '\n'.
    Kurt

  5. #5
    Registered User
    Join Date
    Nov 2006
    Posts
    38
    so how do i fix that

  6. #6
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    I would remove the
    Code:
    cin>>choice;
    Kurt

  7. #7
    Registered User
    Join Date
    Nov 2006
    Posts
    38
    but wouldnt that stop me getting the users answer

  8. #8
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Code:
    choice = getchar ();
    Should reads the users choice as well.

    BTW it is never a good idea to mix c-style with c++ style IO.

    Kurt

  9. #9
    Registered User
    Join Date
    Nov 2006
    Posts
    38

    Question

    could you fix this for me as well as that worked fine

    Code:
     char k = 'y'; 
    	 while (k == "y");

  10. #10
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    If you use doublequotes, the char inside will be treated as a C-style string. That's wrong, although the compiler might be extra smart about it. Use single quotes for single chars.

  11. #11
    Registered User
    Join Date
    May 2007
    Posts
    88
    Code:
    while (k == 'y') {

  12. #12
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    That shouldn't compile.
    try
    Code:
    	 char k = 'y'; 
    	 while (k == 'y'); // this would be an infinite loop. Do you really want the ;
    Kurt

  13. #13
    Registered User
    Join Date
    Nov 2006
    Posts
    38
    thanks i didnt notice that

  14. #14
    Registered User
    Join Date
    Nov 2006
    Posts
    38

    Question

    k i got it mostly working but for some reason the first two questions print in a single line
    Code:
     void people::enter(int i) 
       {
        char k = 'y'; 
    	 while (k == 'y') 
    	   { 
    	    cout << "Enter the name of the person ";
    	     gets(np[i].name);
            cout << "Enter the phone number of the person ";
    	     gets(np[i].phone);
    		cout << "Enter the full address of the person ";
    		 gets(np[i].address); 
    		cout << "Enter the Email address of the person";
    		 gets(np[i].email);
    		cout << "enter the name of the party that the person belongs to ( conservative, labour, liberal, other)";
    		 gets(np[i].party);
    		cout << "Do you wish to enter another member ? (y/n)";
    		cin  >> k;
    		}
    	}
    any idea

  15. #15
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Let me guess. Before calling that function you are using getchar() to read a character.
    You will have to get rid of a leftover '\n' in the input buffer.
    cin.ignore() could help.
    Kurt

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner Needs help in Dev-C++
    By Korrupt Lawz in forum C++ Programming
    Replies: 20
    Last Post: 09-28-2010, 01:17 AM
  2. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  3. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  4. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  5. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM