cin

Is there a possibility to use cin parallel to cout?

醉酒当歌 提交于 2020-05-17 07:44:05
问题 I'm trying to write a program in C++ which will be responsible for simulating blinkers in cars. I want it to be simple and to compile it in a console window. Is it possible to create one thread for input which will be always active and second for output that will run simultaneously? I wanted to use threads to solve this but it doesn't work as I would like. I have a little trouble to understand threads. If anyone could help me to fix this I would be grateful. int in() { int i; cout<<"press 1

Set std:cin to a string

↘锁芯ラ 提交于 2020-04-30 05:43:34
问题 For ease of testing I wish to set Cin's input to a string I can hardcode. For example, std::cin("test1 \ntest2 \n"); std::string str1; std::string str2; getline(cin,str1); getline(cin,str2); std::cout << str1 << " -> " << str2 << endl; Will read out: test1 -> test2 回答1: The best solution IMO is to refactor your core code to a function that accepts a std::istream reference: void work_with_input(std::istream& is) { std::string str1; std::string str2; getline(is,str1); getline(is,str2); std:

Set std:cin to a string

。_饼干妹妹 提交于 2020-04-30 05:36:28
问题 For ease of testing I wish to set Cin's input to a string I can hardcode. For example, std::cin("test1 \ntest2 \n"); std::string str1; std::string str2; getline(cin,str1); getline(cin,str2); std::cout << str1 << " -> " << str2 << endl; Will read out: test1 -> test2 回答1: The best solution IMO is to refactor your core code to a function that accepts a std::istream reference: void work_with_input(std::istream& is) { std::string str1; std::string str2; getline(is,str1); getline(is,str2); std:

C++: cin >> *char

浪尽此生 提交于 2020-04-13 17:23:35
问题 So, I'm currently writing a line editor as a learning project on I/O, writing files, and the like. It is written in C++, and I am currently trying to write out to a file of the user's choosing. I have CLI arguments implemented, but I currently have no idea how to implement an in program way of specifying the file to write to. char *filename; if (argc >= 2){ filename = argv[1]; } else{ cout << "file>"; cin >> filename; cin.ignore(); } This works perfectly well when I use command line arguments

Why is cin.ignore() necessary when using “getline” after “cin”, but is not required when using “cin” multiple times?

醉酒当歌 提交于 2020-04-06 21:27:08
问题 In my knowledge when using getline() after cin , we need to flush the newline character in the buffer first, before calling getline() , and we do this by calling cin.ignore() . std::string name, address; std::cin >> name; std::cin.ignore(); //flush newline character getline(std::cin,address); But when using multiple cin , we do not need to flush the newline character. std::string firstname, lastname; std::cin >> firstname; std::cout << firstname << std::endl; //no need to flush newline

C++ : cin inside a while loop

懵懂的女人 提交于 2020-04-05 06:55:36
问题 I have written a simple code: #include <iostream> using namespace std; int main() { int a, b; while (cin >> a >> b) //Note the cin inside while loop { cout << a << b << "\n"; } } We know that while loop functions only when the expression evaluates true ( 1 ) or false ( 0 ). How come cin is evaluating true and false . Also how is while loop running when I am entering a number and stops when I enter something non-digit ? How is it evaluating true and false? 回答1: When you writes cin >> a , you

C++ : cin inside a while loop

孤街醉人 提交于 2020-04-05 06:53:06
问题 I have written a simple code: #include <iostream> using namespace std; int main() { int a, b; while (cin >> a >> b) //Note the cin inside while loop { cout << a << b << "\n"; } } We know that while loop functions only when the expression evaluates true ( 1 ) or false ( 0 ). How come cin is evaluating true and false . Also how is while loop running when I am entering a number and stops when I enter something non-digit ? How is it evaluating true and false? 回答1: When you writes cin >> a , you

栈与队列

泄露秘密 提交于 2020-03-02 16:24:05
栈的数组实现 确定好栈底的元素 边界条件 变化过程 栈底是-1 #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using namespace std; const int maxn = 1e5 + 10; int tt; int stk[maxn]; void init() { tt = 0; // tt = 0的时候为空,因为如果不为空的话他肯定不会在0的位置 } void push(int x) { stq[tt++] = x; } void pop() { if(tt > 0) tt--; } int query() { return stq[tt - 1]; } bool empty() { return tt > 0 ? false : true; } void read() { int x; string op; cin >> op; if(op == "push") { cin >> x; push(x); } else if(op == "query") { cout << query() << endl; } else if(op == "pop") { pop(); } else { if(empty()) cout << "YES" << endl;

Can't get char from cin.get()

偶尔善良 提交于 2020-02-21 11:53:12
问题 I'm working through some beginner exercises on c++, and this has me stumped. I can enter a number, but I don't get the option to enter a character afterwards, and it skips to the final line. I know I can use cin >> symbol, but i would like to know why this isn't working. #include<iostream> using namespace std; int main() { cout << "Enter a number:\n"; int number; cin >> number; char symbol; cout << "Enter a letter:\n"; cin.get(symbol); cout << number << " " << symbol << endl; return 0; } 回答1:

Check if string is in string (list of strings)

家住魔仙堡 提交于 2020-02-20 07:34:26
问题 I'm new here and to C++. I've had some experience in python, and found "if a in b" really easy, and I'm wondering if there's an equivalent in C++. Background I've been trying to make a list of strings and check if an input is in that list. The reason I want to do this is because I want to only use a function if the input will actually do something in that function. (Change int x and y coordinates in this case) Question string myinput; string mylist[]={"a", "b", "c"}; cin>>myinput; //if