cin

Reading double matrix from cin

假如想象 提交于 2019-12-11 20:21:14
问题 I need to read squad matrix from cin, but I don't know the size of this matrix. So I need to read first row(double numbers separated by space or tab till end of line). After parse this line to get count of double numbers. If in row will be n double numbers then matrix size will nxn. How can I do that? Code: unsigned int tempSize = 0; double tempPoint; double * tempArray = new double [0]; string ts; getline(std::cin, ts); std::istringstream s(ts); while (s >> tempPoint){ if (!s.good()){ return

Someway to buffer or wrap cin so I can use tellg/seekg?

混江龙づ霸主 提交于 2019-12-11 17:20:05
问题 Is there any way to add buffering to cin so that I can effectively use tellg and seekg on that istream? (I only need to go back about 6 characters.) Or is there perhaps some way to wrap the stream with a (perhaps custom) istream object that acts as a buffered pipe that would allow me to use tellg/seekg to revert the stream position a few characters? It might look like this: BufferedIStream bis(cin); streampos pos = bis.tellg(); MyObjectType t = getObjectType(bis); bis.seekg(pos); As a work

Why cin inside while doesn't stop to get user input?

≯℡__Kan透↙ 提交于 2019-12-11 13:34:02
问题 I'm starting now with C++, so I imagine this is gonna be a very easy-newbie question. Well, why the "cin >> x" line inside while doesn't stop the loop to get the user input (if the user inputs a character, in place of a number)? #include <iostream> using std::cin; using std::cout; using std::endl; int main() { int x = 0; cout << "Please, enter x: "; cin >> x; while (!cin) { cout << "Please, it must be a number!\n"; cin >> x; } cout << "Thanks!."; cin.ignore(); cin.ignore(); } I'm barely two

Can std::cin switch from accepting file input to keyboard input at run-time?

血红的双手。 提交于 2019-12-11 13:15:28
问题 Suppose I have a program that accepts input from the keyboard, or alternatively accepts redirected input from a file such that: #include <iostream> using namespace std; int main() { string str; while (getline(cin, str)) { if(str.compare("exit") == 0) return 0; } return 0; } In this implementation, I expect that an instance of the program using keyboard input will terminate upon typing "exit", and that an instance of the program using file input will terminate upon EOF. I would like to

Taking input over standard I/O in multithreaded application

ε祈祈猫儿з 提交于 2019-12-11 11:57:30
问题 I have a question about input/output, or basically interaction with the user, in multithreaded applications. Let's say I have a program that starts three threads and waits for them to end, then starts them again int main() { while(true) { start_thread(1); start_thread(2); start_thread(3); //.... join_thread(1); join_thread(2); join_thread(3); } } Every thread also outputs data over cout . I'm looking for a way to take input by the user ( cin ) without stopping / hindering the main loop in it

How to cin whole sentence with whitespaces [closed]

泪湿孤枕 提交于 2019-12-11 08:13:58
问题 As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 6 years ago . I have tried: char tab[200]; cin>>tab; cout<<tab<<endl; and I would like to make that even if I input in console A B C all the 3 chars

How to make the cin show in the same spot each time for c++ console

早过忘川 提交于 2019-12-11 07:42:07
问题 In the following code, I have the game board stay the same. I have the program looping so that the user can recycle the cards through as many times as they want. I would like it so that the character typed by the user will show in the exact same spot each time, instead of creating a new line. I would like for it to show so they can see what they typed before they hit enter. I am still pretty new to coding and I am in my 2nd C++ class, and I wish to learn and impress. To start off, I am using

Why am I getting a segfault on cin?

走远了吗. 提交于 2019-12-11 07:16:49
问题 I am writing a fairly simple exercise (homework), and most of it works, however it sometimes segfaults on cin. Here is the relevant code. int main() { std::string str = ""; std::cout << "Please select the desired operation:\n"; std::cout << "(A): Generate Decompositions\n"; std::cout << "(B): Generate Acceptable Compositions from S1.txt and S2.txt\n"; std::cout << "cout"; //debug statement std::cin >> str; std::cout << "cin"; //debug statement std::cout << str; char resp = str.at(0); std:

Having some annoying issues with the cin buffer

瘦欲@ 提交于 2019-12-11 05:38:17
问题 Here is my bit of code. Basically, in my main i'm passing the user to this selector menu. When the user makes a selection, i'm passing back to main and then passing them to the appropriate class to perform further functions. For example, when the user selects "send", they get passed to main and then passed to a function which collects input about where to send. Then back to main, then back to a function that asks them how much. This works fine the first time around. The problem is, if they

fast file reading in C++, comparison of different strategies with mmap() and std::cin() performance results interpretation

被刻印的时光 ゝ 提交于 2019-12-11 05:05:50
问题 After advises on a question I made to close mmapped files (c++ close a open() file read with mmap) I made some comparisons and I noticed that, as suggested by some users, std::cin buffer approach is performing similar to my mmapped approach. I decided to make a performance comparison: every script opens a file that contains other files path (about 3500), read the file and takes 10 random paths and opening all these 10 files (of 500 lines every, about 700 characters each line) and read the