I have this code that uses gradebook and student files to create a gradebook and stores student names and grades. When launching the file is asks for a name for the gradebook and uses that information to do the rest of the program. I want to remove that, so it doesn't ask for a name for the gradebook, but instead just has a standard name for the gradebook and does the rest by reference. How would I go about doing this? Is there anyway to remove the enum global constants as well? Also any tips for improving the code or making it look better would be appreciated!
//Source.cpp
#include <iostream>
#include <string>
#include "Gradebook.h"
//Global declarations: Constants and type definitions only -- NO variables
enum choices
{
ADD, LIST, QUIT
};
//Function prototypes
void ChoiceMenu(choices menuChoice, Gradebook& gbook);
choices getChoice(bool& bquit);
int main()
{
//Variable declarations
std::string gname;
bool quit = false; // looping bool
//Program logic
std::cout << "Please enter a name for your gradebook with no spaces: " << std::endl;
std::cin >> gname;
Gradebook g1(gname);
do
{
std::cout << "List of Options:\nAdd (A):\nList (L):\nQuit (Q):\nChoice:";
ChoiceMenu(getChoice(quit), g1); // gets user choices, flips bool on QUIT, and gives access to each feature A, L, Q
} while (!quit); // quit is negated because in functions it is used in, it's more readable to have it become true rather than become false
//Closing program statements
system("pause");
return 0;
}
//Function definitions
void ChoiceMenu(choices menuChoice, Gradebook& gbook) // reference to make sure gradebook can be changed within scope of other functions
{
switch (menuChoice)
{
default:
break;
case ADD:
if (gbook.GetNumStudents() < 3)
{
std::string studentName;
std::cout << "Enter last name:" << std::endl;
std::cin >> studentName;
gbook.AddStudent(studentName);
}
else
{
std::cout << "There are already 3 students in the gradebook." << std::endl;
}
break;
case LIST:
if (gbook.GetNumStudents() == 0)
{
std::cout << "There are no students in the gradebook." << std::endl;
}
else
{
gbook.PrintAllRecords();
}
break;
case QUIT:
// getChoice already flips this bool, ending the loop. Leaving case to prevent ambiguity and misunderstanding.
break;
}
}
choices getChoice(bool& bquit)
{
char userChoice;
std::cin >> userChoice;
if (userChoice == 'a' || userChoice == 'A')
{
return ADD;
}
elseif (userChoice == 'l' || userChoice == 'L')
{
return LIST;
}
elseif (userChoice == 'q' || 'Q')
{
bquit = true;
return QUIT;
}
return choices();
}
do you want the same name every time or a standard pattern?
you stop asking user and hard code it in a constant, and if its a pattern append to that, eg:
const string pattern = "filenamepattern";
...
static int num = 1000;
string gname = pattern+stoi(num++)+".dat";
Gradebook g1(gname);
//first one will be filenamepattern1000.dat, second will be filenamepattern1001.dat .. etc
Ok So I changed my Source file a little bit. All of my "gbook." is getting undefined. Im not sure how to identify it so it runs correctly. Is there a problem with how I set up my switch?
#include <iostream>
#include <string>
#include "Gradebook.h"
int main()
{
//Variable declarations
const std::string gname = "Gradebook.dat";
Gradebook g(gname);
char userChoice;
while (1)
{
std::cout << "List of Options:\nAdd (A):\nList (L):\nQuit (Q):\nChoice:";
switch (userChoice) // reference to make sure gradebook can be changed within scope of other functions
{
std::cin >> userChoice;
default:
std::cout << "That is not a valid option, please enter 'A' to add a student, 'L' to list current students, or 'Q' to quit the program." << std::endl;
break;
case'A':
if (userChoice == 'a' || userChoice == 'A' || gbook.GetNumStudents() < 3)
{
std::string studentName;
std::cout << "Enter last name:" << std::endl;
std::cin >> studentName;
gbook.AddStudent(studentName);
}
else (userChoice == 'a' || userChoice == 'A' || gbook.GetNumStudents() > 3);
{
std::cout << "There are already 3 students in the gradebook." << std::endl;
}
break;
case'L':
if (userChoice == 'l' || userChoice == 'L')
{
gbook.PrintAllRecords();
}
else (gbook.GetNumStudents() == 0);
{
std::cout << "There are no students in the gradebook." << std::endl;
}
break;
case'Q':
if ((userChoice == 'q' || 'Q'))
{
std::cout << "Thank you for using the Gradebook! Now exiting.." << std::endl;
// getChoice already flips this bool, ending the loop. Leaving case to prevent ambiguity and misunderstanding.
exit(1);
}
}
}
}
Nothing in your code declares anything called gbook. On line 9, you declare something called g. Is it supposed to be that?
EDIT: I have to ask - why didn't you look for that? Surely, if you get an error saying a symbol is undefined, the very first thing you would think of doing is looking for the definition, and confirming whether there is or ism't one?