0

Hello..
Ive written this code which dont work, its jobis simply recieving names from the user in a structure [a] and delete a name that chosen by user, the compiler give me error flag in the line noticed bellow:)

#include <iostream.h>
#include <conio.h>

struct m{
       char n[15];       
       };
       

        

void main()          
{
   m a[50];             
    char name[15];            
     int i,size;
    cin>>size;       
    
   
    
    for(i=0;i<size;i++)
    {
          cout<<"entr elements";
    cin>>(a[i]).n;

}
cout<<"enter elment";
    cin>>name;
    
    
    for (int j=0;j<size;j++)
    {
    if((a[j]).n==name)
    for(int c=j; c<size;c++)
    ((a[c]).n)=((a[c+1]).n);            //this is The LINE
}
    for(i=0;i<size;i++)
    cout<<(a[i]).n<<endl;
    
getch();
}

Edited by lucky_43: n/a

3
Contributors
4
Replies
5
Views
7 Years
Discussion Span
Last Post by Clinton Portis
0

Since you are using c-strings (character arrays) you do not get the luxury of using the <string> class overloaded == equality test.

But what you do get is a bunch of <cstring> library functions that will allow you to handle your character arrays with great ease:

#include<cstring>

//instead of this
if((a[j]).n==name)

//try this
if(strcmp(a[j].n, name))

Edited by Clinton Portis: n/a

0

Since you are using c-strings (character arrays) you do not get the luxury of using the <string> class overloaded == equality test.

But what you do get is a bunch of <cstring> library functions that will allow you to handle your character arrays with great ease:

#include<cstring>

//instead of this
if((a[j]).n==name)

//try this
if(strcmp(a[j].n, name))

Thank You Very much:)
I used your idea
even though it is right but it is not enough
the compiler still giving me An "Lvalue Required" Error

0

It simply means that the left value must be a single variable. You cannot have an equation equal to an equation. Try revising it a bit, and see if you can get it to work.

0

This is how I would write your block o' code:

#include<cstring>

for (int j=0;j<size;j++)
{
     if(strcmp(a[j].n, name)
    {
          strcpy(a[j].n, name);
          break;
    }
}

The code I provided you is untested. Please let me know where any additional errors are with line number.

Some rules to remember:

you can use all boolean logic when at the 'char' level (comparing 'chars' to 'chars') you can also assign chars to other chars.

You cannot directly use boolean logic to directly compare c-strings to other c-strings (arrays to arrays)

You can assign an array pointer to an array pointer of the appropriate type (assign an array to an array pointer) if the appropriate amount of memory has been allocated.

Only if you are using <string> class objects can you compare to 'string' type variables using == equality test, or assign one string to another using the = assignment operator, or 'concantinate' (add to) an existing string using the += 'accumulation' operator.

Edited by Clinton Portis: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.