I'm fairly new to using C++, so I'm having some issues -->
I'm sure I could do it another way simply using a function to check if the teacher wants to add another student. However, I believe this way should work. I ran into an error tho. The loop continues to iterate forever, printing "Would you like to add another student: y/n?". I thought it might be from leftover inputs being passed thru cin >> instructorChoice.
But if that was the case, shouldn't it stopped at the other cin(s)? Since it'll be meet them at one point, and again.
I also have another issue, I'm not sure how to print object attributes (I believe that's how you refer to them?), e.g. person.name(). Though I'm sure I'll figure that one out later, not much of a problem. -->My print function in the header file.
I have a main file and a header file for my class. I'm not sure if that's the best way to go about it. I know it's a small program but I wanted to try getting into the habit of better file management while programming.
I'd appreciate any input. Thanks in advance.
/*
Simple "Class" Project 1:
This program allows a teacher to add a student to a list of student,
once she/he is done. The program prints all the students in the list
along with their grades. I'm testing the use of header files by defining
my class and its methods there.
- Me
*/
#include <vector>
#include "student.h" //include files for student class
//includes <iostream>, <string>, namespace
int main() {
//var declarations
string studentName;
int studentGrade;
char instructorChoice;
int i;
//delcare student vector
vector<Student> studentList;
//take the initial input
cout << "Would you like to add a new student: y/n?" << endl;
cin >> instructorChoice;
//run until no more students need to be addded
while (instructorChoice == 'y') {
Student student; //create new student info
//prompt for user info
cout << "Student Name: ";
cin >> studentName;
cout << "Student Grade: ";
cin >> studentGrade;
student.SetName(studentName); //setName
student.SetGrade(studentGrade); //setGrade
studentList.push_back(student); //append to list of students
//notify teacher!
cout << student.GetName() << " has successfully been added!" << endl;
//ask if they would like to add another student
cout << "Would you like to add another student: y/n?" << endl;
cin >> instructorChoice;
}
//print all
for (i = 0; i < studentList.size(); ++i) { //iterate through object vector
studentList[i].PrintStudents();
}
return 0;
}
/*
Student Class Declaration
*/
#include <iostream>
#include <string>
using namespace std;
class Student {
public:
//set
void SetName(string studentName);
void SetGrade(char studentGrade);
//get
string GetName();
int GetGrade();
//print
void PrintStudents();
private:
string name;
char grade;
};
// public function definitions
//set
void Student::SetName(string studentName) { //Set new student name
name = studentName;
}
void Student::SetGrade(char studentGrade) { //Set new student grade
grade = studentGrade;
}
//get
string Student::GetName() { //return the student name
return name;
}
int Student::GetGrade() { //return the student grade
return grade;
}
//print
void Student::PrintStudents() { //Print student name and grade
cout << "Student: " << name << " Grade: " << grade << endl;
}
I tried using cin.ignore();
and cout << flush;
Pretty much anything I can find to clear any leftover buffers/inputs. Didn't work.
Not sure what the issue exactly is.
I read your code.it's good for begin.
The reason for the infinite loop is that grade
type in your class is char
but in main function
is int
.
some wrong in clean code:
write include in all file don't use nested.
write function for cout << "Would you like to add a new student: y/n?" << endl; cin >> instructorChoice;
. because is duplicate.
your print is not bad but you can write string get_student_data()
and can use operator overloading(operator<<
)