Computing.Net > Forums > Programming > incompatible types in assignment?

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

incompatible types in assignment?

Reply to Message Icon

Name: tImmaY
Date: February 17, 2005 at 22:37:24 Pacific
OS: Windows XP Home SP2
CPU/Ram: AMD Athlon XP 2400+ / 512
Comment:

hey, i'm getting an error in this C program i'm writing, but i'm not sure as to why. the error that i'm getting is telling me that i'm trying to assign a value incorrectly, such as: 1 = a; but i'm not trying to assign an int, i'm trying to assign a char. i have the variable i want for the char declared global because i want to access it in other functions. so i have something like:
char current_level[20] = "", level = 1, ..;
...
...
if (level == 1)
{
current_level = "beginner";
}
else if (level == 2)
{
current_level = "intermediate";
...
}
...

but Dev-C++ tells me:
error: incompatible types in assignment

and MSVC++ says:
error C2106: '=' : left operand must be l-value

what am i doing wrong?? thanks



Sponsored Link
Ads by Google

Response Number 1
Name: HiJinx
Date: February 17, 2005 at 23:17:09 Pacific
Reply:


char *current_level="", level='1';
...
if (level == '1') //note single quotes
{
current_level = "beginner";
}

or

char *current_level="";
int level=1;

if (level == 1)
{
current_level = "beginner";
}


0

Response Number 2
Name: tImmaY
Date: February 18, 2005 at 10:39:21 Pacific
Reply:

hmm.. i c.
why do the chars have to be pointers? also, its telling me: invalid initializer
for the line that has the declaration of current_level and level on it. any clues as to why?


0

Response Number 3
Name: egkenny
Date: February 18, 2005 at 14:38:50 Pacific
Reply:

In C:

#include <string.h>
int main()
{
char current_level[20] = "";
char level = 2;
if (level == 1)
{
strcpy(current_level,"beginner");
}
else if (level == 2)
{
strcpy(current_level, "intermediate");
}
return 0;
}

In C++:

#include <string>
using namespace std;
int main()
{
string current_level;
char level = 2;
if (level == 1)
{
current_level = "beginner";
}
else if (level == 2)
{
current_level = "intermediate";
}
return 0;
}


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More


doubt in windows prog Ansi c



Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: incompatible types in assignment?

String in C www.computing.net/answers/programming/string-in-c/9450.html

Problem Declaring name array www.computing.net/answers/programming/problem-declaring-name-array/14292.html

Primitive data types in C www.computing.net/answers/programming/primitive-data-types-in-c/7294.html