class - I get empty string when trying to access a field inside a method in C++ -


following files:

student.cpp

#include <iostream> #include <vector> #include <ctime>      // time() #include <cstdlib>    // srand(), rand() #include <algorithm>  // min_element(), max_element(), sort()  /**  * class student holding single student's record.  */ class student { // these not accessible else private:   // holds name of student.   std::string name_;   // roll no. of student.   int number_;   // vector hold grades.   std::vector<int> grades_;   // number of courses taken student.   const int num_courses_;   // alphabets.   char alpha[63] = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789";   // returns random number   int rand_gen(int min, int max);    // generates random name.   std::string gen_name();   // generates random roll number.   int gen_number();   // generates grade.   int gen_grade();   public:   // constructor function initiates name ,   // number of student.   student();   // << operator overloading.   friend std::ostream& operator<<(     std::ostream& os, const student& s);   // prints grades of student.   void print_grades(std::ostream& os) const;   // prints average.   double compute_average(); };  /** constructor **/ student::student() :   name_(gen_name()), number_(gen_number()), num_courses_(5) {   gen_grade(); }  /** overloading '<<' operator **/ std::ostream& operator<<(   std::ostream& os, const student& s) {   os << "name = " << s.name_      << ", number = " << s.number_;   return os; }  /** print grades **/ void student::print_grades(std::ostream& os) const {   (int = 0; < num_courses_; ++i) {     os << grades_[i] << ", ";   } }  /** generates random numbers between min , max **/ int student::rand_gen(int min, int max) {   return std::rand()%(max - min + 1) + min; }  double student::compute_average() {   int sum = 0;   // assuming no 1 manipulate number of courses   (int g : grades_)     sum += g;   // change return sum / grades_.size(); incase going serious.   return sum / 5; }   // generates random name. std::string student::gen_name() {   // results in empty string printing.   std::cout << this->alpha << std::endl;   int size = this->rand_gen(6, 12);   std::string rand_name = "";   (int = 0; < size; ++i)   {     rand_name += this->alpha[this->rand_gen(0, 61)];     std::cout << rand_name;   }   return rand_name; }  // generates roll number students. int student::gen_number() {   // produce random number between 201100000   // , 201600000   return this->rand_gen(201100000, 201600000); }  // generates grade. int student::gen_grade() {   (int = 0; < num_courses_; ++i) {     // generate random number , push grades vector.     grades_.push_back(this->rand_gen(70, 100));   } } 

and main.cpp:

#include <iostream> #include "student.cpp" #include <ctime>      // time() #include <cstdlib>    // srand(), rand() #include <algorithm>  // min_element(), max_element(), sort()  int main() {   student *s1 = new student();   std::cout << *s1 << std::endl;   std::cout << s1->compute_average() << std::endl;   return 0; } 

when run main.cpp is

// have empty line on terminal here name = , number = 201115456 88 

actually get_name() method should output name, not able access alpha char array. have tried cout - ing char inside get_name(), that's causes emty line in output. not expect. please help. fyi total novice, know on coded without emphasis on basics.

in shortest words - initialization order. in constructor, when start initialization of name_, call gen_name() uses alpha, which... has not been initialized yet. order of initialization of members same, order of declaration. shortest solution - move declaration of:

// alphabets. char alpha[63] = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789"; 

at beginning of members list (at least before name_ declaration).


Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

thorough guide for profiling racket code -