Help whith Structure

I have a project to display the members of the family who lives in the same state and the average age of all members. I got the part of the average age but now im stuck trying to display only the members that live in Florida. Im able to display all members but I need just the ones in who lives in Florida.I tried to use State='Fl' but it didnt work Thank you in advance....

I think it should be something like:

if(State='Fl')


{

// displaying the stored data

cout << i << " First name: " << fami[i].Name << endl;

cout << " Last Name: " << fami[i].LastName << endl;

cout << " State: " << fami[i].State << endl;
cout << " " << endl;

}

it just return garbage.



Code:

#include <iostream>
#include <string>
#include <cstdio>
#include <cmath>
#include <stdlib.h>
#include <conio.h>

using namespace std;

void Menu(); // menu selection
void FamilyInfo(); // call Family Information Input


struct family // family structure

{

char Name[20]; // Name, max 19 characters

char LastName[20]; // Last Name, max 19 characters

char State[20]; // State, max 19 characters

int Age; // age


};


int main() // main module
{

Menu(); // call manu options

FamilyInfo(); // cal Family information input


// system("pause");
return 0;

}

void Menu() // Menu module
{

char select;

while(select !='C')
{

cout << "Please choose from the following" << endl;
cout << endl;

cout << " A ) Enter Family members information" << endl;
cout << " C ) Clear " << endl;
cout << " Option: ";
cin >> select;
cout << endl;
cout << endl;

switch(select)

{

case 'A':
FamilyInfo();
break;

default:
system("CLS");
break;



}



}

}

void FamilyInfo() // FamilyInfo Module

{

// declaring array of 10 element of structure type

// and some of the element also are arrays

struct family fami[10];

int i = 1;
int sum=0;
int avg=0;


cout << " Family Members Information" << endl;
cout << " _________________________________ " << endl;




for(i=1; i<3; i++)

{


// storing the data

cout << " Plase, enter the family information" << endl
<< " " << endl;

cout << i << ") First name:";

cin >> fami[i].Name;

cout << " Last Name: ";

cin >> fami[i].LastName;

cout <<" State: ";

cin >> fami[i].State;

cout << " Age: ";

cin >> fami[i].Age;

sum=sum+fami[i].Age;


system("CLS");
}

avg=sum/3;

cout << " Family Members Information" << endl;

cout << "_________________________________" << endl;
cout << " " << endl;

cout << " Avergage age of the family: " << avg << endl;
cout << " " << endl;
cout << " " << endl;



for(i=1; i<3; i++)

{

// displaying the stored data

cout << i << " First name: " << fami[i].Name << endl;

cout << " Last Name: " << fami[i].LastName << endl;

cout << " State: " << fami[i].State << endl;
cout << " " << endl;

}

}
If you want someone to be able to read this please use code tags.
Also, if you're going to program in C++ use strings for character manipulation.
http://www.cplusplus.com/reference/string/string/
The ' ' are used for single characters only so you should have gotten a compile error for 'Fl' I believe. Use the " " for strings, which can be composed of several characters, numbers, and symbols.

Also note that in C++:

= is not the same as ==

The former is for assignment, assigning things into variables and the latter is for the logical "equals".


Peace.
Last edited on
Topic archived. No new replies allowed.