Welcome to Minetown!
To join our community, please login or register!
Internet Explorer
Internet Explorer is not supported. Please upgrade to a more modern browser.

[Enjin Archive] This weeks C++ homework!
Started by Unknown User

Damn this project is kicking my ass. So far I'm stuck on searching a character array for "DONE" in a while loop.
You need me to take a crack at it?

I don't have much time but i can put off other things so i can help.
Yeah I'll hit you up on mumble after my class tonight if you're on. I'm pretty solid on looping to get array values but using call by functions to get arrays and searching arrays is something im bad at.
Here's the updated instructions for the project.

Comprehensive, Offline, Universal and Resilient Student Evaluation DataBase

(aka COURSE DB)

In this project you will create a simple database system (COURSE DB) for storing student’s course grades. Your program will create and access a text file with the student’s grades. The database will contain two pieces of information for each student: the student’s name (a string) and their current grade (a double). Assume that each student can be uniquely identified by their first name. The database file should look like the following after it is created by your program:

Bjarne 99.00

Mary 99.50

Dexter 92.30

Bob 75.67

Susan 98.32

Your database system will provide the following functionality:

1. Create a new database

This operation will create a new text file in the format shown above. It will allow a maximum of 20 students to be entered into the database. Your program should have a constant global variable that defines the maximum number of students allowed (e.g. MAX_NUM_STUDENTS). By simply changing the value of this constant in your code, your program should be able to support different maximum class sizes. The program should prompt the user for each student’s name and grade. It should verify a valid grade (between 0 and 100) was entered. If a value of DONE is typed for the student’s name, then the program should stop asking for names and grades. The data entered by the user should then be written to a file. Allow the student’s name to be up to 32 characters in length and assume no spaces will be entered (i.e. first name only). Again, this should be defined as a global constant variable (e.g. MAX_NAME_LENGTH). If the user enters the same name twice your program should display an error and ask them to enter a unique student name.

2. View the current database contents

This operation will load the contents of the database from a file formatted as described above. It will print out the name of each student and their class grade. It will also print the current class average.

3. Modify a student’s grade.

This operation will first load the current database. You may optionally display all the student’s names and their current grades. It will then ask the user to type in the name and new grade for the student they would like to modify. If an invalid name is entered, the user should be prompted to enter another name. The program should then verify that the grade is between 0 and 100. Finally, the file should be written again with the updated grade.

Some General Guidance

Your program must use arrays to store data. Use your judgment to figure out how many functions to create (hint: more than two!). Other than constants, do not use any other global variables in your program. We talked about using the strcmp function to compare two strings.

Use Cases (User input is highlighted)

User attempts to view the database before it has been created:

###################################################

# #

# COURSE DB v1.0 #

# #

###################################################

Please select from one of the following menu options:

(C)reate a new database

(V)iew the current database

(M)odify a student's grade

(Q)uit

> v

Error reading database file...

User attempts to modify the database before it has been created:

###################################################

# #

# COURSE DB v1.0 #

# #

###################################################

Please select from one of the following menu options:

(C)reate a new database

(V)iew the current database

(M)odify a student's grade

(Q)uit

> m

Error reading database file...

User creates a new database. Note the behavior when an invalid grade is entered. Also notice that when “DONE 0” is entered, the program stops asking for grades and writes what it has the database file.

###################################################

# #

# COURSE DB v1.0 #

# #

###################################################

Please select from one of the following menu options:

(C)reate a new database

(V)iew the current database

(M)odify a student's grade

(Q)uit

> c

Enter DONE 0 when finished

Enter student 1's name and grade: bob -1

You entered an invalid grade. Please try again.

Enter student 1's name and grade: Joe 101

You entered an invalid grade. Please try again.

Enter student 1's name and grade: Bob 90.5

Enter student 2's name and grade: Mary 100

Enter student 3's name and grade: Jeff 65

Enter student 4's name and grade: Khary 99

Enter student 5's name and grade: DONE 0

Once done, the contents of the file should look like the following:

Bob 90.50

Mary 100.00

Jeff 65.00

Khary 99.00

The user views the database after creating it:

###################################################

# #

# COURSE DB v1.0 #

# #

###################################################

Please select from one of the following menu options:

(C)reate a new database

(V)iew the current database

(M)odify a student's grade

(Q)uit

> v

Bob 90.50

Mary 100.00

Jeff 65.00

Khary 99.00

The average grade in the database is:88.62

The user attempts to modify the grade of a student that does not exist:

###################################################

# #

# COURSE DB v1.0 #

# #

###################################################

Please select from one of the following menu options:

(C)reate a new database

(V)iew the current database

(M)odify a student's grade

(Q)uit

> m

Bob 90.50

Mary 100.00

Jeff 65.00

Khary 99.00

Enter the name of the student to modify and their new grade: Rob 99

The student's name was not found!

The user successfully modifies a student’s grade. Also note your program should make sure a valid grade is entered.

###################################################

# #

# COURSE DB v1.0 #

# #

###################################################

Please select from one of the following menu options:

(C)reate a new database

(V)iew the current database

(M)odify a student's grade

(Q)uit

> m

Bob 90.50

Mary 100.00

Jeff 65.00

Khary 99.00

Enter the name of the student to modify and their new grade: Bob -1

You entered an invalid grade.

Enter the name of the student to modify and their new grade: Bob 40

The new contents of the database file:

Bob 40.00

Mary 100.00

Jeff 65.00

Khary 99.00

The user views the database after modifying the grade:

###################################################

# #

# COURSE DB v1.0 #

# #

###################################################

Please select from one of the following menu options:

(C)reate a new database

(V)iew the current database

(M)odify a student's grade

(Q)uit

>

v

Bob 40.00

Mary 100.00

Jeff 65.00

Khary 99.00

The average grade in the database is:76.00
Sorry for the triple post. But here is the code skeleton my professor gave the class to work with,

/*

* CourseDB.cpp

*

* Created on: Apr 20, 2011

* Author:

*/

#include <iostream>

#include <cstdlib>

#include <fstream>

#include <iomanip>

using namespace std;

const int MAX_NUM_STUDENTS = 20;

const int MAX_NAME_LENGTH = 32;

void createDatabase();

void viewDatabase();

void modifyGrade();

/*

* This function returns true if the database was loaded successfully, and false otherwise

*/

bool loadDatabase(char names,double grades, int& size);

// Alternative

//int loadDatabase(char names,double grades);

void writeDatabase(char names,double grades,int size);

/*

* This function controls the overall flow of the COURSE DB Application

*/

int main()

{

bool continueProcessing = true;

char userSelection=' ';

while (continueProcessing)

{

displayMenu();

cin >> userSelection;

cin.ignore();

switch (userSelection)

{

case 'c':

case 'C':

createDatabase();

break;

case 'v':

case 'V':

viewDatabase();

break;

case 'm':

case 'M':

modifyGrade();

break;

case 'q':

case 'Q':

continueProcessing = false;

break;

default:

cout << "\nPlease enter a valid response.\n";

}

}

cout << "\n\nGoodbye";

}

/*

* This function displays the COURSE DB main menu

*/

void displayMenu()

{

cout << "###################################################\n";

cout << "# #\n";

cout << "# COURSE DB v1.0 #\n";

cout << "# #\n";

cout << "###################################################\n\n";

cout << "Please select from one of the following menu options:\n\n";

cout << "(C)reate a new database\n";

cout << "(V)iew the current database\n";

cout << "(M)odify a student's grade\n";

cout << "(Q)uit\n";

cout << "> ";

}

/*

* This function creates the COURSE DB database

*/

void createDatabase()

{

char names;

double grades;

// Loop while

// 1) User has not entered "DONE 0"

// 2) Number of students entered is < MAX_NUM_STUDENTS

// Prompt the user for a name and a grade

// Make sure the grade is between 0 and 100

// Add the name and the grade into our arrys

// Done Looping

// write the file. Need the array of grades, array of names

writeDatabase(names,grades);

}

/*

* This function displays the contents of the COURSE DB database

*/

void viewDatabase()

{

// load the database

// loop through the values we loaded and print each one

}

/*

* This function will modify a grade within the COURSE DB database

*/

void modifyGrade()

{

}

bool loadDatabase(char names,double grades, int& size)

{

bool result = true;

int counter=0;

ifstream dbFile;

dbFile.open("......");

if (!dbFile)

{

// Print an error

result = false;

}

else

{

// Loop while there is still information in the file

dbFile >> names >> grades;

counter++;

}

return result;

}
I got it up and running. With EXTREMELY sloppy code. But it works so i don't really care lol. My teacher did assign an extra credit part to this assignment which is in the CreateDatabase() function. Basically he wants us to print an error if the user tries to enter the same name twice. Ex) Someone enters Rob 100 as names grades then tries entering Rob 84 in names grades. Now keep in mind this is case sensitive so Rob and rob are unique. However I know this involves more str(etc.) usage and I suck with that! Cryston halp me! Other than that extra credit this works.

#include <iostream>

#include <cstdlib>

#include <fstream>

#include <iomanip>

using namespace std;

const int MAX_NUM_STUDENTS = 5;

const int MAX_NAME_LENGTH = 32;

void createDatabase();

void viewDatabase();

void modifyGrade();

void displayMenu();

bool loadDatabase(char names,double grades, int& size);

void writeDatabase(char names,double grades,int size);

int main()

{

bool continueProcessing = true;

char userSelection=' ';

while (continueProcessing)

{

displayMenu();

cin >> userSelection;

cin.ignore();

switch (userSelection)

{

case 'c':

case 'C':

createDatabase();

break;

case 'v':

case 'V':

viewDatabase();

break;

case 'm':

case 'M':

modifyGrade();

break;

case 'q':

case 'Q':

continueProcessing = false;

break;

default:

cout << "\nPlease enter a valid response.\n";

}

}

cout << "\n\nGoodbye";

}

void displayMenu()

{

cout << "###################################################\n";

cout << "# #\n";

cout << "# COURSE DB v1.0 #\n";

cout << "# #\n";

cout << "###################################################\n\n";

cout << "Please select from one of the following menu options:\n\n";

cout << "(C)reate a new database\n";

cout << "(V)iew the current database\n";

cout << "(M)odify a student's grade\n";

cout << "(Q)uit\n";

cout << "> ";

}

void createDatabase()

{

char names;

double grades;

int i = 0;

cout << endl;

cout << "Enter DONE 0 when finished" << endl;

while(i < MAX_NUM_STUDENTS)

{

cout << "Enter student " << (i+1) <<"'s name and grade: ";

cin >> names;

cin >> grades;

if(grades < 0 || grades > 100)

{

cout << "You entered an invalid grade. Please try again." << endl;

}

else if(strcmp(names, "DONE") == 0 || strcmp(names, "done") == 0 || strcmp(names, "Done") == 0)

{

break;

}

else

{

i++;

}

}

writeDatabase(names,grades, i);

}

void writeDatabase(char names,double grades,int size)

{

int count = 0;

ofstream outputFile;

outputFile.open("config.txt");

while(count < size)

{

outputFile << names << " " << fixed << setprecision(2) << grades << endl;

count++;

}

outputFile.close();

cout << endl;

}

void viewDatabase()

{

char names;

double grades;

int size = 0;

bool result = loadDatabase(names, grades, size);

int count = 0;

double sum = 0;

double average = 0;

if(!result)

{

cout << endl;

cout << "Error reading database file..." << endl;

cout << endl;

cin.get();

}

else

{

while(count < size)

{

cout << names << " " << fixed << setprecision(2) << grades << endl;

sum += grades;

count++;

}

average = sum / size;

cout << endl;

cout << " The average grade in the database is: " << fixed << setprecision(2) << average << endl;

cout << endl;

cin.get();

}

}

void modifyGrade()

{

char names;

double grades;

int size = 0;

bool result = loadDatabase(names, grades, size);

int count = 0;

char tempName;

double tempGrade = 0;

bool found = false;

int i = 0;

bool validGrade = true;

bool rewrite = false;

if(!result)

{

cout << endl;

cout << "Error reading database file..." << endl;

cout << endl;

cin.get();

}

else

{

while(count < size)

{

cout << names << " " << fixed << setprecision(2) << grades << endl;

count++;

}

while(validGrade)

{

cout << endl;

cout << "Enter the name of the student to modify and their new grade: ";

cin >> tempName;

cin >> tempGrade;

if(tempGrade < 0 || tempGrade > 100)

{

cout << "You entered an invalid grade." << endl;

cout << endl;

}

else

{

validGrade = false;

}

}

while(i < size && !found)

{

if(strcmp(names, tempName) == 0)

{

found = true;

grades = tempGrade;

rewrite = true;

}

else

{

i++;

}

}

if(!found)

{

cout << endl;

cout << endl;

cout << "The student's name was not found!" << endl;

cout << endl;

cin.get();

cin.get();

}

}

if(rewrite)

{

writeDatabase(names,grades, size);

cout << "Grade successfully updated!" << endl;

cin.get();

cin.get();

}

}

bool loadDatabase(char names,double grades, int& size)

{

bool result = true;

int counter = 0;

ifstream dbFile;

dbFile.open("config.txt");

if (!dbFile)

{

result = false;

}

else

{

while ( !dbFile.eof() && counter < MAX_NUM_STUDENTS)

{

dbFile >> names >> grades;

counter++;

}

if (dbFile.eof())

{

counter--;

}

}

size = counter;

dbFile.close();

return result;

}
Couldn't hide that in a spoiler or something?
Gtfo of this thread if you don't intend on reading code!
I'll stick to gamemaker <object class="emojione" data="https://resources.enjin.com/1489581540/themes/core/images/emojione/svg/1f613.svg?0" type="image/svg+xml" standby=":undecided:">:undecided:</object>
Jdawg , u soo into this .

but , Good Luck!