| | |
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:
Solved Threads: 0
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.
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.
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)
#include "stdafx.h" #include <iostream> #include <string> struct StudentRecord { char Name[20]; // (1) int ID; float GPA; }; int main() { StudentRecord MyStudent; MyStudent.Name = "Test Value"; // (2) (3) MyStudent.ID = 1234; MyStudent.GPA = 4.0; std::cin.get(); return 0; }
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.
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:
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.
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <cstring> struct StudentRecord { char Name[20]; // (1) int ID; float GPA; }; int main() { StudentRecord MyStudent; strcpy ( MyStudent.Name, "Test Value" ); // (2) (3) MyStudent.ID = 1234; MyStudent.GPA = 4.0; std::cin.get(); return 0; }
New members chased away this month: 3
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.
(or strncpy for safety when using user input)
strcpy (MyStudent.Name, "Test Value"); (or strncpy for safety when using user input)
![]() |
Other Threads in the C++ Forum
- Previous Thread: C++ Timers
- Next Thread: problem in text files
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete deploy developer display dll dynamiccharacterarray email encryption error file format forms fstream function functions game generator givemetehcodez graph homeworkhelp iamthwee ifstream image input int java lib list loop looping loops map math matrix memory multiple newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg simple sorting spoonfeeding string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






