cin

How to make cin >> not convert float to integer?

可紊 提交于 2019-11-28 13:13:36
问题 I have the following simple code: #include <iostream> int main() { int a; std::cout << "enter integer a" << std::endl; std::cin >> a ; if (std::cin.fail()) { std::cin.clear(); std::cout << "input is not integer, re-enter please" <<std::endl; std::cin >>a; std::cout << "a inside if is: " << a <<std::endl; } std::cout << "a is " << a <<std::endl; std::cin.get(); return 0; } When I run the above code and input: 1.5 , it outputs: a is 1 . FYI: I compile and run the code with gcc 4.5.3. This means

C++ cin.fail() question

╄→гoц情女王★ 提交于 2019-11-28 12:42:44
When running the following code and enter a number, it works fine. But when entering a letter, the program enters an infinite loop, displaying "Enter a number (0 to exit): cin failed." My intent was to handle the cin fail case and prompt the user again. int number; do{ cout << "Enter a number (0 to exit): "; cin >> number; if(cin.fail()){ cout << "cin failed." << endl; cin.clear(); }else{ cout << "cin succeeded, " << number << " entered." << endl; } }while(number != 0); You need to clear the line from cin , using cin.ignore , in addition to clearing the stream state (which is what cin.clear

Getting user input in C++ [closed]

馋奶兔 提交于 2019-11-28 10:39:07
I am writing a program that allows a student to write a question and store that Question (or string) in a variable, can anyone please tell me the best way to get user input thanks for your answers and comments Formatted I/O; taken from Baby's First C++ : #include <string> #include <iostream> int main() { std::string name; std::cout << "Enter your name: "; std::getline(std::cin, name); std::cout << "Thank you, '" << name << "'." << std::endl; } This isn't quite satisfactory, as many things can (and thus will) go wrong. Here's a slightly more watertight version: int main() { std::string name;

c++, how to verify is the data input is of the correct datatype [duplicate]

淺唱寂寞╮ 提交于 2019-11-28 10:05:20
Possible Duplicate: how do I validate user input as a double in C++? I am new to C++, and I have a function in which I am wanting the user to input a double value. How would I go about insuring that the value input was of the correct datatype? Also, how would an error be handled? At the moment this is all I have: if(cin >> radius){}else{} I using `try{}catch(){}, but I don't think that would the right solution for this issue. Any help would be appreciated. Zeta If ostream& operator>>(ostream& , T&) fails the extraction of formatted data (such as integer, double, float, ...), stream.fail() will

Avoiding infinite loop when a char is enter in place of int

。_饼干妹妹 提交于 2019-11-28 08:17:04
问题 I'm doing a Banking System project and need to make sure that every input is valid(program has to be robust). If invalid input is given then user has to enter again. But when i have a variable of int type and user enters char type an infinite loop begins. For example: int i; cin>>i; If user enters a char infinite loop starts. How can i avoid it and ask user for an input again? Thanks 回答1: Here is another approach that might help; first writing to std::string and then going over all elements

Why isn't cin >> string working with Visual C++ 2010? [closed]

£可爱£侵袭症+ 提交于 2019-11-28 07:33:47
#include <iostream> using namespace std; int main() { string s; cin >> s; cout << "Hello World!"; } This isn't working. Why? Because you forgot to #include <string> You should #include <string> 来源: https://stackoverflow.com/questions/5343035/why-isnt-cin-string-working-with-visual-c-2010

C++ cin whitespace question

和自甴很熟 提交于 2019-11-28 06:46:21
问题 Programming novice here. I'm trying to allow a user to enter their name, firstName middleName lastName on one line in the console (ex. "John Jane Doe"). I want to make the middleName optional. So if the user enters "John Doe" it only saves the first and last name strings. If the user enters "John Jane Doe" it will save all three. I was going to use this: cin >> firstName >> middleName >> lastName; then I realized that if the user chooses to omit their middle name and enters "John Doe" the

Why does cin.clear() fix an infinite loop caused by bad input to cin?

时光总嘲笑我的痴心妄想 提交于 2019-11-28 06:31:23
问题 I've written a switch statement, and created a default which would simply say the user picked a bad option and repeat the input. I wanted to make sure that if there was an issue, it would clear the buffer first, so I used cin.sync(), but entering an 'a' for the input still caused an infinite loop. I added cin.clear() to clear the flags which gave me working code ... but my confusion is why it worked. Does cin.sync() not work if there is a fail flag? the statement follows, truncated for

Multiple inputs on one line

扶醉桌前 提交于 2019-11-28 06:22:15
I have looked to no avail, and I'm afraid that it might be such a simple question that nobody dares ask it. Can one input multiple things from standard input in one line? I mean this: float a, b; char c; // It is safe to assume a, b, c will be in float, float, char form? cin >> a >> b >> c; Yes, you can input multiple items from cin , using exactly the syntax you describe. The result is essentially identical to: cin >> a; cin >> b; cin >> c; This is due to a technique called "operator chaining". Each call to operator>>(istream&, T) (where T is some arbitrary type) returns a reference to its

difference between cin.get() and cin.getline()

天大地大妈咪最大 提交于 2019-11-28 03:45:04
问题 I am new to programming, and I have some questions on get() and getline() functions in C++. My understanding for the two functions: The getline() function reads a whole line, and using the newline character transmitted by the Enter key to mark the end of input. The get() function is much like getline() but rather than read and discard the newline character, get() leaves that character in the input queue. The book(C++ Primer Plus) that I am reading is suggesting using get() over getline() . My