| | |
Why this code doesnt work?
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Nov 2009
Posts: 2
Reputation:
Solved Threads: 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
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)
#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(); }
Last edited by lucky_43; Nov 7th, 2009 at 1:26 pm.
0
#2 Nov 7th, 2009
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:
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)
#include<cstring> //instead of this if((a[j]).n==name) //try this if(strcmp(a[j].n, name))
Last edited by Clinton Portis; Nov 7th, 2009 at 1:35 pm.
•
•
Join Date: Nov 2009
Posts: 2
Reputation:
Solved Threads: 0
0
#3 Nov 7th, 2009
•
•
•
•
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)
#include<cstring> //instead of this if((a[j]).n==name) //try this if(strcmp(a[j].n, name))

I used your idea
even though it is right but it is not enough
the compiler still giving me An "Lvalue Required" Error
0
#5 Nov 7th, 2009
This is how I would write your block o' code:
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.
C++ Syntax (Toggle Plain Text)
#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.
Last edited by Clinton Portis; Nov 7th, 2009 at 2:10 pm.
![]() |
Similar Threads
- LAST_INSERT_ID doesnt work with servlets, please help (JSP)
- Pascal code doesnt work and i work out why (Pascal and Delphi)
- Setting a width to a photo displayed from MySQL doesnt work (PHP)
- Internet explorer pop up blocker doesnt work (Web Browsers)
- python mysql doesnt work?plz help (Python)
- DVD-RAM doesnt work!!!!!HELP!!!!! (Storage)
- first website doesnt work well? (HTML and CSS)
- Creative webcam doesnt work!!! HELP!!!!! (USB Devices and other Peripherals)
- My phpbb forum doesnt work ? (PHP)
Other Threads in the C++ Forum
- Previous Thread: Locking software till they fill out a webpage form?
- Next Thread: Grabbing a reference to or pointer to...
Views: 206 | Replies: 4
| Thread Tools | Search this Thread |
Tag cloud for C++
6 add api array arrays beginner binary c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete desktop directshow dll dynamic encryption error file forms fstream function functions game getline givemetehcodez google graph homeworkhelper iamthwee ifstream input int integer java lazy lib linkedlist linker linux loop looping loops map math matrix memory microsoft newbie news number output parameter pointer problem program programming project proxy python random read recursion recursive reference return sort stream string strings struct studio system template templates test text tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets





