Exercise 1: compile a template function input that requires the user to enter a non-negative number.

Source: Internet
Author: User

// Write a template function I N P U T. It requires the user to enter a non-negative number and verifies that the number entered by the user is
// No is equal to or greater than 0. If not, it indicates that the input is invalid and you need to enter a new number. When the function is not
// You should be given three chances before exiting successfully. If the input is successful, the function should return the number as a reference parameter.
// If the input is successful, the function returns true; otherwise, f a l s e is returned. Test the function on the computer.

# Include <stdio. h>

 

Template <class T>
Bool input (T &)
{
If (A <0)
{
Printf ("invalid input! /N ");
Return false;
}
Else
{
Printf ("right input! /N ");
Return true;
}
}

 

Void main ()
{
Int iinput, I = 0;
Printf ("Please input an nonnegative number :");
Scanf ("% F", & iinput );
I ++;
While (! Input (iinput ))
{
If (I> = 3)
{
Printf ("You have tried for three times, now you shoshould exit! /N ");
Break;
}
Printf ("Please input again :");
Scanf ("% F", & iinput );
I ++;
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.