hi! i need help with my code...i dont have any syntax errorrs but when i run it it doensnt run!! i dont kno what to do!! plss i really need help!!!
i have to write code tht uses array to store student information and calculate the average, letter grade. i also need help sorting the student name in alphabetical order, deleting a student and editing a student's info! (i basically need to everything thts in my menu)
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;
#define MAX_STUDENTS 20
#define MAX_TEST_SCORES 4
void sort_names (string [], int);
int search(string[],int,string);
struct student_records
{
string first_name, last_name;
int test[MAX_TEST_SCORES];
int num_scores;
double average;
char letter_grade;
};
bool open_and_verify_files(ifstream&fin, ofstream&fout, student_records&s);
void sort_names(string names[], int size );
bool valid_score(int score);
double student_records_num_valid_scores(student_records s);
bool student_records_all_scores_valid(student_records s);
double student_records_sum_scores(student_records s);
bool student_add_test_score(student_records &s, int score);
double student_records_lowest_score(student_records s);
void set_student_values(student_records &s, string first_name, string last_name, int test_scores[]);
bool get_student_from_keyboard(student_records &s);
int get_students_from_keyboard(student_records sa[]);
void student_records_get_average(student_records &s);
void student_records_get_letter_grade(student_records &s);
void intro_to_screen (string intro);
void intro_to_file (ofstream &fout);
void menu_to_screen (string menu_options);
void student_records_print_to_file(student_records s, ofstream &fout);
void student_records_print_to_screen(student_records s);
void student_records_read_from_file(student_records &s, ifstream &fin);
void student_records_read_from_keyboard(student_records &s);
void student_records_init(student_records &s);
void student_array_initialize(student_records sa[], int size);
bool student_array_add_student(student_records sa[], int phys_size, int &num, student_records s);
void clearScreen();
int Menu( );
void Process( );
void pause();
int main()
{
ifstream fin;
ofstream fout;
bool good_data;
student_records s;
string input_file, output_file, intro;
int default_file_ans;
cout << fixed << showpoint << setprecision(2) << left;
intro_to_screen (intro);
cout << "Do you want to use default file? (0 = no) ";
cin >> default_file_ans;
if (default_file_ans == 0)
{
if (open_and_verify_files(fin, fout, s) == false)
return 1;
else
{
Menu();
Process();
}
}
else
{
Menu();
Process();
}
/*
if (open_and_verify_files(fin, fout, s) == false)
return 1;
else
{
fout << fixed << showpoint << setprecision(2) << left;
}
while(!fin.eof())
{
student_records_read_from_file(s, fin);
if(student_records_all_scores_valid(s) == false)
{
cout << setw(15) << s.last_name + ", " + s.first_name << "Bad data!" << endl;
fout << setw(15) << s.last_name + ", " + s.first_name << "Bad data!" << endl;
fin >> s.first_name;
continue;
}
verify_input(good_data, s);
if(student_records_all_scores_valid(s) == true)
{
student_records_get_average(s);
student_records_get_letter_grade(s);
print_to_monitor(s);
print_to_file(fout, s);
}
else
{
cout << setw(15) << s.last_name + ", " + s.first_name << "Bad data!" << endl;
fout << setw(15) << s.last_name + ", " + s.first_name << "Bad data!" << endl;
fin >> s.first_name;
continue;
}
fin.ignore();
fin >> s.first_name;
}
fin.close();
fout.close();
*/
return 0;
}
bool open_and_verify_files(ifstream&fin, ofstream&fout, student_records&s)
{
string input_file, output_file;
cout << "Please enter the input file name: ";
getline(cin, input_file);
fin.open(input_file.c_str());
if(fin.fail())
{
cout << endl << "Cannot open input file." << endl << endl;
return false;
}
else
{
cout << "Please enter the output file name: ";
getline(cin, output_file);
fout.open(output_file.c_str());
}
fin >> s.first_name;
if(fin.eof())
{
cout << endl << "Input file is empty." << endl << endl;
fout << "Input file is empty." << endl;
return false;
}
return true;
}
///////////////////////////////////////////////////////////////
void sort_names(string names[], int size )
{
string temp;
int j ;
bool flag;
do {
size--;
flag = true ;
for (j= 0; j < size ; j++)
if ( names[j].compare(names[j+1]) > 0)
{
temp = names[j];
names[j] = names[j+1] ;
names[j+1] = temp ;
flag = false ;
}
}while (!flag);
}
////////////////////////////////////////////////////////////////////////////
bool valid_score(int score)
{
return (score >= 0) && (score <= 100);
}
double student_records_num_valid_scores(student_records s)
{
int count = 0;
for (int index = 0; index < MAX_TEST_SCORES; index++)
{
// Count every valid score.
if (valid_score(s.test[index]))
count++;
}
// Return the number of valid scores.
return count;
}
bool student_records_all_scores_valid(student_records s)
{ // Returns true if ALL of the student's scores are valid, otherwise false.
return student_records_num_valid_scores(s) == MAX_TEST_SCORES;
}
double student_records_sum_scores(student_records s)
{
int sum = 0;
for (int index = 0; index < MAX_TEST_SCORES; index++)
{
sum += s.test[index];
}
return sum;
}
bool student_add_test_score(student_records &s, int score)
{
if (s.num_scores < 0)
s.num_scores = 0;
if (s.num_scores < MAX_TEST_SCORES)
{
s.test[s.num_scores] = score;
s.num_scores++;
return true; // A score was successfully added
}
else
return false; // If we got here, we could not add the score because the list is full.
}
double student_records_lowest_score(student_records s)
{
int n = 0;
n = s.test[0];
for (int index = 1; index < MAX_TEST_SCORES; index++)
{
if (s.test[index] < n)
n = s.test[index];
}
return (double)n;
}
void set_student_values(student_records &s, string first_name, string last_name, int test_scores[])
{
s.first_name = first_name;
s.last_name = last_name;
for (int index = 0; index < MAX_TEST_SCORES; index++)
{
s.test[index] = test_scores[index];
}
student_records_get_average(s);
student_records_get_letter_grade(s);
}
bool get_student_from_keyboard(student_records &s)
{
string first_name, last_name;
int test_scores[MAX_TEST_SCORES];
cout << "Enter the first name (leave this line blank to cancel): ";
getline(cin, first_name);
if (first_name == "")
{
return false;
}
cout << "Enter the last name: ";
getline(cin, last_name);
cout << "Enter the " << MAX_TEST_SCORES << " test scores: ";
for (int index = 0; index < MAX_TEST_SCORES; index++)
{
cin >> test_scores[index];
}
cin.ignore(100, '\n');
set_student_values(s, first_name, last_name, test_scores);
return true;
}
int get_students_from_keyboard(student_records sa[])
{
int count;
for (count = 0; count < MAX_STUDENTS; count++)
{
if (!get_student_from_keyboard(sa[count]))
{
break;
}
}
return count;
}
void student_records_get_average(student_records &s)
{
if (student_records_all_scores_valid(s))
s.average = (student_records_sum_scores(s) - student_records_lowest_score(s)) / (MAX_TEST_SCORES - 1);
else
s.average = -1.0; // mark the average as bad.
}
void student_records_get_letter_grade(student_records &s)
{
// double average = student_records_get_average(s); // force the average to be recalculated.
if(s.average > 90.00)
s.letter_grade = 'A';
else if(s.average > 80)
s.letter_grade = 'B';
else if(s.average > 67.5)
s.letter_grade = 'C';
else if(s.average > 55)
s.letter_grade = 'D';
else if(s.average > 0)
s.letter_grade = 'F';
}
void intro_to_screen (string intro)
{
cout << "Horiya's Very Famous Grade Calculator" << endl << endl;
cout << "This progam processes test scores to provide individuals with letter grades according to the following scale:" << endl << endl;
cout << setw(25) << " " << "90.00 < average A" << endl;
cout << setw(25) << " " << "80.00 < average <= 90.00 B" << endl;
cout << setw(25) << " " << "67.50 < average <= 80.00 C" << endl;
cout << setw(25) << " " << "55.00 < average <= 67.50 D" << endl;
cout << setw(25) << " " << "0.00 < average <= 55.00 F" << endl;
cout << setfill('*') << setw(90) << "*" << endl;
}
void intro_to_file (ofstream &fout)
{
fout << "Horiya's Very Famous Grade Calculator" << endl << endl;
fout << "This progam processes test scores to provide individuals with letter grades according to the following scale:" << endl << endl;
fout << setw(25) << " " << "90.00 < average A" << endl;
fout << setw(25) << " " << "80.00 < average <= 90.00 B" << endl;
fout << setw(25) << " " << "67.50 < average <= 80.00 C" << endl;
fout << setw(25) << " " << "55.00 < average <= 67.50 D" << endl;
fout << setw(25) << " " << "0.00 < average <= 55.00 F" << endl;
fout << setfill('*') << setw(90) << "*" << endl;
}
void menu_to_screen (string menu_options)
{
cout << setfill('*') << setw(90) << "*" << endl;
cout << "* 1. Print the list of students in a tabular form to the monitor *" << endl;
cout << "* 2. Print the list of students in a tabular form to a file. (Prompt user for file name.)*" << endl;
cout << "* 3. Sort the list of students in alphabetical order based on their last name. *" << endl;
cout << "* 4. Sort the list of students in descending order based on their average. *" << endl;
cout << "* 5. Add a new student to the list. *" << endl;
cout << "* 6. Remove a student from the list. *" << endl;
cout << "* 7. Edit a student information. *" << endl;
cout << "* 8. Quit the program. *" << endl;
cout << setfill('*') << setw(90) << "*" << endl;
}
void student_records_print_to_file(student_records s, ofstream &fout)
{
fout << setfill(' ') << setw(35) << " " << "Tests" << endl;
fout << setw(15) << "Name" << setw(10) << "1" << setw(10) << "2" << setw(10) << "3" << setw(10) << "4" << setw(15) << "Average" << "Letter Grade" << endl;
fout << setw(10) << s.first_name << setw(15) << s.last_name;
for (int i=0; i< MAX_TEST_SCORES; i++)
{
fout << setw(10) << s.test[i] ;
}
if (s.average >= 0.0)
{
fout << setw (15) << s.average << s.letter_grade << endl;
}
else
// stuff.
;
}
void student_records_print_to_screen(student_records s)
{
cout << setfill(' ') << setw(35) << " " << "Tests" << endl;
cout << setw(15) << "Name" << setw(10) << "1" << setw(10) << "2" << setw(10) << "3" << setw(10) << "4" << setw(15) << "Average" << "Letter Grade" << endl;
cout << setw(10) << s.first_name << setw(15) << s.last_name;
cout << setw(10) << s.first_name << setw(15) << s.last_name;
for (int i=0; i< MAX_TEST_SCORES; i++)
{
cout << setw(10) << s.test[i] ;
}
cout << setw (15) << s.average << s.letter_grade << endl;
}
void student_records_read_from_file(student_records &s, ifstream &fin)
{
fin >> s.first_name;
fin >> s.last_name;
for (int i=0; i< MAX_TEST_SCORES; i++)
fin >> s.test[i];
set_student_values(s, s.first_name, s.last_name, s.test);
}
void student_records_init(student_records &s)
{
int zeroes[MAX_TEST_SCORES] = {0, 0, 0, 0};
set_student_values(s, "", "", zeroes);
}
void student_array_initialize(student_records sa[], int size)
{
for (int index = 0; index < size; index++)
{
student_records_init(sa[index]);
}
}
bool student_array_add_student(student_records sa[], int phys_size, int &num, student_records s)
{
if (num < phys_size)
{
sa[num] = s;
num++;
return true;
}
else
return false;
}
void clearScreen()
{
system("cls");
}
int Menu( )
{
int choice;
cout << setfill('*') << setw(90) << "*" << endl;
cout << "* 1. Print the list of students in a tabular form to the monitor *" << endl;
cout << "* 2. Print the list of students in a tabular form to a file. (Prompt user for file name.)*" << endl;
cout << "* 3. Sort the list of students in alphabetical order based on their last name. *" << endl;
cout << "* 4. Sort the list of students in descending order based on their average. *" << endl;
cout << "* 5. Add a new student to the list. *" << endl;
cout << "* 6. Remove a student from the list. *" << endl;
cout << "* 7. Edit a student information. *" << endl;
cout << "* 8. Quit the program. *" << endl;
cout << setfill('*') << setw(90) << "*" << endl;
cout << "Please enter your choice: ";
cin >> choice;
return choice ;
}
void pause()
{
cout << endl << endl << "\tPress 'ENTER' to continue...." << endl;
cin.ignore(10,'\n');
cin.get();
}
void Process( )
{
string intro;
int sa, default_file_ans;
student_records s;
ifstream fin;
ofstream fout;
int choice ;
do
{
clearScreen();
choice = Menu();
if (choice == 1)
{
intro_to_screen (intro);
student_records_print_to_screen(s);
break;
}
else if (choice == 2)
{
if (default_file_ans == 0)
{
if (open_and_verify_files(fin, fout, s) == true);
{
student_records_all_scores_valid(s);
//have a then statement for else and say what will happens if its not gonna happen
}
}
else
break;
}
else if (choice == 3)
{
break;
}
else if (choice == 4)
{
break;
}
else if (choice == 5)
{
if (get_student_from_keyboard(s) == true)
int get_students_from_keyboard(sa);
//have a then statement for else and say what will happens if its not gonna happen
break;
}
else if (choice == 6)
{
break;
}
else if (choice == 7)
{
break;
}
else if (choice == 8)
{
break;
}
else
{
cout << "\n\n\tNot a valid choice.\n" << "\tChoose again.\n\n";
}
pause();
}while (choice != 8);
}
please i am really desperate!! i really need help!! thanks in advence times a million :D!