This should be easy 2 
Author Message
 This should be easy 2

Now this is what i asked you to
help me to solve. I know i should
have decribed my problem in a better
way. Sorry.
Mabe you could come up with
a better solution to the problem.
Please tell me how!

The file "land.dat" look like this:

10
swe dan dan nor swe swe tur swe pol dan

#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>
#define MAXLAND 10

typedef struct
{
char namn[4];

Quote:
}test;

int main(void){
FILE *fil;
test Ttest[MAXLAND],Ttemp[MAXLAND];
int k=0,counter=0,ej_lika,m,stycken,a;
fil=fopen("land.dat","r");

if(!fil){
printf("ERROR open file!");
exit(1);

Quote:
}

fscanf(fil,"%d",&stycken);

while(!feof(fil)){
 fscanf(fil,"%s",&Ttest[k].namn);
 k++;

Quote:
}

fclose(fil);

for(k=0;k<=MAXLAND;k++)
Ttemp[k].namn==NULL;

/' Can i do this different??*/
for(k=0;k<=stycken-1;k++){
ej_lika=0;
  for(m=0;m<=stycken-1;m++){
    if(strcmp(Ttest[k].namn,Ttemp[m].namn)!=0)
    ej_lika++;
      if(ej_lika==stycken){
      strcpy(Ttemp[counter].namn,Ttest[k].namn);
      ej_lika=0;
      counter++;
      break;
  }
 }

Quote:
}

/*check the result.*/
for(k=0;k<counter;k++)
printf("%s\n",Ttemp[k].namn);

getch();
return 0;

--



Thu, 31 Jan 2002 03:00:00 GMT  
 This should be easy 2
<< Please tell me how! >>

Sure, I'll tell you how. But first you must tell me what.

You posted a message *as though* it was part of thread, but it was not part
of a thread. No context, no explanation.

You do not say that your program does, what is wrong with it, or what has
happened already in the abandoned thread.

--

Paul Lutus
www.arachnoid.com


<snip>

--



Fri, 01 Feb 2002 03:00:00 GMT  
 This should be easy 2


Quote:
>10
>swe dan dan nor swe swe tur swe pol dan

If your file really starts with '10' then the logic of your program is
false.

If the general format of your data files is like this (number of items
followed by a list of them) you need to read the number first, then use
malloc to create a dynamic array to hold the data.  I am reluctant to go
into more detail because this has a strong sense of being a homework
assignment.

Francis Glassborow      Journal Editor, Association of C & C++ Users
64 Southfield Rd
Oxford OX4 1PA          +44(0)1865 246490
All opinions are mine and do not represent those of any organisation
--



Fri, 01 Feb 2002 03:00:00 GMT  
 This should be easy 2
Hi,

your code was terribly indented, almost unreadable.

#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>
#define MAXLAND 10

typedef struct
{
char namn[4];

Quote:
}test;

int main(void){
     FILE *fil;
     test Ttest[MAXLAND],Ttemp[MAXLAND];
     int k=0,counter=0,ej_lika,m,stycken,a;
     fil=fopen("land.dat","r");

     if(!fil){
          printf("ERROR open file!");
          exit(1);
     }
     fscanf(fil,"%d",&stycken);

     while(!feof(fil)){
        fscanf(fil,"%s",&Ttest[k].namn);
        k++;
     }
     fclose(fil);

     for(k=0;k<=MAXLAND;k++)
          Ttemp[k].namn==NULL;  /* this makes no sense !!!! */

     /' Can i do this different??*/
     for(k=0;k<=stycken-1;k++){
          ej_lika=0;
          for(m=0;m<=stycken-1;m++){
               if(strcmp(Ttest[k].namn,Ttemp[m].namn)!=0)
                    ej_lika++;
               if(ej_lika==stycken){
                    strcpy(Ttemp[counter].namn,Ttest[k].namn);
                    ej_lika=0;
                    counter++;
                    break;
               }
          }
     }

     /*check the result.*/
     for(k=0;k<counter;k++)
          printf("%s\n",Ttemp[k].namn);

     getch();
     return 0;

Quote:
} /* this was missing !!!!! */

Some comments:

* Since Ttemp is not initialized when used first, the results are
completely undefined.

* If you want to eliminate duplicate entries, then you are not far away
from a correct solution. I think if you solve the problem with the
missing initialisation of Ttemp, then it should be almost done. You
should know how to copy the empty string "" to a char array? Hint, Hint.

HTH

Bernd Strieder
--



Sun, 03 Feb 2002 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. I am new to programming and am lost

2. EASY EASY BEGINNER ??

3. @> two easy easy questions

4. how good am I? Am I Good Enough????

5. Determine where I am running

6. System.Threading.Timer , am I doing this correctly ?

7. Am i connected?

8. Request - Opinions on Book I am considering:

9. I am puzzled: __nogc new = LNK2001

10. Am I in the wrong NG???

11. I am a convert to .NET

 

 
Powered by phpBB® Forum Software