Ok So I need help with the very last part of my code. Basically what the overall code is supposed to give an average age of the family members then display which family members live in Texas. Right now I can only get the last Family member that I input to reside in Texas to print out, instead of all the ones that I input to live in Texas. Can someone help me. Thanks!!
// Final Project
//Developed by Alex Macias
//CMIS 102
#include <iostream>
#include <iomanip>
#include <string> //use when using a string variable
usingnamespace std;
//********************************************************************
//This is the secondary function
//It will display the greeting for the program
//********************************************************************
void displayMessage()
{
cout << "This program will help give you the average age of your family members \n";
}
//********************************************************************
//This is the main function
//********************************************************************
int main ()
{
//declare all my variables
string relNameArray[50];
string stateArray[50];
int totalRel, relAge, sumOfAges;
double avgAges; // this will get the average age
string Name; //declare variable for name input
string State; //declare variable for state or residence
bool residentFound = false;
displayMessage(); //calls secondary function
cout << "Enter the number of family members you have: ";
cin >> totalRel; //input to declare number of family members
sumOfAges = 0;
for (int i = 0; i < totalRel; i++)
{
cout << "Enter name of family member " << (i +1 ) << ":> ";
cin >> Name; //input Name of family member
relNameArray[i] = Name;
cout << "Enter the age of " << Name << ":> ";
cin >> relAge; //input age of family member
sumOfAges = sumOfAges + relAge; //formula to add total of all ages
cout << "Enter the state where " << Name << " resides in" << ":> ";
cin >> State; //Input state of residence
stateArray[i] = State;
cout << "------------------------------------------------------ " << endl;
}
avgAges = sumOfAges/totalRel; //formula to find average age
cout << "The average age of you family members is:" << avgAges << endl;
//statement to declare which family members are from Texas
if (State == "Texas" || State == "texas"){
cout << "The following family members are from Texas: " << Name << endl;
}
return 0;
}
// Final Project
//Developed by Alex Macias
//CMIS 102
#include <iostream>
#include <iomanip>
#include <string> //use when using a string variable
usingnamespace std;
//********************************************************************
//This is the secondary function
//It will display the greeting for the program
//********************************************************************
void displayMessage()
{
cout << "This program will help give you the average age of your family members \n";
}
//********************************************************************
//This is the main function
//********************************************************************
int main ()
{
//declare all my variables
string relNameArray[50];
string stateArray[50];
int totalRel, relAge, sumOfAges = 0;
double avgAges; // this will get the average age
string Name; //declare variable for name input
string State; //declare variable for state or residence
bool residentFound = false;
displayMessage(); //calls secondary function
cout << "Enter the number of family members you have: ";
cin >> totalRel; //input to declare number of family members
sumOfAges = 0;
for (int i = 0; i < totalRel; i++)
{
cout << "Enter name of family member " << (i +1 ) << ":> ";
cin >> Name; //input Name of family member
relNameArray[i] = Name;
cout << "Enter the age of " << Name << ":> ";
cin >> relAge; //input age of family member
sumOfAges = sumOfAges + relAge; //formula to add total of all ages
cout << "Enter the state where " << Name << " resides in" << ":> ";
cin >> State; //Input state of residence
stateArray[i] = State;
cout << "------------------------------------------------------ " << endl;
}
avgAges = sumOfAges/totalRel; //formula to find average age
cout << "The average age of you family members is:" << avgAges << endl;
//statement to declare which family members are from Texas
cout << "The following family members are from Texas: " << endl;
for (int i = 0; i < totalRel; i++)
{
if (stateArray[i] == "texas")
cout << relNameArray[i] << endl;
}
return 0;
}
I have one more question
I tried this to get it to display if no relative were from Texas but the thing is even if there is 1 person from texas and 1 not from Texas it still prints out the statement. I only need it to print out if no relatives input are from Texas but I am having trouble thinking of how to do it.
1 2 3 4 5 6 7 8 9 10 11 12
//statement to declare which family members are from Texas
cout << "The following family members are from Texas: " << endl;
for (int i = 0; i < totalRel; i++)
{
if (stateArray[i] == "texas")
cout << relNameArray[i] << endl;
elseif (stateArray[i] != "texas" || stateArray[i] != "Texas")
cout << "No relatives are from Texas << endl;
}
return 0;
}
elseif (stateArray[i] != "texas" || stateArray[i] != "Texas")
That will always be true, it can't be both equal to "texas" and "Texas" at same time. You probably want && (and) instead of || (or).
Or better: Just get rid of the redundancy.
1 2 3 4
if (stateArray[i] == "texas" || stateArray[i] == "Texas")
; // relative is from texas
else
; // relative is not from texas
To your question, split it up like this:
1 2 3 4 5 6 7 8
bool no_relatives_from_texas = true;
for (int i = 0; i < totalRel; i++)
{
if (relative[i] is from texas)
no_relatives_from_texas = false;
}
if (no_relatives_from_texas)
cout << "No relatives are from Texas << endl;