If statement help.

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!!

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
  // Final Project 
//Developed by Alex Macias
//CMIS 102

#include <iostream>
#include <iomanip>
#include <string>  //use when using a string variable
using namespace 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;
}
First a bug. You need to initialize sumofAges to zero, so line 33 should be:
int totalRel, relAge, sumOfAges=0;

As for your relatives in Texas, you need to replace lines 67-69 with a loop to check each family member individually. In pseudo-code:
1
2
3
4
5
6
cout << "The following family members are from Texas:\n" 
for each relative {
    if the relative's state is texas {
         print the relative's name
    }
}
sorry I am still really confused, would it be something like this? do you mind breaking it down for me a little bit more?
1
2
for ( totalRel = 0; State == "Texas" || State == "texas"; totalRel++)
cout << "The following family members are from Texas:\n" << Name <<endl;
Last edited on
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
  // Final Project
//Developed by Alex Macias
//CMIS 102

#include <iostream>
#include <iomanip>
#include <string>  //use when using a string variable
using namespace 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;
     else if (stateArray[i] != "texas" || stateArray[i] != "Texas")
            cout << "No relatives are from Texas << endl;
    }

	return 0;
} 

else if (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; 
Last edited on
Registered users can post here. Sign in or register to post.