validation entry

This is a discussion on validation entry within the C++ Programming forums, part of the General Programming Boards category; hello all, Im having some problems doing this simple program. Code: #include <iostream> using namespace std; void input(); float result,firstNum, ...

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    21

    validation entry

    hello all,
    Im having some problems doing this simple program.
    Code:
    #include <iostream>
    using namespace std; 
    
    void input();
    float result,firstNum, secondNum;
     
    int main(){
    	
    	input();
    	
    	result = firstNum + secondNum;
    	cout << "\n\t\t\t\t" << result << endl;
    	return 0;
    }
    
    void input(){
    	
    	char casting[255];
    	
    	cout << "\n\t\t\tEnter First Number: ";
    	cin >> casting;
    	firstNum = atof(casting);
    	
    	cout << "\n\t\t\tEnter Second Number: ";
    	cin >> casting ;
       	secondNum = atof(casting);
    }
    the reason Im using atof is because the program crashed if I typed anything else other than a number. Now it doesnt crash it just returns 0.

    What I'd like is actually validate the entry and return an error message if the entry is not a number.
    I've tried using something like...
    Code:
    while (strcmp(casting, "@*!#&")==1);
    	{
    		cout << "ERROR...only numbers\n";
    	}
    ...but maybe is not what I need to use (it doesnt work anyway).

    Is there any simple way to do it? thanks in advance.
    MSVC++

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Is there any simple way to do it?
    There is indeed.
    Code:
    #include <limits> // numeric_limits
    #include <ios>    // streamsize
    
    double get_double(){
      double d;
    
      while (!(cin>> d)) {
        cerr<<"Invalid number, try again: ";
        cin.clear();
        cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
      }
    
      return d;
    }
    
    void input(){
      cout << "\n\t\t\tEnter First Number: ";
      firstNum = get_double();
    	
      cout << "\n\t\t\tEnter Second Number: ";
      secondNum = get_double();
    }
    Last edited by Prelude; 06-28-2004 at 04:35 PM. Reason: I'll get it right eventually. :-(
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Apr 2004
    Posts
    21
    thanks for the reply

    a bit complicated too me; a few unfamiliar words.
    interesting though.
    MSVC++

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Code:
    //until input into d is valid, keep doing what's between the curly braces
    while (!(cin>> d)) 
    {
    	//let the user know there is a problem.
    	//could replace cerr with cout, but cerr is 
    	//technically better
    	cerr<<"Invalid number, try again: ";
     
    	//clear the fail bit of the state member of the cin object
    	cin.clear();
     
    	//ignore any remaining characters in the input 
    buffer or until newline char is found, whichever 
    comes first
       cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
     
      //another approach would be to do something like this:
     //cin.ignore(8000);
    }
    Prelude has given you the best approach. Even if you don't understand it all, you can use her approach as needed for data input validation until you learn what all the different parts mean. Alternatively, if you don't want to look that sophisticated, you can use the "alternatives" listed above and they should work the majority of the time, though Prelude's code will work at times when the slimmed down version won't.

  5. #5
    Registered User
    Join Date
    Apr 2004
    Posts
    21
    thanks

    shouldnt I put a cin >> firstNum ; somewhere?
    Like here maybe?
    Code:
    void input(){
      cout << "\n\t\t\tEnter First Number: ";
      cin >> firstNum ; 
      firstNum = get_double();
    MSVC++

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >shouldnt I put a cin >> firstNum ; somewhere?
    get_double takes care of reading the value for you, then it returns it so that you can assign it to firstNum. Everything you need is done here:
    Code:
    firstNum = get_double();
    But don't forget to define get_double as I did or it won't work as well.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Trouble writing to file using fwrite()
    By yougene in forum C Programming
    Replies: 4
    Last Post: 12-30-2008, 05:13 PM
  2. Immediate programming help! Please!
    By xMEGANx in forum C++ Programming
    Replies: 6
    Last Post: 02-20-2008, 12:52 PM
  3. Pls repair my basketball program
    By death_messiah12 in forum C++ Programming
    Replies: 10
    Last Post: 12-11-2006, 05:15 AM
  4. Binary Search Tree
    By penance in forum C Programming
    Replies: 4
    Last Post: 08-05-2005, 06:35 PM
  5. [C#] Validation class for events?
    By gicio in forum C# Programming
    Replies: 4
    Last Post: 01-03-2003, 12:19 PM

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