input validation, kinda

This is a discussion on input validation, kinda within the C++ Programming forums, part of the General Programming Boards category; Hey there, i have this following code i'd like some feedback from if possible(comments, critism, tips) I'm a newb with ...

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    27

    input validation, kinda

    Hey there,

    i have this following code i'd like some feedback from if possible(comments, critism, tips)

    I'm a newb with C++ and currently trying to grasp the concepts of OOP

    two files, main.cpp and a header file:

    source file:

    Code:
    /* 
     * File:   main.cpp
     * Author: joe
     *
     * Created on September 15, 2008, 4:44 PM
     */
    
    #include "scoreLib.h"
    
    int main()
    {
        float input;
        
    
        std::cout << "Please enter a score: ";
        if(std::cin >> input)
        {
            Score student(input);
            student.gradeResult();
            std::cout << "Your Score is: " << input << std::endl;
        }
        else
        {
            std::cout << "Please enter a numeric value" << std::endl;
        }
        
    return 0;
    }
    header file:

    Code:
    /* 
     * File:   scoreLib.h
     * Author: joe
     *
     * Created on September 15, 2008, 4:44 PM
     */
    
    #include <iostream>
    #ifndef _SCORELIB_H
    #define	_SCORELIB_H
    
    class Score
    {
        
        float *numGrade;
    
        public:
    
        Score(float);
        ~Score();
        
        void gradeResult(){
            if((*numGrade >= 95) && (*numGrade <= 100))
            {
                std::cout << "Merit!\n";
            }
            else if((*numGrade >= 80) && (*numGrade <= 94))
            {
                std::cout << "Pass!\n";
            }
            else if((*numGrade >= 65) && (*numGrade <=79))
            {
                std::cout << "Partial Resit\n";
            }
            else if((*numGrade >= 50) && (*numGrade <= 64))
            {
                std::cout << "Full Resit\n";
            }
            else if((*numGrade >= 0) && (*numGrade <= 49))
            {
                std::cout << "Fail!\n";
            }
            else
            {
                std::cout << "Sorry enter a number between 1 and 100\n";
            }
        }
    };
    
    Score::Score(float defaultScore)
    {
        numGrade = new float;
        *numGrade = defaultScore;
    }
    
    Score::~Score()
    {
        delete numGrade;
    }
    
    #endif	/* _SCORELIB_H */
    I'm currently trying to fix this bug where by if you entered say for instance 3e3, it will still exectute:
    Code:
    std::cout << "Your Score is: " << input << std::endl;
    which it shouldnt, but am having some difficulty trying to figure out why it does this, help MUCH appreciated, thanks in advance

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    I prefer the following technique which you can adapt to your program:
    Code:
    while (!(std::cin >> value) || std::cin.get() != '\n')
    {
      std::cin.clear();
      std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
      // error message and re-prompt here
    }
    Notes:
    1. You need to #include <limits> for numeric_limits and <ios> (I think) for streamsize.
    2. Instead of std::numeric_limits<std::streamsize>::max(), you can use some large number instead (like 1000). That value is the number of bad characters to ignore, so as long as nobody types in more than 1000 bad characters it should be fine.
    3. This version includes the check for std::cin.get() != '\n'. This is necessary for your example of 3e3, since the std::cin >> value code happily reads in the 3, but the cin.get() call finds that there is garbage after the input which makes the whole thing invalid.
    4. The std::cin.get() also has the benefit of clearing the trailing newline, so that if you use this technique for all cin >> input and were also using getline, you wouldn't need the extra ignore() call.
    5. Putting that whole thing into a separate utility function is not a bad idea (although it's unnecessary for you right now): Need help looping this switch statement
    Last edited by Daved; 09-15-2008 at 12:55 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Printing Length of Input and the Limited Input
    By dnguyen1022 in forum C Programming
    Replies: 33
    Last Post: 11-29-2008, 03:13 PM
  2. Unable to enter additional input after validation
    By deleted_user in forum C Programming
    Replies: 22
    Last Post: 11-20-2008, 04:09 PM
  3. large program code ,please help
    By Ash1981 in forum C Programming
    Replies: 14
    Last Post: 01-30-2006, 05:16 AM
  4. Input Validation Question
    By zackboll in forum C Programming
    Replies: 14
    Last Post: 10-12-2004, 12:05 AM
  5. need help with some input
    By blindleaf in forum C Programming
    Replies: 2
    Last Post: 03-16-2003, 12:50 PM

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