cin

cin >> fails with bigger numbers but works with smaller ones?

我与影子孤独终老i 提交于 2019-12-28 04:33:15
问题 Why does cin fail, when I enter numbers like: 1 3999999999 but it works for smaller numbers like: 1 5 ? int main() { int N, X; cout << sizeof(int); cout << "Please enter two numbers: "; cin >> N >> X; vector <int> numbers = vector<int>(); int currentNumber; cout << "Please enter list of numbers: "; for ( int i = 0; i < N; i++ ) { cin >> currentNumber; if (cin.fail()) cout << "Something sucks!"; numbers.push_back(currentNumber); } sort(numbers.begin(), numbers.end(), Compare(X)); cout << "The

How do I deal with the max macro in windows.h colliding with max in std?

北战南征 提交于 2019-12-27 12:05:47
问题 So I was trying to get valid integer input from cin, and used an answer to this question. It recommended: #include <Windows.h> // includes WinDef.h which defines min() max() #include <iostream> using std::cin; using std::cout; void Foo() { int delay = 0; do { if(cin.fail()) { cin.clear(); cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); } cout << "Enter number of seconds between submissions: "; } while(!(cin >> delay) || delay == 0); } Which gives me an error on Windows, saying

Using while(cin>>x) [duplicate]

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-25 05:25:23
问题 This question already has an answer here : Closed 7 years ago . Possible Duplicate: C++ cin whitespace question I'm having problem trying to understand this piece of code. I'd like to apologize if this question has already been answered but I didn't find it anywhere. I'm a beginner and its a very basic code. The problem is >> operator stops reading when the first white space character is encountered but why is it in this case it outputs the complete input string even if we have white spaces

How do you flush the contents of `std::cin` before an additional read from it?

戏子无情 提交于 2019-12-25 04:12:06
问题 I am trying to read a single character multiple times . The catch is that I need to prevent user errors. So for example: char arr[10]; for(int i = 0; i < 10; i++) { cin.get(arr[i]); } Where the inputs should be something like a, b, c, d, ... . But if someone were to enter ab for the first entry I want to capture the a and then ignore the b . I know about cin.ignore however I don't know how I would go about ignoring an arbitrary number of alphanumeric characters or symbols considering that I

getline(cin, string) not giving expected output

 ̄綄美尐妖づ 提交于 2019-12-24 19:17:02
问题 Using c++14. I have read many posts regarding the problem. If I run this code below, it jumps over the getline lines. #include <iostream> #include "main_menu.h" void MainMenu::AddTest() { std::string courseName = ""; std::string testName = ""; std::string date = ""; std::cout << "Enter course name: " << std::endl; std::getline(std::cin, courseName); std::cout << "Enter test name: " << std::endl; std::getline(std::cin, testName); std::cout << "Enter test date: " << std::endl; std::getline(std:

Cin.get() does not wait for a keypress even with cin.ignore() before it

不羁岁月 提交于 2019-12-24 17:35:09
问题 I am a somewhat new programmer practicing with this program below. For whatever reason, at the end of my program, cin.ignore() is being completely skipped by the compiler and moves straight onto cin.get(), but there was already a key-press before, so the compiler skips it altogether and finishes the program without waiting for a key-press. I've tried putting cin.get() and cin.ignore() in the switch case statement but the same error occurs. I've searched about this problem around the web and

Cin.get() does not wait for a keypress even with cin.ignore() before it

眉间皱痕 提交于 2019-12-24 17:35:06
问题 I am a somewhat new programmer practicing with this program below. For whatever reason, at the end of my program, cin.ignore() is being completely skipped by the compiler and moves straight onto cin.get(), but there was already a key-press before, so the compiler skips it altogether and finishes the program without waiting for a key-press. I've tried putting cin.get() and cin.ignore() in the switch case statement but the same error occurs. I've searched about this problem around the web and

Using cin.get() to discard unwanted characters from the input stream in c++

六月ゝ 毕业季﹏ 提交于 2019-12-24 17:04:20
问题 I am working on an assignment for my C++ class. The following code is given. The directions explain to enter a six character string and observe the results. When I do this, the second user prompt is passed over and the program ends. I am pretty certain the reason for this is that the first cin.getline() is leaving the extra character(s) in the input stream which is messing up the second cin.getline() occurrence. I am to use cin.get, a loop, or both to prevent the extra string characters from

std::cin for double and string

别说谁变了你拦得住时间么 提交于 2019-12-24 06:42:45
问题 I am doing an exercise on currency exchange. Program should read amount and name of currency form input stream and return its value in national currency. double amount = 0.0; std::string currency = " "; std::cout << "Please enter amount and currency ('usd','eur' or 'rub'):" << std::endl; std::cin >> amount >> currency; std::cout << amount << currency << std::endl; if ( currency == "usd") { ...; } else if ( currency == "eur" ) { ...; } else if ( currency == "rub" ) { ...; } else { std::cout <<

Clear the cin buffer before another input request, c++

时间秒杀一切 提交于 2019-12-24 05:47:29
问题 I have the following code: int choice = 0; char st1[N]; cout << "enter choice" <<endl; cin >> choice; cout << "enter sentence" << endl; cin.get(st1, N-1); when getting to cin.get line, no matter what the input is, it will read \0 char into st1[0] and that's it. I assume it has something to do with the latest cin ( into choice variable ). How do i "clean" cin buffer before getting new input from the user? if that's possible. thanks 回答1: You might use ignore to drop the newline from the buffer