Array of Structs ASAP


I cant get my array to print in the output, i dont know whats going on please help!


#include <cstdlib>

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

using namespace std;

struct footballPlayers
{
int no;
string first;
string last;
char position;
char year;
int heightFeet;
int heightInches;
int weight;
string hometown;
};

const int ARRAY_SIZE = 25;

void getData (ifstream& inFile, footballPlayers list [], int& listSize);
footballPlayers getOne (ifstream& dataI);
void selectionSort (footballPlayers list[], int listSize);
void printList(footballPlayers list[], int listSize);
void printOne (footballPlayers one);


int main(int argc, char *argv[])
{
cout<<"Driver Name: Chad Cundiff"<<endl;
cout<<"Navigator Name: Chad Cundiff"<<endl;
cout<<"Date: November 9, 2010"<<endl;
cout<<"Lab CRN: 10940"<<endl;
cout<<"This Program works with an array of structs."<<endl;
cout<<endl<<endl;

footballPlayers list [ARRAY_SIZE];
int number;

char choice;


string fileName;
cout<<"Enter file name."<<endl;
getline (cin, fileName);

ifstream dataFile(fileName.c_str() );

if (!dataFile)
{
cout<<"File doesn't exist!\n\n";
system ("pause");
return 1;
}

////////////////////////////////////////////////////
getData (dataFile, list, number);
selectionSort (list, number);
printList( list, number);


system("PAUSE");
return EXIT_SUCCESS;
}
/////////////////////////////////////////////////
footballPlayers getOne (ifstream& dataIn)
{
footballPlayers one;

dataIn >> one.no >> one.first >> one.last >> one.position >> one.year >>
one.heightFeet >> one.heightInches >> one.weight >> one.hometown;

return one;
}
////////////////////////////////////////////////////
void getData (ifstream& inFile, footballPlayers list [], int& listSize)
{
footballPlayers item;

listSize = 0;
item = getOne (inFile);
while (inFile)
{
list [listSize] = item;
listSize++;
item = getOne (inFile);
}
inFile.close ();
}

////////////////////////////////////////////////////
void selectionSort (footballPlayers list[], int listSize)
{
int index;
int smallestIndex;
int minIndex;
footballPlayers temp;

for (index = 0; index < listSize - 1; index++)
{
smallestIndex = index;

for (minIndex = index + 1; minIndex < listSize; minIndex++)
if (list[minIndex].no > list[smallestIndex].no)
smallestIndex = minIndex;

temp = list[smallestIndex];
list[smallestIndex] = list[index];
list[index] = temp;
}

}
///////////////////////////////////////////////////
void printList(footballPlayers list[], int listSize)

{
int looper;

for (looper = 0; looper < listSize; looper++)
{
printOne (list [looper]);
cout<<endl;
}
}
//////////////////////////////////////////////////
void printOne (footballPlayers one)
{
cout<<"NO: "<<one.no<<endl;
cout<<"Name: "<<one.first<<" "<<one.last<<endl;
cout<<"Position: "<<one.position<<endl;
cout<<"Class: "<<one.year<<endl;
cout<<"Height: "<<one.heightFeet<<"'"<<one.heightInches<<endl;
cout<<"Weight: "<<one.weight<<endl;
cout<<"Hometown: "<<one.hometown<<endl;
cout<<endl;
}
Your code should be properly formatted and in code tags, like so. Also, please post your input file.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void printList(footballPlayers list[], int listSize){
    int looper;
    for (looper = 0; looper < listSize; looper++){
        printOne (list [looper]);
        cout<<endl;
    }
}

void printOne (footballPlayers one){
    cout<<"NO: "<<one.no<<endl;
    cout<<"Name: "<<one.first<<" "<<one.last<<endl;
    cout<<"Position: "<<one.position<<endl;
    cout<<"Class: "<<one.year<<endl;
    cout<<"Height: "<<one.heightFeet<<"'"<<one.heightInches<<endl;
    cout<<"Weight: "<<one.weight<<endl;
    cout<<"Hometown: "<<one.hometown<<endl;
    cout<<endl;
}
For output data inside a struct or class, I tend to overload the ostream << operator. It is 'neater' from my opinion.

E.g
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
struct footballPlayers
{
int no;
string first;
string last;
char position;
char year;
int heightFeet;
int heightInches;
int weight;
string hometown;
friend ostream& operator<<(ostream& os, const footballPlayers& obj); 
};

ostream& operator<<(ostream& os, const footballPlayers& obj) {  
  os << obj.first << obj.last << "\n"; //put all output statement here
  return os;  
}  

//In printOne function

void printOne(footballPlayers one) {
  cout << one;
}
Last edited on
Whereas I hate the idea of bit shifting a file by a football player.
cout *= pow(2,footballplayer);//what????
Last edited on
Topic archived. No new replies allowed.