cin

How do you limit the maximum amount of characters in user input in C++?

不羁的心 提交于 2019-12-24 02:18:57
问题 I want it so that when the user inputs more than 5 characters, something will happen, instead of just skipping the rest. In this code, if you type in more than 5 characters, it will only show the first 5 characters. I want to put an "if" statement here that if the user inputs more than 5 characters, it will show an error or something, and not just show the first 5 characters. #include <iostream> #include <iomanip> int main() { using namespace std; string nationname; int maxchar = 5; cout <<

do while loops can't have two cin statements?

自古美人都是妖i 提交于 2019-12-23 22:32:58
问题 I'm just following a simple c++ tutorial on do/while loops and i seem to have copied exactly what was written in the tutorial but i'm not yielding the same results. This is my code: int main() { int c=0; int i=0; int str; do { cout << "Enter a num: \n"; cin >> i; c = c + i; cout << "Do you wan't to enter another num? y/n: \n"; cin >> str; } while (c < 15); cout << "The sum of the numbers are: " << c << endl; system("pause"); return (0); } Right now, after 1 iteration, the loop just runs

How to put data in cin from string

风流意气都作罢 提交于 2019-12-23 19:26:23
问题 I need to write tests(using google testing framework) for small study program that was written not by me. (it's just small console game which can get modes from command line or just get it in runtime) There is a problem: I can't change the souce code but there is in almost all methods used cout and cin. and my question is "how to answer on requests (cin) of programm while testing (something like get data for cin from string )? ". 回答1: Assuming you can control main() (or some other function

cin erratic behaviour

我只是一个虾纸丫 提交于 2019-12-23 12:44:48
问题 I'm a newbie to C++. Small code sample follows: int main(int argc, char* argv[]) { char ch1; int int1; cin >> ch1; cin >> int1; cout << ch1 << '\n'; cout << int1 << '\n'; return 0; } When I run the program and input the following: az I get as output: a 32767 I understand the 'a' but why the integer value of 32767? I just want to test and see what happen if instead of a numeric value assigned to int1 i used a 'z'. I try inputting: ax ...and I also get same results. Now if instead of int int1 I

Detect hitting Enter Key in C++

若如初见. 提交于 2019-12-23 04:14:14
问题 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

Cannot cin.ignore till EOF?

丶灬走出姿态 提交于 2019-12-23 03:07:54
问题 I wanted to ignore all characters in cin to flush cin in this answer: How to get rid of bad input one word at a time instead of one line at a time? But I found that the program seemed to hang awaiting input if I wrote: cin.ignore(std::numeric_limits<std::streamsize>::max()); It propperly flushed cin if I used the '\n' delimiter: cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); My question is, why can't I just ignore till EOF? Why do I have to provide the delimiter? 回答1: The

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

六月ゝ 毕业季﹏ 提交于 2019-12-22 12:34:07
问题 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

Using cin in QtCreator

末鹿安然 提交于 2019-12-22 03:57:49
问题 For school, we use C++ as the language of choice. I am currently using QtCreator as an IDE, and for its GUI library, it is wonderful. The school is using Visual Studio. However, most of the programs we are writing make use of cin and cout for input/output. cout works fine as output, as you can see what it puts out in the application output, but there is no way to provide to cin as if it were on a console, like Visual Studio uses for its C++. An example: #include <iostream> #include <string>

‘numeric_limits’ was not declared in this scope, no matching function for call to ‘max()’

蹲街弑〆低调 提交于 2019-12-22 01:26:29
问题 I compiled this code at home on my mac w/ xcode and there was no provblem. I compile it at school with g++ on linux and I get these errors: :‘numeric_limits’ is not a member of std :expected primary-expression before ‘>’ token :no matching function for call to ‘max()’ #include <iostream> #include <cstdlib> using namespace std; int GetIntegerInput(int lower, int upper) { int integer = -1; do { cin >> integer; cin.clear(); cin.ignore(std::numeric_limits<streamsize>::max(), '\n'); //errors here

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

夙愿已清 提交于 2019-12-20 17:36:59
问题 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