
December 4th, 2008, 05:27 PM
|
|
Registered User
|
|
Join Date: Oct 2008
Posts: 4
Time spent in forums: 30 m 58 sec
Reputation Power: 0
|
|
|
File IO - Binary file.read program
Quote: In an infinite loop show the following menu and allow users to select an operation:
1. List the students sorted by last name
2. Search the students by last name
3. List students with more than certain GPA
4. Quit the program
Once the user selects an operation, your program should perform the operation and display the menu again (except for Quit the program). For options 1 your program needs to sort the array and then display list of students with major and GPA. For options 2 your program should ask the user to enter student’s last name and display the student’s information. If the student doesn’t exist in the array, display appropriate message and show the menu again. For options 3 your program should ask the user to enter a cutoff GPA and list all student whose GPA is greater than or equal to the cutoff GPA. Your program should display appropriate message for user actions. |
Code:
//********************************************/
// Author: David Hill
// Description: This program is confusing
//********************************************/
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstring>
using namespace std;
struct Student
{
char firstName[16];
char lastName[16];
char major[4];
double GPA;
};
// Defining Functions
void stdSORT(Student [], int); // Option 1
void stdSEARCH(Student [], int); // Option 2
void searchGPA(Student [], int); // Option 3
void exchange(Student, Student ); // the extra "cup"
int main(int argc, char *argv[])
{
int choice; // Menu choice
int count; // To count number of students
int i;
// File Operations
fstream file;
file.open(argv[1], ios::in | ios::binary);
file.read(reinterpret_cast<char *>(&count), sizeof(int));
Student *STRUC = new Student[count];
for (i = 0; i < count; i++)
{
file.read(reinterpret_cast<char *>(&STRUC[i]), sizeof(Student));
}
if (count < 0)
{
cout << "Error! No students detected in file\n\n";
exit(-1);
}
else if (!file)
{
cout << "File did not open, please try again.\n\n";
exit(-1);
}
for (i = 0; i < count; i++)
{
file.read(reinterpret_cast<char *>(&STRUC[i]), sizeof(Student));
}
while(10)
{
cout << "Please Select an Option: " << endl;
cout << "1. List the students sorted by last name" << endl;
cout << "2. Search the students by last name" << endl;
cout << "3. List students with more than certain GPA" << endl;
cout << "4. Quit the program" << endl;
cin.ignore();
cin >> choice;
while (choice < 1 || choice > 4)
{
cout << "Invalid option, please choose a new one: ";
cin >> choice;
}
}
switch (choice)
{
case 1: stdSORT(STRUC, count);
break;
case 2: stdSEARCH(STRUC, count);
break;
case 3: searchGPA(STRUC, count);
break;
case 4:
return 0;
}
}
void stdSORT (Student STRUC[], int count)
{
for (int i = 0; i < count-1; i++)
{
for (int j = 0; j < count-1-i; j++)
if (strcmp(STRUC[j].lastName, STRUC[j+1].lastName) >= 1)
exchange (STRUC[j], STRUC[j+1]);
}
cout << "The student's listed in alphabetical order:" ;
for (int i = 0; i < count; i++)
{
cout << STRUC[i].lastName << "\n";
cout << endl;
}
}
void exchange(Student S1, Student S2)
{
Student extra;
extra = S1;
S1 = S2;
S2 = extra;
}
void stdSEARCH(Student STRUC[], int count)
{
bool flag = false;
char stdsearch[16];
cout << "What is the Last Name of the student you would like to search? ";
cin >> stdsearch;
for (int ctr = 0; ctr < count; ctr++)
{
if (strcmp(stdsearch, STRUC[ctr].lastName) == 0)
{
flag = true;
cout << STRUC[ctr].firstName << endl;
cout << STRUC[ctr].lastName << endl;
cout << STRUC[ctr].major << endl;
cout << STRUC[ctr].GPA << endl;
}
}
if (flag == false)
{
cout << "Student is non-existent. Please Try again.";
exit(-1);
}
}
void searchGPA(Student STRUC[], int count)
{
double GPAsearch;
cout << "Please enter a GPA you wish to search: ";
cin >> GPAsearch;
while (GPAsearch < 0.0 || GPAsearch > 4.0)
{
cout << "Invalid GPA. Please enter again: ";
cin >> GPAsearch;
for (int i = 0; i < count-1; i++)
{
STRUC[i].GPA > GPAsearch;
if (STRUC[i].GPA > GPAsearch)
{
cout << STRUC[i].firstName << "'s GPA: " << STRUC[i].GPA << endl;
}
}
}
}
And that is all i've got. Everything works until I get my menu going in cmp. I can't seem to figure out why it won't work. Sorry for the lack of comment...
|