Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

strcpy/strcmp problems 1

Status
Not open for further replies.

Dena

Programmer
Apr 18, 2000
6
US
Hi - it's the student again!<br>Here's my code & the errors that I keep getting.&nbsp;&nbsp;I've tried several different fixes and consulted numerous texts.&nbsp;&nbsp;Please explain why this is not working and how it can be fixed... thank you!<br><br>struct structure&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>{<br> char FName[19];<br> char LName[19];<br> char Address[51];<br>};<br><br>structure entry[50];&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>structure tempname;<br><br><br>void Alphabetize(int structcount, structure &entry)<br>{<br><br> <br> for (int count=0;count&lt;(structcount-1);count++)<br> {<br> if (strcmp(entry.LName[count], entry.LName[count+1]) &lt; 0)<br> {<br> strcpy(tempname, entry.LName[count]);<br> strcpy(entry.LName[count], entry.LName[count+1]);<br> strcpy(entry.LName[count+1], tempname);<br><br> }<br> }<br>}<br><br><br>'strcmp' : cannot convert parameter 1 from 'char' to 'const char *'<br>Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast<br><br>'strcpy' : cannot convert parameter 1 from 'struct structure' to 'char *'<br>&nbsp;No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called<br><br>'strcpy' : cannot convert parameter 1 from 'char' to 'char *'<br>Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast<br>
 
strcmp assumes you are passing an array of characters, not a single character. if You want to compare two single characters if 'a' = 'a' would do fine, but it appears you are trying to check if its smaller or whatnot, so try using an amperstand & , which means Address of. because it is expecting a pointer to a string (which is the only way to get entry into a multicharacter string)<br><br>also Temperturename is not a string, its a struct as you declared it as<br><br>if (strcmp(&entry.LName[count], &entry.LName[count+1]) &lt; 0)<br>{<br>//next line isnt a string, fix it or change its type<br>strcpy(tempname, entry.LName[count]);<br><br>strcpy(&entry.LName[count], entry.LName[count+1]);<br>strcpy(&entry.LName[count+1], tempname);<br><br>}<br><br>I dont know how you plan to use tempname, but you cant make a structure into a string <p>Karl<br><a href=mailto:kb244@kb244.8m.com>kb244@kb244.8m.com</a><br><a href= </a><br>Experienced in , or have messed with : VC++, Borland C++ Builder, VJ++6(starting),VB-Dos, VB1 thru VB6, Delphi 3 pro, Borland C++ 3(DOS), Borland C++ 4.5, HTML, ASP(somewhat), QBasic(least i didnt start with COBOL)
 
change <br>entry.LName[count] to entry[count].LName<br><br>This way you will be comparing the names in the array of entry, you simply have the [count] and [count + 1] in the wrong place<br><br>Hope this helps<br>Tom
 
but wouldnt that still identify the parameters as a single character rather than an array of strings. <p>Karl<br><a href=mailto:kb244@kb244.8m.com>kb244@kb244.8m.com</a><br><a href= </a><br>Experienced in , or have messed with : VC++, Borland C++ Builder, VJ++6(starting),VB-Dos, VB1 thru VB6, Delphi 3 pro, Borland C++ 3(DOS), Borland C++ 4.5, HTML, ASP(somewhat), QBasic(least i didnt start with COBOL)
 
Ok, I've moved the [count] to the right location and now I get a totally new set of errors. Two for each strcmp/strcpy in the program.&nbsp;&nbsp;I discussed it with a grad student at school who said that I needed to use the new command.&nbsp;&nbsp;Now if I declare a structure, aren't I asking for memory?&nbsp;&nbsp;Would I be repeating myself if I did:<br><br>structure entry[50];&nbsp;&nbsp;&nbsp;// declaring type 'structure' with variable 'entry' as an array of<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;50 elements.<br><br>entry = new structure[50];&nbsp;&nbsp;// allocating memory for a variable 'entry' of 50<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;elements of the type 'structure' <br><br>The following statement is just one of many strcmp/strcpy commands that all give me the same following error:<br><br>if (strcmp(entry[count].FName, &quot;q&quot;) == 0)&nbsp;&nbsp;<br> <br><br>error C2676: binary '[' : 'struct structure' does not define this operator or a conversion to a type acceptable to the predefined operator<br>error C2228: left of '.FName' must have class/struct/union type<br><br>
 
A structure does not allocate memory, but any new objects based on that structure does. For example the declaration of a structure or a class, only defines its skeleton, what it looks like, its a snapshot, then you declare an object.<br><br>the first one where you just did <br>StructureName StructObject[50];<br>declares an array of structure objects, or I like to call them nodes sometimes.<br>the next line, if you declared entry as<br>StructureName *entry;<br>then entry = new structurename[50];<br><br>would point the pointer to the new object in memory which is an array of 50 struct types.<br><br>for example<br><br>struct MyStruct<br>{<br>int Age;<br>};<br><br>MyStruct.Age = 12 //CANT DO THIS , MyStruct doesnt really exist it only exist as a Type which defines how the 'MyStruct' is setup<br>If you want to use this , declare an object<br><br>MyStruct MyObj;<br><br>MyObj.Age = 12; //Now this will work<br><br>or you can declare the object(or objects) right after the declaration<br><br>struct MyStruct<br>{<br>int Age;<br>}myObj[50];<br>(multiples can be }MyObj[50],*entry,SimpleObj; )<br><br>Just making sure you understood how a structure is setup.<br>in order to use the new operator the object you are assigning it to must be a pointer, and whenever you use pointers you cant use . to goto member functions(unless you dereference the type first) but instead using -&gt; for example<br>entry-&gt;FName;<br> <p>Karl<br><a href=mailto:kb244@kb244.8m.com>kb244@kb244.8m.com</a><br><a href= </a><br>Experienced in , or have messed with : VC++, Borland C++ Builder, VJ++6(starting),VB-Dos, VB1 thru VB6, Delphi 3 pro, Borland C++ 3(DOS), Borland C++ 4.5, HTML, ASP(somewhat), QBasic(least i didnt start with COBOL)
 
You do not need the new operator, you already declared an array of type entry with structure entry[50];<br>And since it is not a pointer you do not need new.<br><br>if(strcmp(entry[index].LName,&quot;q&quot;)==0)<br>should be true if LName = &quot;q&quot;.<br>if you are looking for the first letter if LName you would <br>use <br>if(entry[index].LName[0]== 'q')// this would be true if LName[0] was 'q'.<br>If this doesnt help, send me the new code and I will look it over,<br><br>Tom.<br><A HREF="mailto:TMclaug2@optonline.net">TMclaug2@optonline.net</A><br>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top