Why this code doesnt work?

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Nov 2009
Posts: 2
Reputation: lucky_43 is an unknown quantity at this point 
Solved Threads: 0
lucky_43 lucky_43 is offline Offline
Newbie Poster

Why this code doesnt work?

 
0
  #1
32 Days Ago
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
  1. #include <iostream.h>
  2. #include <conio.h>
  3.  
  4. struct m{
  5. char n[15];
  6. };
  7.  
  8.  
  9.  
  10.  
  11. void main()
  12. {
  13. m a[50];
  14. char name[15];
  15. int i,size;
  16. cin>>size;
  17.  
  18.  
  19.  
  20. for(i=0;i<size;i++)
  21. {
  22. cout<<"entr elements";
  23. cin>>(a[i]).n;
  24.  
  25. }
  26. cout<<"enter elment";
  27. cin>>name;
  28.  
  29.  
  30. for (int j=0;j<size;j++)
  31. {
  32. if((a[j]).n==name)
  33. for(int c=j; c<size;c++)
  34. ((a[c]).n)=((a[c+1]).n); //this is The LINE
  35. }
  36. for(i=0;i<size;i++)
  37. cout<<(a[i]).n<<endl;
  38.  
  39. getch();
  40. }
Last edited by lucky_43; 32 Days Ago at 1:26 pm.
Reply With Quote Quick reply to this message  
Join Date: Oct 2005
Posts: 404
Reputation: Clinton Portis is on a distinguished road 
Solved Threads: 40
Clinton Portis's Avatar
Clinton Portis Clinton Portis is online now Online
Posting Pro in Training
 
0
  #2
32 Days Ago
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:
  1. #include<cstring>
  2.  
  3. //instead of this
  4. if((a[j]).n==name)
  5.  
  6. //try this
  7. if(strcmp(a[j].n, name))
Last edited by Clinton Portis; 32 Days Ago at 1:35 pm.
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 2
Reputation: lucky_43 is an unknown quantity at this point 
Solved Threads: 0
lucky_43 lucky_43 is offline Offline
Newbie Poster
 
0
  #3
32 Days Ago
Originally Posted by Clinton Portis View Post
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:
  1. #include<cstring>
  2.  
  3. //instead of this
  4. if((a[j]).n==name)
  5.  
  6. //try this
  7. 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
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 131
Reputation: restrictment is on a distinguished road 
Solved Threads: 9
restrictment's Avatar
restrictment restrictment is offline Offline
Junior Poster
 
0
  #4
32 Days Ago
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2005
Posts: 404
Reputation: Clinton Portis is on a distinguished road 
Solved Threads: 40
Clinton Portis's Avatar
Clinton Portis Clinton Portis is online now Online
Posting Pro in Training
 
0
  #5
32 Days Ago
This is how I would write your block o' code:
  1. #include<cstring>
  2.  
  3. for (int j=0;j<size;j++)
  4. {
  5. if(strcmp(a[j].n, name)
  6. {
  7. strcpy(a[j].n, name);
  8. break;
  9. }
  10. }

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.
Last edited by Clinton Portis; 32 Days Ago at 2:10 pm.
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC