String Help - const char to char converstion

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Mar 2007
Posts: 30
Reputation: Ψmon is an unknown quantity at this point 
Solved Threads: 0
Ψmon Ψmon is offline Offline
Light Poster

String Help - const char to char converstion

 
0
  #1
Mar 12th, 2009
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.

  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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,740
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 739
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: String Help - const char to char converstion

 
1
  #2
Mar 12th, 2009
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:
  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.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 476
Reputation: nucleon has a spectacular aura about nucleon has a spectacular aura about 
Solved Threads: 91
nucleon's Avatar
nucleon nucleon is offline Offline
Posting Pro in Training

Re: String Help - const char to char converstion

 
0
  #3
Mar 12th, 2009
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)
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 30
Reputation: Ψmon is an unknown quantity at this point 
Solved Threads: 0
Ψmon Ψmon is offline Offline
Light Poster

Re: String Help - const char to char converstion

 
0
  #4
Mar 12th, 2009
Thanks - I really appreciate the help!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC