set validation

This is a discussion on set validation within the C++ Programming forums, part of the General Programming Boards category; hello all i am having a problem setting validation. i have written this code and it works. when i try ...

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    12

    set validation

    hello all i am having a problem setting validation. i have written this code and it works. when i try and put in validation with if statements it keeps crashing the program. i have no idea where to go from here. here is what i am trying to do:

    Modify my StudentGrade class that the "set" methods perform data validation. A student ID should be in the range of 10000-50000, and a grade should be in the range 0 - 100. Use a single "if" statement (using the && operator) in each "set" method.

    #include <iostream>
    using std::cout;
    using std::cin;
    using std::endl;

    #include <string>
    using std::string;
    using std::getline;
    Code:
    	class StudentGrade
      {
    	public:
    	//function that sets the student ID
    	void setStudentID( string number )
    	{
    		studentID = number; //store the course name in the object
    	}
    
    	//function that gets the student ID
    	string getStudentID()
    	{
    		return studentID; //returns the objects Student ID
    	}
    
    	void setTestScore( string score )
    	{
    		testScore = score;
    	}
    
    	string getTestScore()
    	{
    		return testScore;
    	}
    
    	void displayMessage()
    	{
    		cout << "Student " << getStudentID() << " has a score of " 			<< getTestScore() << endl;
    	}
    	private:
    	string studentID;
    	string testScore;
    };
    
    	int main()
       {
    	string studentIDnumber;
    	string studentTestscore;
    	StudentGrade myStudentID;
    	
    	if ( studentIDnumber < 10000 && >50000 )
    	cout<< "The number must be between 10000 - 50000. ";
    
    	else
    	cout << "Student ID is: " <<myStudentID.getStudentID() << endl;
    
    	cout << "\nPlease enter the Student ID number:" << endl;
    	getline( cin, studentIDnumber );
    	myStudentID.setStudentID( studentIDnumber );
    
    	cout << endl;
    
    	cout << "Student test score is: " <<myStudentID.getTestScore() << 	endl;
    
    	cout << "\nPlease enter the Student Test Score:" << endl;
    	getline( cin, studentTestscore );
    	myStudentID.setTestScore( studentTestscore );
    
    	cout << endl;
    
    	myStudentID.displayMessage();
    	return 0;
       }

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,681
    I would expect something like a student id and test scores would be integer values and not strings.

    Code:
    string studentIDnumber;
    string studentTestscore;
    StudentGrade myStudentID;
    	
    if ( studentIDnumber < 10000 && >50000 )
        cout<< "The number must be between 10000 - 50000. ";
    You can't test a string and an int type like this. Also, the general syntax of the test is incorrect. Testing like you are doing should be using logical or (||) instead of logical and (&&). It should be something like:

    Code:
    int studentIDnumber;
    ...
    if( studentIDnumber < 10000 || studentIDnumber > 50000 )
        cout<< "The number must be between 10000 - 50000. ";
    Make your data types ints instead of strings. Also, you need to initialize your values before you start testing them.
    I used to be an adventurer like you... then I took an arrow to the knee.

  3. #3
    Registered User
    Join Date
    Dec 2005
    Posts
    12
    the problem is i have to use and if statement with the && operator. that is what is confusing me.

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,681
    Code:
    if( studentIDnumber >= 10000 && studentIDnumber <= 50000 )
    {
        // Good data, do something
    }
    else
    {
        // Bad data
        cout<< "The number must be between 10000 - 50000. ";
    }
    Last edited by hk_mp5kpdw; 12-13-2005 at 07:37 AM.
    I used to be an adventurer like you... then I took an arrow to the knee.

  5. #5
    Registered User
    Join Date
    Dec 2005
    Posts
    12
    thanks, i must be a moron, i cant figure out how to rewrite the code to make it work. thanks for the help

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 01-13-2008, 01:14 AM
  2. The new FAQ
    By Hammer in forum A Brief History of Cprogramming.com
    Replies: 34
    Last Post: 08-30-2006, 10:05 AM
  3. problem with open gl engine.
    By gell10 in forum Game Programming
    Replies: 1
    Last Post: 08-21-2003, 04:10 AM
  4. Menu Item Caption - /a for right aligned Accelerator?
    By JasonD in forum Windows Programming
    Replies: 6
    Last Post: 06-25-2003, 11:14 AM
  5. Set default directory with GetTempDir?
    By Bajanine in forum Windows Programming
    Replies: 2
    Last Post: 05-04-2003, 11:36 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21