cout <<"This program would generate a report to display the loan payment detail.\n";
cout <<"Please press enter to continue ...........\n";
cin.get(ch);
cout <<"Please enter your loan amount , RM";
cin >> loan;
cout <<"Please enter your loan interest , RM";
cin >> interest ;
getch();
return 0;
}
Is there any way to limit the enterd values for loan or interest ?For example if i enter the values for loan in alphabet form the statement of "Please enter the values in number!" would prompt out.
The cin >> loan; waits for an integer, if you enter something different (like a character) then it return error and false. So you can have if( !( cin >> loan ) ) to check if the cin stream is ok. If it is not you have to clear the stream from errors using the clear() method and then erase all the characters from the stream using the ignore() methode.
Here is an example:
1 2 3 4 5 6 7 8 9 10 11 12 13
#include <iostream>
#include <limits>
usingnamespace std;
int main(){
int temp;
cout << "Give value (integer): ";
while( ! ( cin >> temp ) ){
cout << "That was not an integer...\nTry again: ";
cin.clear();
cin.ignore( numeric_limits<streamsize>::max(), '\n' );
}
}
The limits header is for the numeric_limits<streamsize>::max() that returns the maximum amount of characters a stream can handle.
cin.ignore( numeric_limits<streamsize>::max(), '\n' );
Actally ignores all the characters in the stream, until a new line is found.