ok I got this far in the code but I can't seem to get a person to enter an age and the program to return the info of those with the same age.

I think this is what I need to use...or at least something like this on my code.

/////                                                                                  
int search(student[], int size, int anAge);
{
  for (int i=0; (i<size); i++)
    if (Slist[i].Age == anAge)
      {
        return i;
      }
}
////   


#include <iostream>
#include <string>
#include <fstream>

using namespace std;

struct student
{
  string Name;
  char   Gender;
  string Major;
  int    Age;
};

void GatherData(student[], int);                                                                                 
void DisplayStudent(student[], int);
void BubbleSort(student[], int);                                                                                

int main()
{
  student MyClass[10];
  GatherData(MyClass, 10);                                                                   
  BubbleSort(MyClass, 10);
  for (int s=0; s < 10; s++)
    DisplayStudent(MyClass, s);

  return 0;
}

void GatherData(student Slist[], int size)
{
  student Default = {"lastname, firstname", 'gender', "major", age};
  int i = 0;
  ifstream inFile("hw1.txt", ios::in);
  string last, first;
  while (inFile >> first)
    {
      inFile >> last;
      Slist[i].Name  = last + ", " + first;
      inFile >> Slist[i].Gender;
      inFile >> Slist[i].Major;
      inFile  >> Slist[i].Age;
      i++;
    }
  for (int j = i; j < size; j++)
    {
      Slist[j] = Default;
    }
}


void DisplayStudent(student Slist[], int slot)
{
  cout << "Name: " << Slist[slot].Name << endl;
  cout << "Gender: " << Slist[slot].Gender << endl;
  cout << "Major: " << Slist[slot].Major << endl;
  cout << "Age: " << Slist[slot].Age << endl;
}

void BubbleSort(student Slist[], int N)
{                                                           
  student temp;
  for (int pass = 1; pass <= N-1; pass++)                             
  {
    bool didIf= false;
    for (int item= 0; item <  N-pass; item++)
    {
        if (Slist[item].Name > Slist[item+1].Name)
          {
            didIf = true;
            temp= Slist[item];
            Slist[item] = Slist[item+1];
            Slist[item+1] = temp;
          }
    }                                          
    if (didIf == false) return;
  }
}

Edited 3 Years Ago by mike_2000_17: Fixed formatting

When you read the Rules as requested upon signing up, what part of using CODE tags was unclear? Or even the post titled "Read this before posting"?

This article has been dead for over six months. Start a new discussion instead.