Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7739671
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T08:34:49+00:00 2026-06-01T08:34:49+00:00

I have run this program before and it worked fine. Then I added the

  • 0

I have run this program before and it worked fine. Then I added the “if” statements to the “set” methods and i started seeing very large numbers when I ran the program. What can I do to fix this problem or can someone enlighten me as to why this is happening?

class GradeBook{

public: 
    void setStudentID(int ID){

        if(10000 <= studentID && studentID <= 50000){

            studentID = ID;
        }
    }

    int getStudentID(){

        return studentID;
    }

    void setStudentGrade(int grade){

        if(0 <= studentGrade && studentGrade <= 100){

        studentGrade = grade;
        }
    }

    int getStudentGrade(){

        return studentGrade;
    }

    void displayMessage(){

        cout << "Student " << getStudentID() << " has a score of " << getStudentGrade() << endl;
    }

private:

    int studentGrade;
    int studentID;
};

int main(){

    int nameOfID;
    int nameOfGrade;
    GradeBook gb;

    cout << "Please enter a student ID: " << endl;
    cin >> nameOfID;
    gb.setStudentID(nameOfID);
    cout << "Please enter the student's grade: " << endl;
    cin >> nameOfGrade;
    gb.setStudentGrade(nameOfGrade);
    getchar();

    gb.displayMessage();
    getchar();
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T08:34:50+00:00Added an answer on June 1, 2026 at 8:34 am
    1. You have your comparisons wrong you meant.

      if(10000 >= studentID && studentID <= 50000)
      
    2. You don’t have else statements to make sure the variable is initialized, thus I’d change it to:

      if(10000 >= studentID && studentID <= 50000){
           studentID = ID;
      }
      else{
           studentID = 0; //or whatever value you want to mean invalid
      }
      

      That will hopefully fix your issues.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this problem when trying to run hello world program using android SDK.
Everytime I run the program, this mysterious error pops up saying that I have
This is my problem. I have a program that has to run in a
I have run into this problem before but never quite solved it. I have
I have run into this problem a few times and I'm not happy with
I have tryed to run this code in my console: script/plugin install git://github.com/apotonick/cells.git ...but
So I have run up against this problem a few times: I have some
I have a problem: if i run this test in NUnit ,it works Board
I have a problem when I run this snippet of code on the emulator
I have been working on a quick WinForms program. I found this SO question

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.