This is the full program code. Its still unfinished but if I choose option 1 and enter the names. It goes to keepGoing function but it won't let me enter Y or N. It goes straight to the end of the program.
Why ?
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
usingnamespace std;
// Declare a structure for the record.
struct Students
{
char name;
};
// FUNCTION PROTOTYPES
void addStudent(string[], int);
void stuAttendance();
void stuReport();
void showMenu();
void keepGoing();
int main ()
{
//Open file
//fstream attendance("Student Attendance.txt", ios::out | ios::in);
//VARIABLES AND CONSTANTS
//variables
int numStudents;
int menuChoice;
int size;
string place;
int num;
//constants
constchar ADD_STUDENT = 1,
ATTENDANCE = 2,
REPORT = 3;
//ARRAYS
string stuInfo[numStudents];
// Introduction
cout << "Welcome to the attendance calculator: " << endl << endl;
// Accept number of students
cout << "\nPlease enter the number of students you have between 2 and 30 \n"
<< "or -1 to quit: ";
cin >> numStudents;
if (numStudents == -1)
{
cout << "!!Exiting program!!\n";
system("pause");
return 0;
} elseif (numStudents < 2 || numStudents > 30)
{
cout << "Please enter a number between 2 and 30\n";
cin >> numStudents;
}
cout << numStudents << "\n";
// GET OPTIONS FROM USER
showMenu();
cin >> menuChoice;
//Switch for menuChoice
if (menuChoice == 1)
addStudent(stuInfo, numStudents);
elseif (menuChoice == 2)
stuAttendance();
elseif (menuChoice == 3)
stuReport();
keepGoing();
//cout << "Would you like to continue?";
//cin >> place;
//cout << "Enter a number please work: ";
//cin >> num;
/*if (proceed == 'Y' || proceed == 'y')
{
showMenu();
}
else
cout << "Thank you for using Attendance Program.";*/
system("pause");
return 0;
}
//*************************************************************
//showMenu function shows the choices for the program to run *
// choices of S A or R. *
//*************************************************************
void showMenu()
{
cout << "Please make a choice from the menu.\n" << endl;
cout << "1. Add a student\n"
<< "2. Enter attendance\n"
<< "3. See report\n"
<< ":: ";
}
//*******************************************************
// addStudent function allows user to enter a students *
// information into the system. *
//*******************************************************
void addStudent(string nStudent[], int size)
{
cout << "Welcome to addStudent" << endl;
cout << "\nPlease enter the student information: " << endl;
for (int index = 0; index < size; index++)
{
cout << "Enter the name of new student #" << (index+1)
<< ": ";
getline(cin, nStudent[index]);
cin.ignore();
}
}
//******************************************************
// stuAttendance function alow a user to add students *
// attendance record to the report. *
//******************************************************
void stuAttendance()
{
cout << "Welcome to stuAttendance." << endl;
}
//*****************************************************
// stuReport function displays a students attendance *
// record for the user to view *
//*****************************************************
void stuReport()
{
cout << "Welcome to stuReport." << endl;
}
//******************************************************
// keepGoing function asks the user if they would like *
// to continue with the program *
//******************************************************
void keepGoing()
{
string place;
cout << "Would you like to continue? Please enter Y or N." << endl;
cin >> place;
}
line 10: This allocates only a single character for name.
line 40: How many instances of stuInfo do you think are allocated here?
Hint: numStudents is uninitialized.
PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post. http://www.cplusplus.com/articles/jEywvCM9/
thanks for the code tags info. I was wondering about that. The user initializes the number of students. The programs not complete I just cant figure out why it will let me allow the Y or N before the menu loop, but if I place it after the loop it doesn't allow it. If I run the program as its written now select 2 students enter their name it asks to continue Y or N and goes straight to the end. I added the function to see if it would help. But that didn't work either.
@AbstractionAnon
How does the compiler know how many students the user is going to enter?
@musicman74
Don't know but it works when I run it.
Then change your IDE to the one that does not allow such tricks, as they will work against your effort put in learning C++. Basically, your IDE by some additional plugin denies C++ standards and allows simplification, that would normally be an error, without you even noticing. (Code::Blocks?)