Click here to Skip to main content
15,890,186 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
when i debug this prg, there is an error that: erroec2440: '=' cannot convert from 'const char[10] to 'char[10]'. what should i do?

C#
class student
{public:

    char name[10];
    int code;
    float grade;



};
int main()
{
    
    char subject[5];
    char subject1[5]="srch";
    char subject2[5]="info";
    char name[20];
    student stu[3];
    stu[1].name="ali souri";
    stu[2].name="moh jdhfu";
    stu[3].name="far nfjgi";
    



    
    cout<<"HELP:"<<endl;
    cout<<"if u want search, enter srch & if u want enter information, enter info."<<endl;
    cout<<"thx."<<endl;
    gets(subject);
    
    if(strcmp(subject,subject1)==0)
    {

    cout<<"enter ur name:";
    gets(name);

    for(int i=0 ; i<3 ; i++)
    {
    if(strcmp(name,stu[i])==0)
    {
        cout<<"that's ok"<<endl;
    }
    else
    {
        cout<<"they are not match"<<endl;
        cout<<"do u want retry?";
    }
    }
    }
    else
        cout<<"bye";
    
    getchar();
    getchar();
    return 0;
}
Posted
Comments
Sergey Alexandrovich Kryukov 18-Apr-12 0:33am    
Please, next time always show the line(s) of the code where a compilation error was detected.
--SA
eli551 19-Apr-12 5:08am    
ok. sure.
Maximilien 18-Apr-12 15:12pm    
Since your are coding in c++, you should use std::string instead of char arrays.

safer and cleaner.

It depends on what you want to do, but what you are trying to do is not good. Instead, you could do — for example — this:
C++
#include <cstring>

//...

    strcpy(stu[1].name, "ali souri");
    strcpy(stu[2].name, "moh jdhfu");
    strcpy(stu[3].name, "far nfjgi");

In this case, you would copy character data, but not the pointers, which would be really bad in this case.

Here is why: the strings on the right of assignment operator are immediate constants, the string data could be stored right in the code (and usually stored this way), and the code is read-only, protected from modification on the CPU level. An attempt to write in this area would produce a general protection fault (in Intel CPUs). An assignment of a constant pointer to a non-constant pointer would open the write access to the read-only area in the memory, so you could, say, assign a different value to any element of the array sty[1].name, with the fatal result.

Yes, C++ has the constant cast operator (please see http://en.cppreference.com/w/cpp/language/const_cast[^]), but this is not what you would want to do, by the serious reason I've explained above.

[EDIT]

By the way, is there a specific reason to use null-terminated strings, not std::string? It would be safe, reliable, easy, more in the spirit of C++…

—SA
 
Share this answer
 
v4
Comments
CPallini 18-Apr-12 8:50am    
My 5.
Sergey Alexandrovich Kryukov 18-Apr-12 11:06am    
Thank you.
--SA
Nelek 18-Apr-12 16:54pm    
Mine too
Sergey Alexandrovich Kryukov 18-Apr-12 16:59pm    
Thank you, Nelek.
--SA
ProEnggSoft 18-Apr-12 22:51pm    
5+
In addition to SA's exhortation to use std::string I'd like to add a few things...

If you're learning how to program in C++ and you're writing code like that then you're not doing it very effectively. Demand your course fees back off the university, burn the textbook you're using or never go to the website you're learning off - they're doing you no favours.

If you're a programmer in another language, go and buy a copy of "Accelerated C++" by Kernig and Moo. If this is your first language go and buy a copy of "Programming -- Principles and Practice Using C++" by Stroustrup. Both are excellent texts for self study and won't sell you short by teaching you the rubbish habits you've picked up.

Cheers,

Ash

PS: This isn't a solution, just a rant about crap C++ teaching
 
Share this answer
 
Comments
Niklas L 19-Apr-12 4:15am    
Love your rants ;) Welcome back!
you can initialize an array like
char subject1[5]="srch";
but after that you cannot use such notations for copying strings.
that is subject1 = "oops" , will give you compiler error

you should use strcpy (or strcpy_s for safer version )

jkchan
http://cgmath.blogspot.com[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900