The problem is to create a console-based program that prompts the user for name, student number, course, and grade (50-100 whole number only). After the input, a message should be displayed depending on the grade. The program should display an error message and terminate immediately if there is an invalid character input in the name, and/or the input in the grade does not fall in the range (50-100).

Additional Requirements
* create a class for student, complete with attributes
* create a method display for class student, which will be used to output the message. The method display should accept 1 parameter (string).

Sample Program Run:
A+ : 95 – 100 A : 90 – 94
B+ : 85 – 89 B : 80 – 84
C+ : 75 – 89 F : 74 and below
Enter Name: Dino Raptor
Enter Student Number: 2009-12345
Enter Course: MATH 11
Enter Grade: 95
Output Message: Congratulations Dino Raptor! You got A+ for MATH 11!

Here is my code so far:

#include <iostream> // Basic C++ I/O classes
#include <string> // C++ string class
#include <cctype>
#include <stdlib.h>

using namespace std; // Use the std (standard) namespace

class Student {   //student class
   private:
      string name;
      string student_no;
      string course;
      int grade;
   public:
      string getName() const;    //class member
      void setName(string);
      string getStdno() const;      //class member
      void setStdno(string);
      string getCourse() const;      //class member
      void setCourse(string);
      int getGrade();
      int setGrade(int n);
};

   string Student::getName() const
   { return name; }

   void Student::setName(string s)
   { 
      if (s.length() == 0)
         name = "No name assigned";
      else
         name = s; 
   }

   string Student::getStdno() const
   { return student_no; }

   void Student::setStdno(string h)
   { 
      if (h.length() == 0)
         student_no = "No student_id assigned";
      else
         student_no = h;
   }

   string Student::getCourse() const
   { return course; }

   void Student::setCourse(string c)
   { 
      if (c.length() == 0)
         course = "No course assigned";
      else
         course = c;
   }

   int Student::getGrade()
   { return grade; }

    int Student::setGrade(int n)
   { 
      if (n >= 95 && n <= 100)     //print corresponding letter grade
           string m = "A+ ";
      else if (n >= 90 && n <= 94)
           cout << "A ";
      else if (n >= 85 && n <= 89)
           cout << "B+ ";
      else if (n >= 80 && n <= 84)
           cout << "B ";
      else if (n >= 75 && n <= 79)
           cout << "C+ ";
      else if (n >= 50 && n <=74)
           cout << "F ";
      else
           exit(2);
   }

void setDisp_info(Student&);
void getDisp_info(Student&);

bool InputMatches(string strUserInput, string strTemplate)
{
      if (strTemplate.length() != strUserInput.length())
      return false;

      // Step through the user input to see if it matches
      for (unsigned int nIndex=0; nIndex < strTemplate.length(); nIndex++)
      {
          switch (strTemplate[nIndex])
          {
          case '#': // match a digit
               if (!isdigit(strUserInput[nIndex]))
                  return false;
               break;
          case '?': // match anything
               break;
          default: // match the exact character
               if (strUserInput[nIndex] != strTemplate[nIndex])
                  return false;
          }
      }

      return true;
}

int main ()
{
   Student p1;
   setDisp_info(p1);
   getDisp_info(p1);
   return 0;
}

void setDisp_info(Student& pers)
{
   string str;
   string h;
   string course;
   string grade;
   int n;
   string m;

while (1)
{
   cout << "Enter Name: ";       //get student name
   getline(cin,str);
   pers.setName(str);

   bool bRejected=false; // check for errors

     /* check each character in the string until we either hit
      the end of the string, or we rejected a character*/
     for (unsigned int nIndex=0; nIndex < str.length() && !bRejected; nIndex++)
     {
          // If the current character is an alpha character, continue
          if (isalpha(str[nIndex]))
          continue;

          // If it is a space, continue
          if (str[nIndex]==' ')
          continue;
 
          // Otherwise reject input and exit
          bRejected = true;
          exit(2);
      }

      // If the input has been accepted, exit the while loop
      break;
}

while(1)
   {
   cout << "Enter Student Number ####-#####: ";        //get student number
   getline(cin,h);
   pers.setStdno(h);
   if (InputMatches(h, "####-#####"))
      break;
   }

   cout << "Enter Course: ";       //get student course
   getline(cin,course);
   pers.setCourse(course);

   cout << "Enter Grade: ";       //get student grade
   cin >> n;
   pers.setGrade(n);

}

void getDisp_info(Student& pers)          //print output
{
   cout << "Congratulations " << pers.getName() << "!" << endl;
   cout << "You got " << pers.getGrade();
   cout << " for " << pers.getCourse() << endl;
}

the problem is when i run the program i get this sample message:

congratulations Dino Raptor! You got 37879712 for Math 11

please tell me what i'm missing? thanks a lot

I suggest you hand-trace your program's execution. I see a couple things:

  1. Your class does not have a constructor. it's relying on the compiler-provided default which does not appear to be sufficient for this class.
  2. Because you don't have any constructors, your Student member values are not being initialized properly.
  3. You aren't handling your grade-related variable(s) correctly. You should take a closer look at your usage. You never store anything to them, yet you expect them to have correct values when you output them.

Edited 6 Years Ago by Fbody: n/a

thank you for your feedback. i know the problem lies within the grade-related function. i've tried to tweak my program but i got an error message:
"assignment to int from const char lacks a cast"

anyway, thank you for your reply. it's really helpful.

This doesn't actually print anything.

if (n >= 95 && n <= 100) //print corresponding letter grade
    string m = "A+ ";

Also, you can't just assign a string like "A" to the member variable grade, as I suspect you tried to do, since grade is an int. I'd either store the variable as an int and support another get function called getGradeAsLetter() or something; or else just store the variable as a string.

The call to exit(2) in setDisp_info() will exit the program right then and there. I'm not sure this is what you want.

This question has already been answered. Start a new discussion instead.