944,134 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 412
  • C++ RSS
Nov 7th, 2009
0

Why this code doesnt work?

Expand Post »
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
C++ Syntax (Toggle Plain Text)
  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; Nov 7th, 2009 at 1:26 pm.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
lucky_43 is offline Offline
7 posts
since Nov 2009
Nov 7th, 2009
0
Re: Why this code doesnt work?
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:
C++ Syntax (Toggle Plain Text)
  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; Nov 7th, 2009 at 1:35 pm.
Reputation Points: 237
Solved Threads: 117
Practically a Posting Shark
Clinton Portis is offline Offline
822 posts
since Oct 2005
Nov 7th, 2009
0
Re: Why this code doesnt work?
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:
C++ Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
lucky_43 is offline Offline
7 posts
since Nov 2009
Nov 7th, 2009
0
Re: Why this code doesnt work?
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.
Reputation Points: 102
Solved Threads: 17
Posting Whiz in Training
restrictment is offline Offline
228 posts
since Oct 2009
Nov 7th, 2009
0
Re: Why this code doesnt work?
This is how I would write your block o' code:
C++ Syntax (Toggle Plain Text)
  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; Nov 7th, 2009 at 2:10 pm.
Reputation Points: 237
Solved Threads: 117
Practically a Posting Shark
Clinton Portis is offline Offline
822 posts
since Oct 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Locking software till they fill out a webpage form?
Next Thread in C++ Forum Timeline: Grabbing a reference to or pointer to...





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC