Thread: switch function

  1. #16
    Registered User
    Join Date
    May 2007
    Posts
    88
    As ZuK said earlier, you really shouldn't mix C and C++-style IO. Use cin.get(), not gets(). Incidentally, you should always use fgets() rather than gets(). If you're writing C++, you shouldn't use either of them.

  2. #17
    Registered User
    Join Date
    Nov 2006
    Posts
    38

    Question

    hi thanks for all the help so far the code i use is just what i was told to do during my c++ course though all corrections have been made a note of so i can use them in the future

    another snag though

    Code:
    	case 's':
    		 {
    		   system("cls");
    		    	cout<<"search options \n";
    				cout<<"what would you like to do? \nTo search for full contact details press F \nTo search for addresses  press A \nTo search for telephone numbers press T \nTo search for email address press E\nTo search for the members of a politial party press P\nTo quit press Q :- ";
      				cin>> choice2;
    			
    			
    				while(count2 = 0) 
     					{
         				 choice2 = getchar ();
    	 				 switch(choice2)
    	   					{
    	   					 case 'F':
    		 	 	 	 	  {
    						   tdata.findF();
    		 	 	 	 	    count2 = 1;
    		     	 	 	    break;
    		    			  }
    agein it jumps the switch
    k i tried removing the cin agein but this stops me from being able to type any letter

    any ideas

  3. #18
    Registered User
    Join Date
    May 2007
    Posts
    88
    Hint: This is exactly the same problem you were having before.

    Also, as a matter of good coding style, you probably shouldn't have any lines of code that are, like, 200 columns long.

  4. #19
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Code:
    while(count2 = 0)
    this condition is alwais false ( that is assignement that returns 0 )
    you want
    Code:
    while(count2 == 0)
    Kurt

  5. #20
    Registered User
    Join Date
    Nov 2006
    Posts
    38
    k i changed the while but the cin cant be removed do i have to use the cin.ignore agein or am i barking up the wrong tree

  6. #21
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    I'd think there should only be one input statement ( inside the loop ) either cin >> choice or choice=getchar().
    Kurt

  7. #22
    Registered User
    Join Date
    Nov 2006
    Posts
    38
    k i took out the get char and it worked thanks agein

  8. #23
    Registered User
    Join Date
    Nov 2006
    Posts
    38

    Question

    anotther one

    when i call this function it prints out eurika 12 times

    Code:
    void store::findF(void)
    {
    	int i = 0;
    	bool serch = false;
    	char person[20];
    	
    	system("cls");
    	
    	cout << "please enter the name of the person you wish to find their details :  ";
    	gets(person);
    
    	for(i = 0; i < Voters; i++)
    	{
    		if(!strcmp(np[i].name, person))
    		  {
    		 	 cout << "Eurkia \n" << np[i].name <<  "\n" << np[i].phone << "\n" << np[i].address << "\n" << np[i].email << "\n" << np[i].party ;
    		 	 serch = true;
    		  }
    	}
     
    	if(serch == false)
    	  {
    	 	 cout << "Sorry record not found.";
    	  }
    }
    it shouldent any ideas


    ps sorry for keep asking you guys are brilliant mush more help than my tutor
    thanks for spending your time on this for me

  9. #24
    Registered User
    Join Date
    May 2007
    Posts
    88
    There's quite a bit wrong with that code: using system("cls"), using gets(), using global variables that really shouldn't be global, spelling the word search "serch", etc.

    You should really learn about C++ strings and containers, they would make this job a breeze.

  10. #25
    Registered User
    Join Date
    Nov 2006
    Posts
    38
    k i understand that my knolage is laking but i only have two days left on this project and i just need to get it working please help

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