Question: Unitialized local variables in C++ program and can't get it to run properly. Here is my code: #include #include #include #include #include using namespace std;

Unitialized local variables in C++ program and can't get it to run properly. Here is my code:

#include #include #include #include #include using namespace std;

//structure Student struct student { string name; int percent; };

//function prototypes bool addStudent(student &student); void outputStudent(student &student, string fileName, bool addHeading); bool validGrade(int *percent);

//main function int main()//put function calls in main { string fileName; bool validGrade; bool addHeader; student student; //creates the instance of the structure thats passed to the addStudent function char choice;

cout << "Enter output file name: ";//asks user what you want to call file getline(cin, fileName); fileName += ".txt";//appends name to the file

//loop that calls function do { validGrade = addStudent(student); if (validGrade)//check for valid input { outputStudent(student, fileName, addHeader); addHeader = false; } else { cout << "Invalid grade!" << endl; cout << "Do you want to add another student? (Y/N)" << endl; cin >> choice; cin.ignore(); } }//end do while (choice=='y' || choice=='Y');

return 0; }//end main

//FUNCTIONS //////////////////////////////////////////////////////////// void outputStudent(student & student, string fileName, bool addHeading) { ofstream outfile(fileName, ofstream::app);

if (outfile.fail()) { cout << "Output file opening failed!" << endl; exit(1); }

if (addHeading) { outfile << setw(20) << left << "Name" << "Guide" << endl;

outfile << setw(20) << left << student.name << setw(20) << student.percent << endl;

outfile.close(); } } //////////////////////////////////////////////////////////// bool addStudent(student &student) { int p; cout << "Adding new student Enter name: "; getline(cin, student.name); cout << "Enter student grade percentage: " << endl; cin >> p; student.percent = p; if (validGrade(&student.percent)) { return true; } else { cout << endl << "Invalid grade percentage!" << endl; return false; } } //////////////////////////////////////////////////////////// bool validGrade(int *percent)//percent is passed as pointer { if (*percent < 0 || *percent > 100) { return false; } else { return true; } }//this function is called in the addStudent function aswell ////////////////////////////////////////////////////////////

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!