Thread: guys, help hot to clear a string in 2d array

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    6

    Post guys, help hot to clear a string in 2d array

    [SIZE="2"][COLOR="Black"]let me guys, look at my program, i can register upto 3 members and can view it...
    but i dont know how to clear the string in 2dimensional array
    i declared the variables (ex. name[3][20])
    3 for the number of array....
    20 for the length of the string...

    who can do delete code for the array?
    here's my program....

    Code:
    #include <stdio.h>
    #include <string.h>
    #define p printf
    #define s scanf
    
    int avail=0,slot[3];
    char empcode2[20],empcode[3][20],name[3][20];
    
    main()
    {
    	char chmain;
    	clrscr();
    
    	p("[A] - Add\n");
    	p("[B] - View\n");
    	p("[C] - Delete\n");
    	chmain=tolower(getche());
    	do
    	{
    	p("\b");
    	clreol();
    	}
    	while(chmain!='a' && chmain!='b' && chmain!='c');
    
    	switch(chmain)
    	{
    		case 'a':
    		add();
    		exit(0);
    		break;
    
    		case 'b':
    		view();
    		exit(0);
    		break;
    
    		case 'c':
    		delete();
    		exit(0);
    		break;
    	}
    	getche();
    }
    
    
    
    add()
    {
    	int x=0;
    	char chadd;
    	clrscr();
    
    	for(x=0;x<3;x++)
    	{
            	if(avail==3)
    		{
    		p("MAXIMUM");
    		getche();
    		main();
    		exit(0);
    		}
    
    		if(slot[x]==0)
    		{
    		p("Enter Employee Code: ");
    		s("%s",&empcode[x]);
    
    		p("Enter Name: ");
    		s("%s",&name[x]);
    
    		slot[x]=1;
    		avail++;
    		}
    
    	p("Do You Want To Register Again\n");
    	p("[Y] - Yes\n");
    	p("[N] - No\n");
    	chadd=tolower(getche());
    
    	if(chadd=='y')
    	{
    		if(avail!=3)
    		clrscr();
    
    		else
    		{
    		p("\nMaximum");
    		getche();
    		main();
    		exit(0);
    		}
    	}
    
    	if(chadd=='n')
    	{
    	main();
    	exit(0);
    	}
    
    }
    }
    
    view()
    {
    	int y=0;
    	char chview;
    	clrscr();
    
    	p("Enter Employee Code: ");
    	s("%s",&empcode2);
    
    	for(y=0;y<3;y++)
    	{
    	if(strcmp(empcode2,empcode[y])==0)
    	p("%s",name[y]);
    	}
    	p("\nDo You Want To Seach Again\n");
    	p("[A] - Search Again\n");
    	p("[B] - Main Menu\n");
    	chview=tolower(getche());
    
    	switch(chview)
    	{
    		case 'a':
    		view();
    		exit(0);
    		break;
    
    		case 'b':
    		main();
    		exit(0);
    		break;
    	}
    
    	getche();
    }
    
    delete()
    {
    	int z=0;
    	char chdelete;
    
    	p("Enter Employee Code: ");
    	s("%s",&empcode2);
    
    	for(z=0;z<3;z++)
    	{
    		if(strcmp(empcode2,empcode[z])==0)
    		{
    		p("%s",name[z]);
    
    		chdelete=tolower(getche());
    			if(chdelete=='y' )
    			{
    			/* Space For Delete Code */
    			}
    
    			if(chdelete=='n')
    			{
    			main();
    			exit(0);
    			}
    		}
    
    	}
    ]

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Several points to note:

    1. Question 10.2
    Don't use the pre-processor to rewrite the language. After a couple more letters, your code will be unreadable.

    2. Don't recursively call main(). If you want to get back to main, do a return.

    3. You also seem to be abusing exit() as well. This exits the whole program, not just the current function. Your program only performs one action and then it's all over.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Name[i][0] = '\0';

    Will delete the ith entry in the Name[i][] array. The other char's will be in place, but since Name[i][0] is NULL, that whole string is null (empty).

    I noticed you called main() in your other functions - you don't want to ever do that. Use a do while loop around your menu block of code, instead. When your other functions are done, they just *return* naturally, to the do while loop in main().

    Calling main() again, will eventually crash your program (not right away, but before very long).

    The general idea: (this is not code to run, it's code logic to study).
    Code:
    int main(void) {
      //declare your variables
    
      //then BIG do while loop:
      do {
         all your menu code in here
    
         get user choice
    
    
         switch or if statements handle users choice,
         calling functions as needed
    
      }while(choice != 'q' && choice != 'Q');
      //when user chooses q the program ends
      return 0;
    }

  4. #4
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    There is no way to clear the contents of an array. An array always contains something. You have to decide how you would like to signal an empty string. Perhaps it's because the first character is '\0' as was suggested. Or you can fill the entire string with spaces. Or you can have a separate flag or mode indicator that contains a code number of your choosing. You haven't got the logic in your program yet to differentiate full vs. empty entries, so it's up to you.

  5. #5
    Registered User
    Join Date
    Mar 2011
    Posts
    6
    so what is the right code for adding, viewing and deleting?

    my program is a weekly payroll system....but i need to have a menu...

    4options for main menu...

    1st is the add...
    where i can add and delete member....

    2nd is view....
    where i can view how much is the salary and other member's information...

    3rd is compute..
    compute the total hours worked, in this case i need to compute hours with log-in logout system...
    ex. monday, 8:00 is login...and 17:00 is logout, so it is 9hours, but it will become 8hours only beacause of the breaktime...

    4th is the quit...
    where the program will automatically exit
    using sleep...

    i need to do the 1st and 2nd option....
    the 3rd and fourth, i think i can make it on my own...

    help guys...
    thx in advance...
    Last edited by ajmosca25; 03-23-2011 at 09:06 AM.

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You want a BIG switch statement, right in main(), or in a menu() function.
    Code:
    int main(void) {  //it's always int main(void) for now.
      //declare your variables for main, here
    
      do {
        //show your menu options in here, including one to [Q]uit
        //get the user's choice
        //and handle it with function calls
        //from your switch statement
      
    
    
      }while(choice != 'q' && choice != 'Q');
    
      return 0;
    } //end of main

    No sleep statement is needed or desired.
    Last edited by Adak; 03-23-2011 at 09:54 AM.

  7. #7
    Registered User
    Join Date
    Mar 2011
    Posts
    6
    how about the add function? in my program above,
    you can add member, but once you visit the view function...
    and then go back to add(), i cant fill up or type...

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Like I said, you need to remove all the recursive calls to main, calls to exit(), and replaces all those horrid p and s macros.

    Then it's something like
    Code:
    void add ( ) {
      char again;
      do {
        for ( i = 0 ; i < 3 ; i++ ) {
            if ( slot[i] == 1 ) continue;
            // input
        }
        if ( i == 4 ) {
            // full
        } else {
            // prompt for again
        }
      } while ( again == 'y' );
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    Registered User
    Join Date
    Mar 2011
    Posts
    6
    i got the same problem in your code....when i visit in the add() function again after viewing members...it always prompt for confirmation of adding members...and also when adding...i got no choice to get back to main..

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Don't do exit(), in your function - your ONLY exit you need, can be done through main(), just by completing the program.

    Don't call other functions, EXCEPT at the switch statement in main(). Main() is your hub - you call the other functions, like spokes on a bicycle wheel, and then your program ALWAYS returns, to main(), (not by calling main again, but by finishing up in the current function, and returning to main() ), within the BIG program loop. If you need to call two functions (and you will need to do that), then in main:

    Code:
    case 2: delete
        //first, find out who they want to delete
        //then search and see if that person is in the data
        // if found, mark that record deleted (no don't erase it
        // just set his name[0] to '\0' and his ID number to 0,
        // and set up your display, so those types of records
        // will not be displayed. 
        break;

  11. #11
    Registered User
    Join Date
    Mar 2011
    Posts
    6
    ahh, but how can i end adding members when i choose 'n' for no to again...
    it always loop it again and again until the end of for loop....

    and i have always and error, type mismatch in redeclaration of "add" if there is void before add in declaration..

    how can i ignore using main() if i cant get to main menu???
    its so are you telling me, if i add member....then it will direct me to the main?
    Last edited by ajmosca25; 03-25-2011 at 11:59 AM.

  12. #12
    Registered User
    Join Date
    Mar 2011
    Posts
    6
    guys, im already finished the 1st and 2nd option of main menu....

    im in calculating total time....
    i thought it was so easy but not...

    my formula is total hour = time out hour - time in hour....
    but i forgot that there is minutes...

    way to input time is
    Enter Time In : 8:00
    Enter Time Out : 17:00

    help....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char Handling, probably typical newbie stuff
    By Neolyth in forum C Programming
    Replies: 16
    Last Post: 06-21-2009, 04:05 AM
  2. from 2D array to 1D array
    By cfdprogrammer in forum C Programming
    Replies: 17
    Last Post: 03-24-2009, 10:33 AM
  3. sorting 2D string array
    By sureshhewa in forum C Programming
    Replies: 14
    Last Post: 07-27-2008, 01:30 PM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  5. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM