Thread: How to make blank entry = default

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    26

    Question How to make blank entry = default

    Hi!

    Just wondering, how to turn a normal "Enter" response to a default entry?

    i.e.

    Code:
    float number1;
    cin Input number: ;
    if (number1 = (blank entry or "Enter"){
    number1 = 0.02;
    }
    Thanks.

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Use std::getline to read a line of input instead of reading the integer directly. If the line is empty, use a default value, else parse the integer from the line.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >Just wondering, how to turn a normal "Enter" response to a default entry?
    First read the input into a string first. At that point, there are several different ways to proceed. Here is one method:
    Code:
    	std::string snum;
    	double num;
    
    	std::cout << "Enter a number (or press <ENTER> for default):";
    	std::getline(cin, snum);
    
    	std::istringstream converter(snum);
    	converter >> num;
    
    	if (!converter)
    	{
    		if (converter.eof())
    		{
    			std::cout << "No value was entered.  A default value will used." << endl;
    			num = .02;
    		}
    	}
    	std::cout << num << std::endl;

  4. #4
    Registered User
    Join Date
    Jul 2008
    Posts
    26

    Alrighty

    Quote Originally Posted by swoopy View Post
    >Just wondering, how to turn a normal "Enter" response to a default entry?
    First read the input into a string first. At that point, there are several different ways to proceed. Here is one method:
    Code:
    	std::string snum;
    	double num;
    
    	std::cout << "Enter a number (or press <ENTER> for default):";
    	std::getline(cin, snum);
    
    	std::istringstream converter(snum);
    	converter >> num;
    
    	if (!converter)
    	{
    		if (converter.eof())
    		{
    			std::cout << "No value was entered.  A default value will used." << endl;
    			num = .02;
    		}
    	}
    	std::cout << num << std::endl;
    Sweet, thank you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. trying to make a KenGen ( for a game tool )
    By lonewolfy in forum C# Programming
    Replies: 4
    Last Post: 03-28-2007, 08:23 AM
  2. Pls repair my basketball program
    By death_messiah12 in forum C++ Programming
    Replies: 10
    Last Post: 12-11-2006, 05:15 AM
  3. Windows XP and Debian GRUB
    By kawk in forum Tech Board
    Replies: 5
    Last Post: 03-26-2006, 06:09 PM
  4. Win32 Common Controls in C++, how do i make and use them?
    By C+noob in forum Windows Programming
    Replies: 6
    Last Post: 01-09-2006, 11:53 AM
  5. constructors
    By shrivk in forum C++ Programming
    Replies: 7
    Last Post: 06-24-2005, 09:35 PM