Ok new here and trying to pick up everything again since donkeys yrs in army.
Newbie qns #001.
Below is a simple thingy i made that takes in input from the user, 1 to 100.
A <if, else> to check user input if it's valid within 1 to 100 range and a <do, while> loop to loop when invalid entry is made.
The problem is that if user enter letters in, the program just go haywire. Keep on looping the <do, while> without requesting a input from user.
I tot i have it covered in the <if,else> ... don't understand what happen.
Any enlightment ?
*side note*
What should i use? stdio.h or iostream for input and output. I have nightmares with printf and scanf last time. Still have some phobia with it.
#include "stdafx.h"#include "iostream.h"
int getNumber();
int main(int argc, char* argv[]){ int value01;
printf("Hello User!\n"); value01 = getNumber(); cout << "The number entered is: " << value01 << endl; return 0;}
int getNumber(){ bool getNumber_tf=false; int getNumber_int; do { cout << "Please enter a number from 1 to 100:" << endl; getNumber_int = 0; cin >> getNumber_int;
if(getNumber_int > 0 && getNumber_int < 101) { getNumber_tf = true; } else { getNumber_tf = false; cout << "Invalid entry" << endl; }
}while(getNumber_tf==false);
return getNumber_int;}
The melody of logic will always play out the truth. ~ Narumi Ayumu, Spiral
Apparently you have not done enough reading and research into using cin. There are plenty of tutorial sites on C++, that deals with the correct use of cin, that can be easily located on the Internet.
There are a number of things about cin you have to know. Firstly, it is an system-defined Input Stream object that performs a formatted and buffered input from keyboard when used with the extraction operator (>>). To illustrate this point, let's assume you have two statements below:
int intnum;
cin >> intnum;
The cin will format the keyboard input as an integer since intnum is defined as an integer; when the input is not recognised as integer, this process will fail. An internal error bit will be set within cin. Once in the error state, cin will not be able to perform any input until it is clear of the error state. When this is done the non-numeric character that caused the error must be removed from the input buffer before cin can correctly interpret numeric input again. The purpose of the std::get() function is meant for this purpose.
I have made some modification to the int getNumber( ) function below to do what I just described above:
int getNumber(){ bool getNumber_tf=false; int getNumber_int; do { cout << "Please enter a number from 1 to 100:" << endl; getNumber_int = 0; cin >> getNumber_int;// formatted input from keyboard
if(cin.fail() || getNumber_int <= 0 || getNumber_int > 100) { cout << "Invalid entry" << endl; cin.clear(); // Clear the error state cin.get(); // remove the erroneous input char from buffer } else getNumber_tf = true;
=================================================================================================
I hope this will clarify your confusion about using cin.
Cheers.