Pointer, getline and strcmp

I'm trying to write a program that takes text as
input and copies it to its output, minus any lines
that are  the same as the line they follow
Example:     if the input consists of the lines:    C    is    great    great    program    program     the output should be:    C    is    great     program       Should also use high level functions, main(),  
getline(),strcmp(). Also must use pointer in  main
and all the functions mentioned above. The  
program must accept 50 lines and then perform the  
edit. I really appreciate all the help I can get.
Thank you. i wrote the following code, but it's
not working:   #include <string.h> #include <stdio.h> void main()  { {  char text [67], text1 [67] ;  printf ("Please enter a text\n\n"); {        int i;  for (i = 1; i<= 50; ++i)  gets (text);  puts (text);  strcmp (text, text);  if (text == text)  printf ("\nYour text is:\n", text ); } } }  I also wrote this code, but did not work either  #include <stdio.h>   #define MAX_LINES 50 #define MAX_LEN 100   int main ( )  {    char lines[MAX_LINES][MAX_LEN];    int num_lines = 0;    while ( fgets(lines[num_lines],MAX_LEN,stdin) && 
++num_lines < MAX_LINES );     return 0;  }  
kkanealAsked:
Who is Participating?
 
nietodConnect With a Mentor Commented:
I see two main problems in the first code example.

in the if()

if (text == text)  

You are comparing "text" to itself (well, you are TRYING to compare text to itself, but you are doing that wrong, more on that later).  This maks no sense.  Text will always equal itself, right?  So you want to compare text with something else.  to be specific, the previous line that was read.

To do this, when you read a line, you will compare it to a saved copy of the last line.  If the two are different, then you will print this new line.  If the two are the same, then you won't print this new line.  Then you will save this new line so that when the next line is read that new line will be compared with this one.

Does that make sense?

Also when you compare two strings, you cannot use "=".  you must use the strcmp() function.

Try that and then post what you get done.
0
 
nietodCommented:
We cannot provide answers to school assignments.  That is grounds for removal from this site.  (for both you and the experts involved.)  We can provide only limitied help in accademic assignments.    We can answer specific (direct) questions, like you might ask your teacher.  We can review your work and post suggestions, again, like your teacher might do.

Do you have specific questions?
Do you have any work on this (incomplete even) that we can review?
0
 
kkanealAuthor Commented:
I wrote the follwoing two codes, but none works like it suppose to.

#include <string.h>
#include <stdio.h>
void main()  
{
{  
char text [67], text1 [67] ;  
printf ("Please enter a text\n\n");
{  
int i;  
for (i = 1; i<= 50; ++i)  
gets (text);  
puts (text);  
strcmp (text, text);  
if (text == text)  
printf ("\nYour text is:\n", text );
 }
}
}  

I also wrote this code, but did not work either  
#include <stdio.h>  
#define MAX_LINES 50
#define MAX_LEN 100  
int main ( )  
{    
char lines[MAX_LINES][MAX_LEN];    
int num_lines = 0;    
while
( fgetslinesnum_lines],MAX_LEN,stdin) && 
++num_lines < MAX_LINES );    
 return 0;  
}  
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
Ernest022699Commented:
nietod is right (as usual!), but here's a little more information:

Strings are stored as arrays of characters.  For example, I'll use your variable name with a little modificatiopn:
  char szText1[67];
  char szText2[67];

Using just the name of the variable (such as szText1) is essentially using a pointer to the (first character in the) string.  szText1 is equivalent to &szText1[0].  Because the address of szText1[0] will always be different from szText2[0], szText1 will always be different from szText2 even if the contents of the strings are identical.

The function strcmp() (and its relatives) actually compares the contents of two arrays.

Does this help?
0
 
nietodCommented:
Beginners often work with strings long long before they work with pointers.  If that is the case here, the explanation will not be of any value.  This is one of the things that make C so hard to learn.  But not C++ because you can use a string class from the start, regratably it isn't usually taught that way.
0
 
Ernest022699Commented:
Ah, nietod, I hadn't remembered that.

When I taught C, I used to say "Pointers are the soul of C.", often on the first night of class.  I found that teaching the basic concept of pointers fairly straightforward.  Most students could handle the concept of "mailboxes" and "letters" or things being stored in memory.  Later in the course the students were prepared to learn how things really worked.

Even though this question is in the C++ area, the questioner clearly us using C.
0
 
kkanealAuthor Commented:
I finally was able to get it, the problem is I have to use getline( ), but whenever I use it, I got a compiler error that getline( ) is undeclared identifier.

#include <stdio.h>  
#include <string.h>
#define MAX_LINES 5  
#define MAX_LEN 100

int main ( )  
{    
char lines[MAX_LINES][MAX_LEN];  
int nlines = 0;  
int i;  
char *prevline = NULL;          
while
 fgets(lines[nlines],MAX_LEN,stdin) && ++nlines < MAX_LINES );          
for ( i = 0 ; i < nlines ; i++ )
{    
if ( prevline != NULL && strcmp(prevline,lines[i]) == 0 )
{      
continue;    
}
else
{      
printf("%s",lines[i]);      
prevline = lines[i];    
}  
}    
return 0;
}

0
 
nietodCommented:
>> I got a compiler error that getline( )
>> is undeclared identifier
It would help if you posted the code, but if you tried the use the global getline() function it is declared in the "std" namespace, so you need to specify "std::getline()" or use the std namespace.

Your code looks like it shoudl work, but it has to save every single line in the file.  This is a waste of memory.  a better idea would be to save only a copy of the last line you read in a character array (string).  Then each time you read a line, you compare it with the previous line, then when you are done with that line, you copy it to the character array that stores the previous line, then repeat the loop.  Make sense?
0
All Courses

From novice to tech pro — start learning today.