• 0

need help with strcpy...


Question

Im going through a list of names(namn[m]) and I want to compare them with a few other names (tankbaraNamn[k]).....now my problem is:

if a name, in the array tankbaraNamn[k], can't be found in the array, name[m], I want the program to simply print the name which couldn't be found (tankbaraNamn[k] and say that it's not in the list. My strcpy isn't working though, I get error messages whenever I try to copy a specific part of the array(tankbaraNamn[k] to another string(tempNamn)..I don't know how to solve this...can anyone please help me?

Im using the program Dev-C++.

NOTE: tankbaraNamn[k] and tempNamn are declared:

char tankbaraNamn[5][15], tempNamn[15];

tankbaraNamn[k] has previously in the code scanned the strings which should be located there (and it works as it should)

//start on error--------------------------------

for(k=0;k<poster;k++)
 for(m=0;m<100;m++)
 {
  if((strcmp(tankbaraNamn[k], namn[m])==0))
  {
   hittat=1;
   printf("%s finns p? plats %d\n",tankbaraNamn[k], m);
  }
  else
  {
   strcpy(tempNamn, tankbaraNamn[k]);
   hittat=0;
  }
  if(hittat==0)
   printf("%s finns inte med p? listan\n", tempNamn);
  else
  ;
 }

//end of code------------------------------------

plz help a poor gu:(:(

Link to comment
Share on other sites

13 answers to this question

Recommended Posts

  • 0

How about instead of making a copy of the name (char*) that is not found, make a note of the index. Like this:

for(k=0;k<poster;k++)
{
	for(m=0;m<100;m++)
	{
  if((strcmp(tankbaraNamn[k], namn[m])==0))
  {
 	 hittat=1;
 	 printf("%s finns p? plats %d\n",tankbaraNamn[k], m);
  }
  else
  {
 	 hittat=0;
 	 printf("%s finns inte med p? listan\n", tankbaraNamn[k]);
  }

	}
}

Link to comment
Share on other sites

  • 0

Yea, but then the program will print out the names it doesn't find several times (like 80)...I only want it printed once..which is why I have to strcpy somehow.

someone solve this please :(

Link to comment
Share on other sites

  • 0
Yea, but then the program will print out the names it doesn't find several times (like 80)...I only want it printed once..which is why I have to strcpy somehow.

someone solve this please :(

That seems to be what you're doing anyways...execpt with an extra if statement.

Link to comment
Share on other sites

  • 0

"That seems to be what you're doing anyways...execpt with an extra if statement"

ye but the difference is that those words can only be printed once (since they will be found 1 time for sure) but the other words is printed every time the for-loop counts +1 since they will never be found

Link to comment
Share on other sites

  • 0
"That seems to be what you're doing anyways...execpt with an extra if statement"

ye but the difference is that those words can only be printed once (since they will be found 1 time for sure) but the other words is printed every time the for-loop counts +1 since they will never be found

for(k=0;k<poster;k++)
{
count = 0;
for(m=0;m<100;m++)
{
 if((strcmp(tankbaraNamn[k], namn[m])==0))
 {
 ?printf("%s finns p? plats %d\n",tankbaraNamn[k], m);
 ?++count;
 }
 else if (count <= 0)
 {
 ?printf("%s finns inte med p? listan\n", tankbaraNamn[k]);
 }

}
}

Edit: Guess you want it printed out multiple times if it's found multiple times?

Edited by kjordan2001
Link to comment
Share on other sites

  • 0

no, I want everything printed only once...if the tankbaraNamn[k] is located in the namn[m] the name should only be printed once...if it's not found, the tankbaraNamn[k] should be printed too (once).

Link to comment
Share on other sites

  • 0

for(k=0;k<poster;k++)
{
count = 0;
for(m=0;m<100;m++)
{
if((strcmp(tankbaraNamn[k], namn[m])==0))
{
 printf("%s finns p? plats %d\n",tankbaraNamn[k], m);
 ++count;
}


} // inner for-loop
   if (count <= 0)
   {
       printf("%s finns inte med p? listan\n", tankbaraNamn[k]);
   }
}// outer for-loop

Wouldn't you want to put the test for count being <= 0 outside of the inside loop if you only wanted printed once? Same should go for printing if found.

Link to comment
Share on other sites

  • 0
no, I want everything printed only once...if the tankbaraNamn[k] is located in the namn[m] the name should only be printed once...if it's not found, the tankbaraNamn[k] should be printed too (once).

Okay, then you should go for the break code I had put before but then changed:

for(k=0;k&lt;poster;k++)
{
count = 0;
for(m=0;m&lt;100;m++)
{
if((strcmp(tankbaraNamn[k], namn[m])==0))
{
 printf("%s finns p? plats %d\n",tankbaraNamn[k], m);
 ++count;
 break;
}
}
if (count &lt;= 0)
{
 printf("%s finns inte med p? listan\n", tankbaraNamn[k]);
}

}

This way you don't waste time looking for something you've already found since you said you only want it printed out once.

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.