// Ch. 10 Demo Program #3 // Mr. Minich // This program illustrates the use of the data structure, struct // which allows programmers to define their own data types. #include<iostream.h> #include <string.h> // necessary for strcpy function struct Team // The definition of the struct Team. { // Often struct definitions are placed int Wins; // above/before the main function so int Losses; // that actual struct variables (like PennState, char CoachLastName[21]; // Michigan, & OhioState, below) can float WinPct; // be used in any function of the program. }; // end of Team // Wins, Losses, CoachLastName, and WinPct are "members" of Team. int main() { int IntInput = 0; char NameInput[21]; Team PennState; // Declares the variable PennState to be the // data type Team (which is a struct that // is defined above.) Team Michigan; Team OhioState; cout << "Enter Penn State's wins: "; cin >> IntInput; PennState.Wins = IntInput; // The . is the dot operator which must // must be used to reference specific // members (fields) of the struct variable. cout << "Enter Penn State's losses: "; cin >> IntInput; PennState.Losses = IntInput; strcpy(PennState.CoachLastName, "Paterno"); // PennState.CoachLastName = "Paterno"; will NOT work PennState.WinPct = float (PennState.Wins) / (float (PennState.Wins) + float (PennState.Losses)); // Note the use of typecasting above. Michigan.Wins = --PennState.Wins; Michigan.Losses = ++PennState.Losses; OhioState.Wins = --Michigan.Wins; OhioState.Losses = ++Michigan.Losses; Michigan.WinPct = Michigan.Wins / (Michigan.Wins + Michigan.Losses); OhioState.WinPct = OhioState.Wins / (OhioState.Wins + OhioState.Losses); // Can you explain why Michigan and Ohio State's winning percentages will // be stored as 0? cout << "Penn State's winning percentage is " << PennState.WinPct << endl; return 0; } // end of main