Monday, May 24, 2010

Help in deleting names in 2d array in C.?

# include%26lt;stdio.h%26gt;





char main ()


{


char name [24] ;


char list [6] [24];


char dname [24];


char temp;


printf("\n----------------------------...


printf("\nHello!, Please Enter Six Names of Your Choice");


printf("\nUse a space to separate each name\n");


printf("------------------------------...


for(int val = 0; val %26lt; 6; val++)


{


/* read the name */


scanf("%s", name);


/* copy the read name into the list of names */


for(int val2 = 0; val2 %26lt; 24; val2++)


list[val][val2] = name[val2];


}


printf("\n----------------------------...


printf("\n The names that you entered are:\n");


printf("------------------------------...


for (int i = 0 ; i %26lt; 6 ; i++ )


{


printf ( "%s\n", list[i] );


}


/*delete name*/


printf("\nEnter the name you want deleted\n");


for(int count=0;count%26lt;=5;count++)


{


scanf("%s",%26amp; dname);


if (dname != list[count])


{


printf("\n This name has been deleted: ""%s",dname);


}


}


return 0;


}

Help in deleting names in 2d array in C.?
The following code performs as you describe. I hope you find it useful.





Note: this code was compiled under a C++ compiler. You can use it in C with minor changes (like changing the comment syntax from dowble dash (//) to dash-star star-dash (/* */)).





//------------------------------------...





#include %26lt;stdio.h%26gt;


#include %26lt;string.h%26gt;





//------------------------------------...





#define MAX_NAMES6





char sName[MAX_NAMES][32];





//------------------------------------...





void PrintNames()


{


int i;





/* deleted names are treated as empty strings; the output code will ignore the empty strings thus showing as they were deleted */





printf("\nNames:\n");


for (i = 0; i %26lt; MAX_NAMES; i++)


if (sName[i][0] != '\0')


printf("%s\n", sName[i]);





printf("\n");


}





//------------------------------------...





int main()


{


char sTmpName[32];


int i;





// read the names


printf("Please, enter %d names.\n", MAX_NAMES);


for (i = 0; i %26lt; MAX_NAMES; i++)


{


printf("Enter name %d: ", i + 1);


scanf("%s", sName[i]);


} // end for





// print the list of names


PrintNames();





printf("Enter a name to delete: ");


scanf("%s", sTmpName);


i = 0;


while (i %26lt; MAX_NAMES %26amp;%26amp; strcmp(sTmpName, sName[i]) != 0)


i++;





if (i %26lt; MAX_NAMES)


{


// Name found





/* deleted names are treated as empty strings; so make this string empty (empty strings are diferent from NULL strings: empty strings are "", while NULL strings are those that point to no char array) */


sName[i][0] = '\0';


} // end if


else


printf("\nName \"%s\" NOT found\n", sTmpName);





// print the list of names


PrintNames();





return 0;


}


//------------------------------------...
Reply:Hi,


in the /* delete name*/ part of ur programme.


U have put scanf() in for(;;) loop. Scanf() should be just after the statement:


printf("\nEnter the name you want deleted\n");


then comes ur if() condition, it should check


if (dname == list[count])


then list[count]=" "; i.e., put a null string in place of the name to be deleted.


Then simply print ur array.


for checking string u could use


strcmp(dname,list[count]); function. It is in string.h header file. so include it #include%26lt;string.h%26gt;


if both the strings are equal it will return 0.





if (!strcmp(dname,list[count])


list[count]=" ";


i.e., put a null string in place of
Reply:Two things:





1. You're not actually deleting the entry from your array.





2. In C, use strcmp(dname, list[count]) for comparing strings. The "==" and "!=" operators don't work on strings (or at least not in the way you'd expect)





Several other things:


1. You can replace for(int val2 = 0; ...) list[val][val2] = name[val2] with:





strcpy( list[val], name );





Strcpy does that work for you.





2. You have the scanf() INSIDE your 0..4 loop so the user will have the enter a name 5 times. Put it outside the loop.

brandon flowers

No comments:

Post a Comment