Thread: Console stops working when program is running.

  1. #1
    Registered User
    Join Date
    Mar 2016
    Posts
    36

    Console stops working when program is running.

    Since I am not getting any error I can't find what is wrong with the program.
    The text of the exercise is:
    "Create a class named StudentGrade. Include fields for a student ID number, studentname (use the Person class from Exercise 3), numeric test score, possible points, andletter grade.
    Create a constructor that requires the student ID, name, and test score, and allows a fourth parameter that holds possible test points. If a value for possible points isnot passed to the constructor, the possible points value defaults to 100. (If a 0 value ispassed for possible points, force possible points to 1 so that you do not divide by 0 whencalculating a grade.)
    The constructor calculates the student’s percentage (divides scoreby possible points) and assigns a letter grade based on: 90% and above (A), 80% (B), 70%(C), and 60% (D). Create a main()function that instantiates and displays enoughStudentGrade objects to test a variety of scores and possible point values"

    Code:
    #include <iostream>
    using namespace std;
    
    
    class Person
    {
        private:
            string lastName;
            string firstName;
            string zipCode;
        public:
            Person();
            Person(string , string , string);
            string getLastName();
            string getFirstName();
            string getZipCode();
            void setFirstName(string);
            void setLastName(string);
                
    };
    Person::Person()
    {
        lastName = firstName = zipCode = "X";
        
    }
    Person::Person(string lName, string fName, string zCode)
    {
        lastName = lName;
        firstName = fName;
        zipCode = zCode;
    }
    string Person::getLastName()
    {
        return lastName;
    }
    string Person::getFirstName()
    {
        return firstName;
    }
    string Person::getZipCode()
    {
        return zipCode;
    }
    void Person::setFirstName(string firstName)
    {
        this->firstName = firstName;
    }
    void Person::setLastName(string lastName)
    {
        this->lastName = lastName;
    }
    
    
    class StudentGrade
    {
        private:
            int idNum;
            int testScore;
            int possiblePoints;
            char letterGrade;
            Person Student;
        public:
            StudentGrade(int , string, int, int = 100);
            char getLetterGrade();
    };
    StudentGrade::StudentGrade(int idNum, string firstName, int testScore, int possiblePoints)
    {
        Student.setFirstName(firstName);
        this->idNum = idNum;
        if(possiblePoints = 0)
            possiblePoints = 1;
        if(testScore/possiblePoints >= 90)
            letterGrade = 'A';
        if(testScore/possiblePoints >= 80)
            letterGrade = 'B';
        if(testScore/possiblePoints >= 70)
            letterGrade = 'C';
        if(testScore/possiblePoints >= 60)
            letterGrade = 'D';
        
    }
    char StudentGrade::getLetterGrade()
    {    
        return letterGrade;
    }
    int main()
    {
        StudentGrade NI(21, "Nikola" , 63 , 100);
        cout << NI.getLetterGrade();
        return 0;
    }

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    While your program may not have any errors it does have several warnings you should fix:
    |=== Build: Debug in testcpp (compiler: gcc 6.1.0) ===|
    main.cpp||In member function ‘void Person::setFirstName(std::__cxx11::string)’:|
    main.cpp|44|warning: declaration of ‘firstName’ shadows a member of ‘Person’ [-Wshadow]|
    main.cpp|9|note: shadowed declaration is here|
    main.cpp||In member function ‘void Person::setLastName(std::__cxx11::string)’:|
    main.cpp|48|warning: declaration of ‘lastName’ shadows a member of ‘Person’ [-Wshadow]|
    main.cpp|8|note: shadowed declaration is here|
    main.cpp||In constructor ‘StudentGrade::StudentGrade(int, std::__cxx11::string, int, int)’:|
    main.cpp|66|warning: declaration of ‘possiblePoints’ shadows a member of ‘StudentGrade’ [-Wshadow]|
    main.cpp|59|note: shadowed declaration is here|
    main.cpp|66|warning: declaration of ‘testScore’ shadows a member of ‘StudentGrade’ [-Wshadow]|
    main.cpp|58|note: shadowed declaration is here|
    main.cpp|66|warning: declaration of ‘idNum’ shadows a member of ‘StudentGrade’ [-Wshadow]|
    main.cpp|57|note: shadowed declaration is here|
    main.cpp|70|warning: suggest parentheses around assignment used as truth value [-Wparentheses]|
    Next do you have a question or problem with this code? You really need to ask specific questions and thoroughly describe the problem.


    Jim

  3. #3
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    Should this not be in the C++ forum?

    if(possiblePoints = 0)
    Can you spot a logical error here?
    Double Helix STL

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    *moved to C++ programming forum*
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Mar 2016
    Posts
    36
    Quote Originally Posted by laserlight View Post
    *moved to C++ programming forum*
    Sorry guys I didn't even notice I posted it in the C forum ,apologies
    Quote Originally Posted by jimblumberg View Post
    While your program may not have any errors it does have several warnings you should fix:

    Next do you have a question or problem with this code? You really need to ask specific questions and thoroughly describe the problem.

    Jim
    I have a problem that the console just crashes when I compile and run the program , I don't get any error like you did in your compiler, so I don't understand what is wrong with the program. Also all those errors that you posted above seem to be from using the same member names in the public function as in the field class , but since I used "this->" it should really not be the problem?
    Quote Originally Posted by swgh View Post
    Should this not be in the C++ forum?

    Can you spot a logical error here?
    Yeah ofcourse it should not be assignment operator '=' but '==' it was really late here when I posted this , that is why I accidentaly posted it in the C forum .
    Last edited by ZeroesAndOnes; 08-12-2016 at 01:31 AM.

  6. #6
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    Code:
    this->
    Is just a pointer to the address in memory where each type is stored.
    AFAIK (and I don't know C++ that well), that would not stop the
    compiler displaying such warnings.

    In your constructor/copy constructor, you have already set the default state
    of each data member. Thus, these lines

    Code:
    void Person::setFirstName(string firstName)
    {
        this->firstName = firstName;
    }
    do not make sense to me. Your assigning the address of first name to
    an address that already exists in that location. Hence the warning "shadow".
    Again, I could be wrong, but it's just theory.
    Double Helix STL

  7. #7
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Is just a pointer to the address in memory where each type is stored.
    In the content shown
    Code:
    this->firstName = firstName;
    the this-> tells the compiler to use the member variable with the name of firstName.

    The warning is because the parameter has the same name as a member variable which is usually a bad idea because it hides the member variable with the same name. Even though the program is correct because the OP properly scoped the member variable, naming parameters the same as your member variables is considered a bad habit. I really recommend you stop naming your parameters the same as your member variables.

    Also when you're dealing with non-POD types like std::string IMO it would be better to pass the parameter by reference/const reference into your functions, to avoid the unnecessary and possibly costly copy operation.

    Code:
    void Person::setFirstName(const std::string& first_name)
    {
        firstName = first_name;
    }
    about the assignment

    By the way this function is setting the first name to a new value supplied by the parameter, not initializing the variable as in the constructor.

    I have a problem that the console just crashes when I compile and run the program , I don't get any error like you did in your compiler, so I don't understand what is wrong with the program.
    Then you need to see about increasing your compiler warning level. Your compiler should be able to warn you about the problems with your code. And note that the reason your program is crashing is because you are dividing by zero because of the problem pointed out by the last warning (main.cpp|70|warning: suggest parentheses around assignment used as truth value [-Wparentheses]| ). Also you may want to start compiling your program, without running, so you can see any and all warning messages. Remember just because the program compiles doesn't mean that it is warning free and warning should always be fixed, never ignore warnings.

    Jim

  8. #8
    Registered User
    Join Date
    Mar 2016
    Posts
    36
    Quote Originally Posted by jimblumberg View Post
    In the content shown
    Code:
    this->firstName = firstName;
    the this-> tells the compiler to use the member variable with the name of firstName.

    The warning is because the parameter has the same name as a member variable which is usually a bad idea because it hides the member variable with the same name. Even though the program is correct because the OP properly scoped the member variable, naming parameters the same as your member variables is considered a bad habit. I really recommend you stop naming your parameters the same as your member variables.

    Also when you're dealing with non-POD types like std::string IMO it would be better to pass the parameter by reference/const reference into your functions, to avoid the unnecessary and possibly costly copy operation.

    Code:
    void Person::setFirstName(const std::string& first_name)
    {
        firstName = first_name;
    }
    about the assignment

    By the way this function is setting the first name to a new value supplied by the parameter, not initializing the variable as in the constructor.


    Then you need to see about increasing your compiler warning level. Your compiler should be able to warn you about the problems with your code. And note that the reason your program is crashing is because you are dividing by zero because of the problem pointed out by the last warning (main.cpp|70|warning: suggest parentheses around assignment used as truth value [-Wparentheses]| ). Also you may want to start compiling your program, without running, so you can see any and all warning messages. Remember just because the program compiles doesn't mean that it is warning free and warning should always be fixed, never ignore warnings.

    Jim
    Thank you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 06-01-2015, 04:00 AM
  2. Replies: 6
    Last Post: 09-03-2013, 07:32 AM
  3. Program suddenly stops working.
    By KittenAqua in forum C Programming
    Replies: 5
    Last Post: 10-18-2011, 05:45 AM
  4. Program stops working as soon as I input value?..
    By darkmagic in forum C++ Programming
    Replies: 1
    Last Post: 03-08-2011, 02:18 AM
  5. Running a console program with maximum speed
    By arno-nyme in forum Linux Programming
    Replies: 27
    Last Post: 06-01-2008, 12:41 AM

Tags for this Thread