error C2440: '=' : cannot convert from 'const char [3]' to 'char'

Please help. I'm having erros with the program I'm trying to run for school. I keep getting the "error C2440: '=' : cannot convert from 'const char [3]' to 'char'" It starts during the "Else" and "If" section.

Please help me!!!

#include <iostream>
#include <string>

using namespace std;
int main()
{
int count;
float sum;
float average;
int age[5];
char names[5];
char state[5];
while (count=5)
{
cout << "Enter family member's name" << endl;
cin >> name[count];
cout << "Enter family member's age" << endl;
cin >> age[count];
cout << "Which state are you from? Type in CA, TX, DC?" << endl;
cin >> state[count];
if (state[count]="CA")
{
state[count] = "California";
}
else
{
if (state[count]="TX")
{
state[count] = "Texas";
cout << names[count] << " lives in " << state[count] << endl;
}
else
{
if (state[count]="DC")
{
state[count] = "District of Columbia";
}
else
{
}
}
}
count = count + 1;
}
sum = 0;
average = 0;
sum = age[1] + age[2] + age[3] + age[4] + age[5];
average =sum/5;
cout << "Average age is " << average << " years old." << endl;
return 0;
}
while (count=5)

This is not right, you need the == (comparison) operator (as the condition in the () is a boolean expression which is supposed to evaluate true or false, not assign a value). You also never initialize count to 5 (or any value for that matter) outside of the condition of the while statement. Same for your if statements, you need the == operator.

There are several issues with what you are doing with the array, I'll use this as an example:

state[count] = "California";

What you are doing is checking to see if the character at position count is a string, which is an invalid conversion of char to string. You do the same thing with your if statements, you check a character value against a string and several other assignments as well.

age[5]

This is wrong as well as the positions always start from 0, so the last position in an array of 5 is 4 (0, 1, 2, 3, 4).
@bri135i
You had the logic a little messed up. Remember, a single = means assign, double == checks if equal to. You also had nested if /else. This posted program woks. I added in a for loop at the end, to display the five entered people and their data. By the way, DC is not a state, so I changed it to Florida. Arrays, like age[], starts with zeroes, which is the reason your sum wouldn't work out, since there would be NO age[5]. You also don't have any input checking, which I'm sure you'll put in later in the program.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <iostream>
#include <string>

using namespace std;
int main()
{
	int x, count=0;
	int sum;
	float average;
	int age[5];
	string name[5];
	string state[5];
	while ( count <= 4)
	{
		cout << "Enter family member's name" << endl;
		cin >> name[count];
		cout << "Enter family member's age" << endl;
		cin >> age[count];
		cout << "Which state are you from? Type in CA, TX, FL?" << endl;
		cin >> state[count];
		if ( state[count] == "CA")
		{
			state[count] = "California";
		}
		else if (state[count]=="TX")
		{
			state[count] = "Texas";
		}
		else if (state[count]=="FL")
		{
			state[count] = "Florida";
		}
		else
		{
			cout << "I don't understand!!" << endl;
                        count--;
		}
		count++;
	}
	sum = 0;
	average = 0.0;
	sum = age[0] + age[1] + age[2] + age[3] + age[4];
	average = sum/5;
	cout << "Average age of the five entered ages is " << average << " years old." << endl;
	cout << "The entered data was :" << endl;
	for ( x=0; x < 5;x++)
	{
		cout << name[x] << " lives in " << state[x] << " and is " << age[x] << " years old." << endl;
	}
	return 0;
}
Last edited on
WOW! I that was fast! But I just changed it up a bit without the 5 ages. I maxed it out to 10, but if the user were to only put lets say 7, it will only sum up the 7 ages and find the average of the 7. The user will input "done" when there is no one else to add. I'm getting these to errors:

(16) : error C2446: '==' : no conversion from 'const char *' to 'std::string *'
(16) : error C2440: '==' : cannot convert from 'const char [5]' to 'std::string [5]'

Oh yeah..DC is a state District of Columbia which is where our Capitol Washington resides.

#include "stdafx.h"
#include <iostream>
#include <string>

using namespace std;
int main()
{
int x, count=0;
float sum;
float average;
float age[10];
string name[5];
string state[5];
cout << "Enter the family member's name. Type 'done' when complete." << endl;
cin >> name[count];
while (name=="done")
{
cout << "Enter family member's age" << endl;
cin >> age[count];
cout << "Which state are you from? Type in CA, TX, FL?" << endl;
cin >> state[count];
if ( state[count] == "CA")
{
state[count] = "California";
}
else if (state[count]=="TX")
{
state[count] = "Texas";
cout << name[x] << " lives in " << state[x];
}
else if (state[count]=="DC")
{
state[count] = "Capitol";
}
else
{
cout << "I don't understand!!" << endl;
count--;
}
count++;
}
sum = 0;
average = 0.0;
sum = age[0] + age[1] + age[2] + age[3] + age[4] + age[5] + age[6] + age[7] + age[8] + age[9] + age[10];
average = sum/(count+1);
cout << "Average age of the five entered ages is " << average << " years old." << endl;
cout << "The entered data was :" << endl;
for ( x=0; x < 10;x++)
{
cout << name[x] << " lives in " << state[x] << " and is " << age[x] << " years old." << endl;
}
return 0;
}
@bri135i
Okay, so now you have array age[10], but you're only asking for five names, ages and cities. How were you expecting to get the other five ages? Also it wouldn't be while( name=="done"), it would have to be while(name!="DONE"). Since you wanting the inputs to stop once the name entered, is "DONE", while it isn't, you do want it to continue or until five names are entered.
Sorry to burst your bubble, but DC is NOT a state. It is a District. There are only 50 states, and DC isn't one of them.
okay you're right. This whole time i thought it was a state. I feel dumb now, but thanks for the education.

This is what i'm trying to do with the program: Design a program that will allow a user to Input a list of your family members along with their age and state where they reside. Determine and print the average age of your family and print the names of anyone who live in Texas.

I changed the states and names to have [10] and made the change and i'm getting this error on the "(name!="DONE")" line. Is this because I changed it from [5] to [10]? But why did it only do it for the variable name and not the others? Also, will this find the average age if i only add lets say 3 people instead of 10? Or do I have to put at least 10 people to end it?

(23) : error C2446: '!=' : no conversion from 'const char *' to 'std::string *'
(23) : error C2440: '!=' : cannot convert from 'const char [5]' to 'std::string [10]'


#include "stdafx.h"
#include <iostream>
#include <string>

using namespace std;
int main()
{
int x, count=0;
int sum;
float average;
int age[10];
string name[10];
string state[10];
cout << "Enter the family member's name. Type 'done' when complete." << endl;
cin >> name[count];
while ( name != "DONE")
{
cout << "Enter family member's age" << endl;
cin >> age[count];
cout << "Which state are you from? Type in CA, TX, VA?" << endl;
cin >> state[count];
if ( state[count] == "CA")
{
state[count] = "California";
}
else if (state[count]=="TX")
{
state[count] = "Texas";
cout << name[x] << " lives in " << state[x];
}
else if (state[count]=="VA")
{
state[count] = "Virginia";
}
else
{
cout << "I don't understand!!" << endl;
count--;
}
count++;
}
sum = 0;
average = 0.0;
sum = age[0] + age[1] + age[2] + age[3] + age[4] + age[5] + age[6] + age[7] + age[8] + age[9] + age[10];
average = sum/count;
cout << "The average age of your family is " << average << " years old." << endl;
cout << "The entered data was :" << endl;
for ( x=0; x < 10 ;x++)
{
cout << name[x] << " lives in " << state[x] << " and is " << age[x] << " years old." << endl;
}
return 0;
}
Last edited on
@bri135i
You were having the error, because you weren't checking the array of name. You also didn't have the input of name[count] located inside the do/while check, so only the age and state arrays would be added after the first time through. I have the program checking for 'DONE" or if count is 10, and if the state abbreviation is in upper or lower case. The age average is in a for loop that counts from 0 to count.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// Which State.cpp
#include "stdafx.h"
#include <iostream>
#include <string>

using namespace std;
int main()
{
	int x, count=0;
	float sum;
	float average;
	int age[10];
	string name[10];
	string state[10];
	bool okay = true;

	do
	{
		cout << "Enter the family member's name. Type 'done' when complete." << endl;
		cin >> name[count];
		if ( name[count] == "DONE" || name[count]=="done")
		{
			okay = false;
			break;
		}

		cout << "Enter family member's age" << endl;
		cin >> age[count];

		cout << "Which state are you from? Type in CA, TX, VA, NY, VT?" << endl;
		cin >> state[count];
		if ( state[count] == "CA" ||  state[count] == "ca")
		{
			state[count] = "California";
		}
		else if (state[count]=="TX" ||  state[count] == "tx")
		{
			state[count] = "Texas";
		}
		else if (state[count]=="VA" ||  state[count] == "va")
		{
			state[count] = "Virginia";
		}

		else if (state[count]=="NY" ||  state[count] == "ny")
		{
			state[count] = "New York";
		}
		else if (state[count]=="VT" ||  state[count] == "vt")
		{
			state[count] = "Vermont";
		}
		else
		{
			cout << "I don't understand!!" << endl;
			count--;
		}
		count++;
	} while (okay || count != 10);

	sum = 0.0;
	average = 0.0;
	for ( x=0;x<count;x++)
	{
		sum = sum + age[x];
	}
	average = sum/count;
	cout << endl << endl << "The average age of your family is " << average << " years old." << endl << endl;
	cout << "The entered data was :" << endl << endl;
	for ( x=0; x < count ;x++)
	{
		cout << name[x] << " lives in " << state[x] << " and is " << age[x] << " years old." << endl;
	}
	return 0;
} 
@whitenite1
WOW! It works! Thank you. One last question...how can I get the output for who ever lives in Texas? For example, 2 out of the 4 people I added lives in Texas, and I want that to show up before the average age is printed out.
Last edited on
@bri135i
I'm not sure if this is exactly what you wanted, but here it is.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
// Which State.cpp
#include "stdafx.h"
#include <iostream>
#include <string>

using namespace std;
int main()
{
	int x = 0;
	int found = 0;
	int count=0;
	float sum;
	float average;
	int age[10];
	string name[10];
	string Find_State = "";
	string state[10];
	string state_abbr[10];
	bool okay = true;

	do
	{
		cout << "Enter the family member's name. Type 'done' when complete." << endl;
		cin >> name[count];
		if ( name[count] == "DONE" || name[count]=="done")
		{
			okay = false;
			break;
		}

		cout << "Enter family member's age" << endl;
		cin >> age[count];

		cout << "Which state is " << name[count] << " from? Type in CA, NY, TX, VA, VT?" << endl;
		cin >> state[count];
		if ( state[count] == "CA" ||  state[count] == "ca")
		{
			state[count] = "California";
			state_abbr[count] = "CA";
		}
		else if (state[count]=="TX" ||  state[count] == "tx")
		{
			state[count] = "Texas";
			state_abbr[count] = "TX";
		}
		else if (state[count]=="VA" ||  state[count] == "va")
		{
			state[count] = "Virginia";
			state_abbr[count] = "VA";
		}

		else if (state[count]=="NY" ||  state[count] == "ny")
		{
			state[count] = "New York";
			state_abbr[count] = "NY";
		}
		else if (state[count]=="VT" ||  state[count] == "vt")
		{
			state[count] = "Vermont";
			state_abbr[count] = "VT";
		}
		else
		{
			cout << "I don't understand!!" << endl;
			count--;
		}
		count++;
	} while (okay || count != 10);
char yn=' ';
	do
	{
		cout << "Do you wish to list people living in a certain state?" << endl;
		cin >> yn;
		if ( yn == 'Y')
		{
			cout << "Enter the state abbreviation : ";
			cin >> Find_State;
			for (x=0;x<count;x++)
				if (state_abbr[x] == Find_State)
				{
					cout << name[x] << " lives in " << state[x] << endl;
					found++;
				}
			if (!found)
				{
					cout << "Couldn't find anyone living in the state of " << Find_State << "." << endl;
				}
		}
		else if ( yn == 'N')
			cout << "Okay, we'll continue on!!" << endl << endl;
		break;
	} while (yn != 'Y' || yn != 'N');
	
	sum = 0.0;
	average = 0.0;
	for ( x=0;x<count;x++)
	{
		sum = sum + age[x];
	}
	average = sum/count;
	cout << endl << endl << "The average age of your family is " << average << " years old." << endl << endl;
	cout << "The entered data was :" << endl << endl;
	for ( x=0; x < count ;x++)
	{
		cout << name[x] << " lives in " << state[x] << " and is " << age[x] << " years old." << endl;
	}
	return 0;
}

I used another array, state_abbr[10], to hold the abbreviations of the entered state. A for loop checks for the abbreviation, and if found, prints the persons name and the full state, but not the ages. Then the program will list all the names, etc, at the end. I wrote the section so you can specify which state you're looking for, instead of hard-coding it in. You may want to search for other states as well, later.
Last edited on
This is PERFECT!!! Thank you so much!!!
Nice program bri135i. I even learned a lot by helping you, which, to me, is the main reason I help.
What is the psuedocode for this program may I ask...
Topic archived. No new replies allowed.