cin

My cin is being ignored inside a while loop

匆匆过客 提交于 2019-12-07 02:26:45
问题 I am trying to code a simple question and number checker into my first C++ program. Problem is, when I type a string like one two, or three, the program becomes and infinite loop and it ignores the cin function to re-assign lives to a number. cout << "How many lives would you like 1 (hard), 2 (medium), or 3 (easy)?" << endl; cin >> lives; while(lives != 1 && lives != 2 && lives != 3 && !isdigit(lives)) { cout << "You need to input a number, not words." << endl; cout << "How many lives would

Detect hitting Enter Key in C++

橙三吉。 提交于 2019-12-06 19:56:26
How can I get my code to detect me hitting the enter key? I tried using cin.get() without any success. Also when the enter key is pressed, I'd like to change a boolean x from true to false. Why doesn't this work? if (cin.get() == '\n'){ x = false; } I'd like to end my loop (and thus and the program) when the enter key is pressed (see code below) All code (simple rock, paper, scissors game): #include <iostream> #include <string> #include <cstdlib> //random #include <time.h> //pc time using namespace std; int main() { string rpsYou; string rpsCom; string winner; bool status = true; while (status

cin >> integer and while loop

隐身守侯 提交于 2019-12-06 16:20:55
With below code, if I enter a letter or a really long number, the while loop will go haywire, why is that? void main() { int n{ 0 }; while (true) { cout << "Enter a number: "; cin >> n; cout << n << endl; } } The problem is that operator>> is expecting to draw an integer off of the input stream, but there is something else sitting there (the non-integer that the user typed). This sets an error state on the input stream. In this state, the cin >> ... construct no longer blocks for input because there's already something (not an integer) in the stream, so you see your loop go haywire. What needs

C++ Input a char instead of int lead to endless loop. How to check the wrong input?

六月ゝ 毕业季﹏ 提交于 2019-12-06 14:13:06
问题 One part of my program: Let user input a series of integer, and then put them into an array. int n=0; cout<<"Input the number of data:"; cin>>n; cout<<"Input the series of data:"; int a[50]; for(i=0; i<n; i++) { cin>>a[i]; } Then, when user input wrong data such as a character 'a' or 'b'. The program will go into an infinite loop. How to catch the wrong cin? How to clear the buffer and give user the chance to input a right data again? 回答1: Simply check if the input is a number first and then

confused by control flow execution in C++ Primer example

…衆ロ難τιáo~ 提交于 2019-12-06 12:18:09
I am going through C++ Primer (5th ed). In section 1.4.4, there is the following example: #include <iostream> int main() { // currVal is the number we're counting; we'll read new values into val int currVal = 0, val = 0; // read first number and ensure that we have data to process if (std::cin >> currVal) { int cnt = 1; // store the count for the current value we're processing while (std::cin >> val) { // read the remaining numbers if (val == currVal) // if the values are the same ++cnt; // add 1 to cnt else { // otherwise, print the count for the previous value std::cout << currVal << "

The C++ program should exit as soon as he presses 'esc'

谁说我不能喝 提交于 2019-12-06 06:15:33
I have a program in which there is a code which looks something like this: int Element[15]; cout << "Enter name/symbol of the Element: "; cin >> Element; and I want the program to exit as soon as he presses 'esc' key. And also the user should not have to press the 'enter' key after pressing the 'esc' key. So how to do that?? gthacoder In Windows you can do it using Windows API. GetAsyncKeyState(VK_ESCAPE) helps you to check if Escape is pressed. You can try something like this: #include <windows.h> #include <iostream> using namespace std; int main() { int Element[15]; HANDLE h; do { h =

cin directly to vector<int>, break loop when no more data

爱⌒轻易说出口 提交于 2019-12-06 06:04:56
The following code runs and stores input in the vector as it should but loops indefinitely listening for input. The intent is to take a string of ints from one line of input, separated by spaces, and store them in a vector. int main(int argc, char ** argv){ int input; vector<int> intVector; while (cin >> input) intVector.push_back(input); //print vector contents copy(intVector.begin(), intVector.end(), ostream_iterator<char>(cout, " ")); cout << "\n"; return 0; } I want to somehow add a simple, extra condition in the while loop that checks for the end of the line so that it doesn't just keep

std::cin skips white spaces

风格不统一 提交于 2019-12-06 02:16:34
问题 So I am trying to write a function to check whether a word is in a sentence, by looping through a char array and checking for the same string of char's. The program works as long as the Sentence doesn't have any spaces. I googled around and they are all the same suggestions; cin.getline But however I implement it, it either doesn't run or skips the entire input and goes straight towards the output. How can I account for spaces? #include <iostream> using namespace std; bool isPartOf(char *,

C++ cin fails when reading more than 127 ASCII values

空扰寡人 提交于 2019-12-06 01:51:19
问题 I've created a text file that has 256 characters, the first character of the text file being ASCII value 0 and the last character of the text value being ASCII value 255. The characters in between increment from 0 to 255 evenly. So character #27 is ASCII value 27. Character #148 should be ASCII value 148. My goal is to read every character of this text file. I've tried reading this with cin . I tried cin.get() and cin.read() , both of which are supposed to read unformatted input. But both

Storing Two Spaced String in vector<string>

旧街凉风 提交于 2019-12-05 22:45:57
Recently I faced a problem But before that I will tell you what is the reference Consider this program #include<bits/stdc++.h> using namespace std; int main() { vector<string> RS; string word; while(cin>>word) RS.push_back(word); } This code stores each word of spaced string in vector But the problem comes here ..... #include<bits/stdc++.h> using namespace std; int main() { vector<string> RS,FS; string word; while(cin>>word) RS.push_back(word); while(cin>>word) FS.push_back(word); } Here the motive is to store the string words of first line in RS and of second line in FS vectors But it doesn't