cin

Keep a running C++ program console on top?

拈花ヽ惹草 提交于 2019-12-10 12:17:17
问题 Let's say I was using cin in my program to allow the user to input into the console. That is simple enough but what if they were typing into, let's say, a web browser and I wanted them to input that into the console at the same time? When I click away the C++ program console window and have something else on top, the input obviously does not go into the console. How can I make it so the console is always running on top so that even when I were to input something into a web browser, it would

cin.get() in a loop

余生长醉 提交于 2019-12-09 17:53:55
问题 I was trying to read from standard input. The first line is the number of lines that I will read. The lines that I read next will be printed again. Here is the code: #include <iostream> using namespace std; int main() { int n; cin >> n; for (unsigned int i = 0; i < n; ++i) { char a[10]; cin.get (a, 10); cout << "String: " << a << endl; } return 0; } When I run it and give number of lines, the program exits. I haven't figured out what's going on, so I've decided to ask it here. Thanks in

C++ - Reading a double, followed by a character, from cin

亡梦爱人 提交于 2019-12-08 08:36:54
问题 I'm trying to read a double, followed by a character, from cin using the snippet: double d; char c; while(1) { cin >> d >> c; cout << d << c << endl; } The peculiar thing is that it works for some characters, but not for others. For example, it works for "2g", "2h", but fails for "2a", "2b", "2x" ...: mwmbp:ppcpp mwisse$ ./a.out 2a 0 2b 0 2c 0 2g 2g 2h 2h 2i 0h 2x 0h 2z 2z As pointed out by one of you, it does indeed work for integers. Do you know why it doesn't work for doubles? I have as

cin >> integer and while loop

风格不统一 提交于 2019-12-08 06:57:02
问题 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; } } 回答1: 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

Using cin.eof()

旧巷老猫 提交于 2019-12-08 06:24:42
问题 I have some code and I wanted to use cin.eof() to stop my program from reading the input. I was thinking of doing: char array[10] while(!cin.eof()) { for(int i = 0; i < 10; i++) { cin >> array[i]; } } And the code goes on. However, when I click '\n', my output is outputted. When i click cntrl + d, (on a UNIX terminal) the program performs the output again and then proceeds to end. How do I get it so that my program stops reading at a newline and prints my output when I type cntrl + d only

Program skips second cin

喜夏-厌秋 提交于 2019-12-08 05:26:48
问题 I'm making a C++ Mind Reader program, which is nearly complete. However, it feels the need to skip the second cin. I've searched and I'm not exactly sure what's wrong. I've examined the code and I bet I've done something stupid, but I am still baffled about it. The skipped cin is on line 32, here's my code: #include <iostream> #include <string> #include <cstdlib> using namespace std; int main() { //declaring variables to be used later string name; string country; int age; //header goes below

confused by control flow execution in C++ Primer example

删除回忆录丶 提交于 2019-12-08 01:24:32
问题 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; //

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

白昼怎懂夜的黑 提交于 2019-12-07 22:24:04
问题 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?? 回答1: 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

Storing Two Spaced String in vector<string>

风流意气都作罢 提交于 2019-12-07 13:02:41
问题 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

C++ - Reading a double, followed by a character, from cin

好久不见. 提交于 2019-12-07 04:03:25
I'm trying to read a double, followed by a character, from cin using the snippet: double d; char c; while(1) { cin >> d >> c; cout << d << c << endl; } The peculiar thing is that it works for some characters, but not for others. For example, it works for "2g", "2h", but fails for "2a", "2b", "2x" ...: mwmbp:ppcpp mwisse$ ./a.out 2a 0 2b 0 2c 0 2g 2g 2h 2h 2i 0h 2x 0h 2z 2z As pointed out by one of you, it does indeed work for integers. Do you know why it doesn't work for doubles? I have as yet been unable to find information on how cin interprets its input. This is currently a bug on LLVM: