cin

Limiting number of elements in vector

无人久伴 提交于 2019-12-04 06:19:07
问题 Trying to limit the amount of inputs that a user could insert into a vector when inputting an array into 1 manually, but for some reason it's being weird. #include <iostream> using namespace std; void fillVector(vector<int>& newThisIsAVector) { cout << "Please type in your 10 numbers separated by a space. On completion press enter."; int input; cin >> input; while (newThisIsAVector.size() < 10) { newThisIsAVector.push_back(input); cin >> input; } cout << endl; } It's supposed to limit you at

cin.get() in a loop

戏子无情 提交于 2019-12-04 05:18:50
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 advance. Mixing formatted and unformatted input is fraught with problems. In your particular case, this line

Reading 'unsigned int' using 'cin'

本秂侑毒 提交于 2019-12-04 03:28:17
问题 I am trying to read an unsigned int using cin as follows: #include <limits.h> #include <iostream> using namespace std; int main(int argc, char* argv[]) { unsigned int number; // UINT_MAX = 4294967295 cout << "Please enter a number between 0 and " << UINT_MAX << ":" << endl; cin >> number; // Check if the number is a valid unsigned integer if ((number < 0) || ((unsigned int)number > UINT_MAX)) { cout << "Invalid number." << endl; return -1; } return 0; } However, whenever I enter a value

What are the rules of the std::cin object in C++?

纵饮孤独 提交于 2019-12-04 02:33:33
问题 I am writing a small program for my personal use to practice learning C++ and for its functionality, an MLA citation generator (I'm writing a large paper with tens of citations). For lack of a better way to do it (I don't understand classes or using other .cpp files inside your main, so don't bother telling me, I'll work on that when I have more time), I am writing a function for each type of citation. I might break this down into a function for each reused code if I get more time. My

Can a variable be initialized with an istream on the same line it is declared? [duplicate]

☆樱花仙子☆ 提交于 2019-12-04 01:56:41
This question already has an answer here: Are there any tricks to use std::cin to initialize a const variable? 6 answers Can the following two lines be condensed into one? int foo; std::cin >> foo; The smart-ass answer: int old; std::cin >> old; The horrible answer: int old, dummy = (std::cin >> old, 0); The proper answer: old has to be defined with a declaration before it can be passed to operator>> as an argument. The only way to get a function call within the declaration of a variable is to place it in the initialization expression as above. The accepted way to declare a variable and read

How to deactivate input statement after some time?

一笑奈何 提交于 2019-12-03 18:06:07
问题 We know input function or operator (cin, scanf,gets….etc) wait to take input form user & this time has no limit. Now, I will ask a question & user give the answer, till now there no problem but my problem is “user has a time(may 30 or 40 sec) to give the input, if he fail then input statement will automatically deactivated & execute next statement.” I think you get my problem. Then please help me in this situation. It will be better if someone give me some really working example code. I use

Cin and Boolean input

邮差的信 提交于 2019-12-03 13:55:37
I am new to C++ and I was wondering how the function cin in case of a boolean data works. Let's say for instance : bool a; cin >> a; I understand that if I give 0 or 1, my data a will be either true or false. But what happens if I give another integer or even a string ? I was working on the following code : #include <iostream> using namespace std; int main() { bool aSmile,bSmile; cout << "a smiling ?" << endl; cin >> aSmile; cout << "b smiling ?" << endl; cin >> bSmile; if (aSmile && bSmile == true) { cout << "problem"; } else cout << "no problem"; return 0; } If I give the values of 0 or 1

C++ having cin read a return character

半城伤御伤魂 提交于 2019-12-03 05:48:14
I was wondering how to use cin so that if the user does not enter in any value and just pushes ENTER that cin will recognize this as valid input. Martin Cote You will probably want to try std::getline : #include <iostream> #include <string> std::string line; std::getline( std::cin, line ); if( line.empty() ) ... I find that for user input std::getline works very well. You can use it to read a line and just discard what it reads. The problem with doing things like this, // Read a number: std::cout << "Enter a number:"; std::cin >> my_double; std::count << "Hit enter to continue:"; std::cin >>

Should reading negative into unsigned fail via std::cin (gcc, clang disagree)?

无人久伴 提交于 2019-12-03 04:19:17
For example, #include <iostream> int main() { unsigned n{}; std::cin >> n; std::cout << n << ' ' << (bool)std::cin << std::endl; } When input -1 , clang 6.0.0 outputs 0 0 while gcc 7.2.0 outputs 4294967295 1 . I'm wondering who is correct. Or maybe both are correct for the standard does not specify this? By fail, I take to mean (bool)std::cin be evaluated false. clang 6.0.0 fails input -0 too. As of Clang 9.0.0 and GCC 9.2.0, both compilers, using either libstdc++ or libc++ in the case of Clang, agree on the result of the program above, independent of the C++ version (>= C++11) used, and print

C++杂记(一)――未声明的cin和cout(已声明std空间情况下)

匿名 (未验证) 提交于 2019-12-03 00:28:02
有时候时间久了,一些基本功就会忘了,这里记录一下。 问题: 在写CPP程序时,报错: 未声明的cin和cout 代码举例如下: #include "stdafx.h" using namespace std; #include <iostream> int main() { int flag = 0; if (flag) cout<<"success!"; else cout<<"error"; return 0; } 乍一看没问题啊!后来发现 using namespace std; 要紧跟在这句引用的后面 #include <iostream> 解决: 正确的顺序为 #include "stdafx.h" #include <iostream> using namespace std; 文章来源: C++杂记(一)――未声明的cin和cout(已声明std空间情况下)