Hi everyone!

This is my first post and i'm also a newbie so don't be harsh on me!

I have this exercise to do which asks pretty much what the title says.

I have a function that reads strings from a file and puts them in an array. Then I insert from the keyboard a new string to the same array and I have a second function where it tries to sort them alphabetically.

I managed to write down a version of the code, using bits and pieces, without really understanding what I'm writing. Here it is:

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

FILE *file;

int ReadStrings(char filename[], char A[], int size);
int InsertStrings (char num[], char A[], int size, int noElements);

int main(void)
  {
      int c, i, n, d, result;
      char str[100], name[100], m[100];
      
      printf("Please enter the file's name:");
      scanf ("%s",name);
      
      do 
         {
             printf("\nPlease enter the size of the array (no bigger than 100):");
             scanf("%d",&n);
         }
      while (n>100);
      
      result=ReadStrings(name,str,n);
      printf("\nThe number of strings inserted in the array are %d\n",result);
    
      for (i=result;i<=n-1;i++){
      printf("\nEnter new string to sort in the array, -1 to end: ");
      scanf("%s",m);
      
      d=InsertStrings(m, str, n, result);
      result++;
      
      if (d==0)
         printf("\n0 - No number was inserted\n");
      else if (d==1)
         printf("\n1 - One number was inserted\n");
         }
      system ("pause");
      return 0;
  }   

int ReadStrings(char filename[], char A[], int size)
{

 int i=0;

 if ((file = fopen (filename, "r" ) ) != NULL )
    {
     while (!feof (file)&&(i<size))
      {   
         fscanf(file,"%s",&A[i]);
         printf("%s ",&A[i]);
         i++;
         if (i == size)
            {
             printf("\n0 - No number was inserted\n");
             break;
             }    
      }             
    } 
 else
  { 
      printf("\n Error at File Open \n");
  }

return i ;   
}

int InsertStrings(char num[],char A[], int size, int noElements)
{
 int i=0,L=1,m,j,k=0,pos, d=1;;
 char B[100]={},C[100]={};

 for(i=0;i<size;i++)       
    B[i]=A[i];

 printf("You have entered %s\n",num);

 i=-1;

 while(i<size)
 {
  i++;   

  if(B[i]==' ')                       
      {
       k=0;
       m=strcmp(num,C);

       if(m<=0)                               
          {
           k=0;

           for(j=0;j<size;j++)
              C[j]=' ';  

           break;
          }

       else                                  
          {
           k=0;                                      
           L++;                                      

           if((B[i]==' ')&&(B[i+1]==' '))
              break;

           for(j=0;j<size;j++)
              C[j]=' ';
          }
      }
      
      else if(B[i]!=' ')
      {                                     
         C[k]=A[i];
         k++;
      }      
 }
 
 printf("''%s'' will be placed in array spot %d\n",num,L);

 if(L==1)
 { 
   for(i=0;i<size;i++)
      A[i]=' ';
  
   for(i=0;i<(strlen(num));i++)
      A[i]=num[i];
  
   k=0;
  
   for(i=(strlen(num)+1);i<size;i++)
   {
      A[i]=B[k];
      k++;
   }
   
   printf("\n");
  
   for(i=0;i<size;i++) 
      printf("%s",&A[i]);     
 }
 
 else if(L>1&&L<=noElements)
 {
  k=0;
  
  for(i=0;i<size;i++) 
     {
      if(B[i]==' ')
      {
       k++;
       pos=i;
       
       if(k==L-1)
          break;
      }
     }
     
   j=0;
   
   for(i=(pos+1);i<((pos+1)+(strlen(num))+1);i++)
      {
       A[i]=num[j];
       j++;
      }
   
   j=pos+1;
   
   for(i=((pos+1)+(strlen(num))+1);i<size;i++)
      {
       A[i]=B[j];
       j++;
      }
   
   printf("\n");
  
   for(i=0;i<size;i++) 
      printf("%s",&A[i]);   
     
 }

 else if(L>noElements)
 
 {
  k=0;
  
  while(k!=noElements)
  {  
   for(i=0;i<size;i++) 
      {
       if(B[i]==' ')
       {
        k++;
        pos=i;
        
        if(k==L-1)
           break;
       } 
      }
   
   k=0;
   
   for(i=(pos+1);i<( (pos+1)+(strlen(num)));i++)
      {
       A[i]=num[k];
       k++;
      }
      
   printf("\n");
  
   for(i=0;i<size;i++) 
      printf("%s",&A[i]); 
     
}

return d;
}
}

Can you help me out to what I want? The program seems to be working fine up to a certain point but then all hell's breaking loose! Thnx in advance!

I don't know how to do file i/o in C. I only know c++.

commented: Then why bother to post? -4
commented: yep. dont post then -1

oops.. sorry! posted on the wrong side of the forum... could the admin move it to c???

meanwhile, here's another approach i've been trying

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

FILE *file;

int ReadStrings(char filename[], char A[], int size);
int InsertStrings (char num[], char A[], int size, int noElements);

int main(void)
  {
      int c, i, n, d, result;
      char str[100], name[100], m[100];
      
      printf("Please enter the file's name:");
      scanf ("%s",name);
      
      do 
         {
             printf("\nPlease enter the size of the array (no bigger than 100):");
             scanf("%d",&n);
         }
      while (n>100);
      
      result=ReadStrings(name,str,n);
      printf("\nThe number of strings inserted in the array are %d\n",result);
    
      for (i=result;i<=n-1;i++){
      printf("\nEnter new string to sort in the array, -1 to end: ");
      scanf("%s",m);
      
      d=InsertStrings(m, str, n, result);
      result++;
      
      if (d==0)
         printf("\n0 - No number was inserted\n");
      else if (d==1)
         printf("\n1 - One number was inserted\n");
         }
      system ("pause");
      return 0;
  }   

int ReadStrings(char filename[], char A[], int size)
{

 int i=0;

 if ((file = fopen (filename, "r" ) ) != NULL )
    {
     while (!feof (file)&&(i<size))
      {   
         fscanf(file,"%s",&A[i]);
         printf("%s ",&A[i]);
         i++;
         if (i == size)
            {
             printf("\n0 - No number was inserted\n");
             break;
             }    
      }             
    } 
 else
  { 
      printf("\n Error at File Open \n");
  }

return i ;   
}

int InsertStrings(char num[],char A[], int size, int noElements)
{
   int d, i, j;
   char tmp[100];

     if (noElements>=size)
        {
         d=0;
         return d;
         system ("pause");}
     
     else {
     
     for(i=0;i<size;i++)
      { 
          if(i>=noElements)
          {
              for(j=size-1;j>=i;j--)
                  A[j] = A[j-1];
 
              break;
          }    
          
      }}
      
     noElements++; 
     A[i] = num[i];           

      for (i=0; i< (noElements -1); i++)  
    {
          for(j = (i+1); j < noElements; j++)   
         {
                if (A[i] > A[j])          
               {
                        tmp[i]= A[i];          
                        A[i] = A[j];
                        A[j] = tmp[i];
               }
          }
     }

      for(i=0;i<noElements;i++)
          printf("%s ",A[i]);
      
      d=1;
          
      return d;
  }

any thoughts?

Hi everyone!

Hi there!!!

This is my first post and ...

Really? I wondered what that 1 posts meant under your name! How 'bout that!

... i'm also a newbie so don't be harsh on me!

Why not? If your question is lame you deserve it. If your question is well thought out, you don't... :icon_wink:

I have this exercise to do which asks pretty much what the title says.

Don't use the TITLE as your question. Post the question and all pertinent details in the post. By the way, that was a very good title. +2 points!

I have a function that reads strings from a file and puts them in an array. Then I insert from the keyboard a new string to the same array and I have a second function where it tries to sort them alphabetically.

I managed to write down a version of the code, using bits and pieces, without really understanding what I'm writing. Here it is:

Code tags! First post! +10 points!!! :)
Unfortunately, that's where you left it.

If you don't understand what you are writing, you should not be writing code. You need to sit at a desk with pencil and paper and plan -- yes plan -- what you need to accomplish.

Write down what the task is.
Break that description into pieces -- like in English class, what are the nouns, verbs, and the implied parts (but don't really look for nouns and verbs -- that's English class)

Write down how you accomplish each task. Then break that down into smaller pieces until you get to the point each step is as small as it can get.

There's your design. Now run through it by hand to see if it does what you want. Write down variables as they change, write down your linked list and where each node gets pointed to. YOU be the computer and that description is your code.

When that's done, works, etc., now you're ready to start coding. Take a section at a time. Do the input. Get it working completely. Add the output. Get it working. Then one more step. Get it working. Add another... ad nauseum.

That's programming! (sorry, but that's really how we do it.)


Now, for the rest of your post:

Can you help me out to what I want? The program seems to be working fine up to a certain point but then all hell's breaking loose! Thnx in advance!

You want us to read an understand your entire program (that even you didn't understand) and figure out:
1) if you are correct and it works fine up to -- somewhere
2) where that certain point is
3) what is going wrong
4) what "all hell's breaking loose!" means.

Time for:
[boilerplate_help_info]

Posing requests for help must be well thought out if you want help quickly and correctly.  Your post did not meet the criteria for quality help. You may get some posts, but are they going to be useful?  Check your post with these checkpoints - what is it [i]you[/i] missed:
[list=1]
[*]Ask a question that can be answered. Do not ask
- What's wrong with my code?
- Why doesn't this work?
- Anything else that does not give us useful information
[*]Post your code.  If we don't know what you did, how can we possibly help?
- Use [b]PROPER FORMATTING[/b] -- see this
- Use CODE Tags so your formatting is preserved.
If we can't follow your code, it's difficult to help. We don't care that you're still working on it. If you want us to read it, it must be readable
[*]Explain what the code is supposed to do.  If we don't know where the target is, how can we help you hit it?
[*]Explain what actually happened! If we don't know where the arrow went when you shot it, how can we tell what went wrong and how far from the target you are?
[*]If you have errors, post them! We can't see your screen.  We can't read your mind. You need to tell us what happened.
[*]Do [b]not[/b] ask for code. We are not a coding service. We will help you fix your code. 
    If anyone posts working code for you, they are a cheater. 
    If you use that code [i]you[/i] are a cheater.
[*]Do [b]not[/b] bore us with how new you are. We can tell by your code.
- Do not apologize. We were all new, and unless you are completely 
  brain dead you will get better.
- Do not ask us to "take it easy on you."
- Do not say "I don't know what's going on." That's obvious since
  you posted for help. Use that time wisely by [b]explaining[/b] as best 
  you can so we can help.
[*][b]Do not post your requirements and nothing else. [/b]We view that as a lazy do-nothing student that wants us to do their work for them. That's cheating and we [i]will[/i] be hard on you.
[*]Do not attach files except when absolutely necessary. Most of us are not going to download file.  Add the information to your post.
[*][b]Do not tell us how urgent it is.[/b] Seriously, for us there is no urgency at all. Many that can help will ignore any URGENT or ASAP requests.
[/list]
Think more about your next post so we don't have to play 20 questions to get the info we need to help you.

[/boilerplate_help_info]

thanx for your advice WaltP!

i followed your advice and i've been working on the second version i've posted. my approach is this:

1. i enter the filename-> done
2. i enter the size of the array.
- if the the size is smaller than the strings the file contains, program terminates-> done
- if the the size is bigger i goto step 3.
3. function ReadStrings, reads the number of the strings in the array and prints it, along with the strings themself-> done
4. i enter the new string -> problem

that's where the program is terminating. the code at that point is:

for (i=result;i<=n-1;i++){
printf("\nEnter new string to sort in the array, -1 to end: ");
scanf("%s",m);

d=InsertStrings(m, str, n, result);

Could my mistake be, not calling the parameters of the function properly?

Could be. There's not enough info to tell. Need to know how the variables are defined and declared.

Could be. There's not enough info to tell. Need to know how the variables are defined and declared.

the code is on my previous post and my file contains the words hola, banana, go, instructor, dictatorship

when i compile and run, the printout is this:
-----------------------------------------------------------------------
Please enter file's name: (I put) b.txt
Please enter the size of the array (no bigger than 100): (I put) 10
hola, banana, go, instructor, dictatorship
The number of strings inserted in the array are 5

Enter new string to sort in the array, -1 to end: (I put) door
----------------------------------------------------------------------
program terminates

:)

the code is on my previous post and my file contains the words hola, banana, go, instructor, dictatorship

when i compile and run, the printout is this:
-----------------------------------------------------------------------
Please enter file's name: (I put) b.txt
Please enter the size of the array (no bigger than 100): (I put) 10
hola, banana, go, instructor, dictatorship
The number of strings inserted in the array are 5

Enter new string to sort in the array, -1 to end: (I put) door
----------------------------------------------------------------------
program terminates

i can understand your problem as i am also a biggner so,
if your program is working well to some extent please increase your array length because when you add some more data to a small array it will overflow and give you errors;)
you can read "turbo c" it will give you answer to all this type of questions easily.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.