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;
}
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.
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.
@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]'
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;
}
@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.
@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.
// Which State.cpp
#include "stdafx.h"
#include <iostream>
#include <string>
usingnamespace 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";
}
elseif (state[count]=="TX" || state[count] == "tx")
{
state[count] = "Texas";
state_abbr[count] = "TX";
}
elseif (state[count]=="VA" || state[count] == "va")
{
state[count] = "Virginia";
state_abbr[count] = "VA";
}
elseif (state[count]=="NY" || state[count] == "ny")
{
state[count] = "New York";
state_abbr[count] = "NY";
}
elseif (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;
}
}
elseif ( 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.