943,568 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 1242
  • C++ RSS
Mar 12th, 2009
0

String Help - const char to char converstion

Expand Post »
Hello all - I need homework help.

This part of the assignment is to create a struct, called StudentRecord, the first attribute of which needs to be size 20. My implementation of this is at (1).

After instantiating a StudentRecord named MyStudent, I'm to assign some value to it. I chose "Test Value" and my implementation is located at (2).

The problem is I continue getting this error:
error C2440: '=' : cannot convert from 'const char [20]' to 'char [20]
followed by:
There is no context in which this conversion is possible

It shows up right when I attempt to assign the char [20] a value.

C++ Syntax (Toggle Plain Text)
  1. #include "stdafx.h"
  2. #include <iostream>
  3. #include <string>
  4.  
  5. struct StudentRecord
  6. {
  7. char Name[20]; // (1)
  8. int ID;
  9. float GPA;
  10. };
  11.  
  12. int main()
  13. {
  14. StudentRecord MyStudent;
  15. MyStudent.Name = "Test Value"; // (2) (3)
  16. MyStudent.ID = 1234;
  17. MyStudent.GPA = 4.0;
  18. std::cin.get();
  19. return 0;
  20. }

I've spent the last few days digging online for help - and have found no leads that make sense to me. I am assuming that I'm either asking the wrong questions, querying the wrong keywords, or don't fully understand the error.

Thus I ask you for help!

Please help me understand what the error message is trying to tell me.

Thank you for your time.
Reputation Points: 12
Solved Threads: 0
Light Poster
Ψmon is offline Offline
30 posts
since Mar 2007
Mar 12th, 2009
1

Re: String Help - const char to char converstion

You can't assign to arrays like that. In this case you're pretty much stuck with copying the contents of the string literal to the array manually or with a function like strcpy that does it manually:
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <cstring>
  3.  
  4. struct StudentRecord
  5. {
  6. char Name[20]; // (1)
  7. int ID;
  8. float GPA;
  9. };
  10.  
  11. int main()
  12. {
  13. StudentRecord MyStudent;
  14. strcpy ( MyStudent.Name, "Test Value" ); // (2) (3)
  15. MyStudent.ID = 1234;
  16. MyStudent.GPA = 4.0;
  17. std::cin.get();
  18. return 0;
  19. }
Note that I changed the <string> header to <cstring>. <string contains the std::string object and helpers while <cstring> contains the string handling functions (including strcpy) and types inherited from C.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Mar 12th, 2009
0

Re: String Help - const char to char converstion

MyStudent.Name is the name of a storage location capable of holding up to 20 characters (including the terminating null character). You cannot simply assign a string constant to MyStudent.Name since that just tries to copy the address of the string constant to MyStudent.Name. What you need to do is copy the characters from the string constant into the space provided in MyStudent.Name.
strcpy (MyStudent.Name, "Test Value");
(or strncpy for safety when using user input)
Reputation Points: 163
Solved Threads: 91
Posting Pro in Training
nucleon is offline Offline
476 posts
since Oct 2008
Mar 12th, 2009
0

Re: String Help - const char to char converstion

Thanks - I really appreciate the help!
Reputation Points: 12
Solved Threads: 0
Light Poster
Ψmon is offline Offline
30 posts
since Mar 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

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: C++ Timers
Next Thread in C++ Forum Timeline: problem in text files





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


Follow us on Twitter


© 2011 DaniWeb® LLC