cin

C++ if(!cin) causes loop

感情迁移 提交于 2019-12-12 15:05:54
问题 I tried to use if(!cin) to validate if the user input really is an integer. However my programm then just goes into an infinite loop never asking vor new input do{ cin >> temp->data; if(!cin){ cout << "Please enter a Number!" << '\n'; correct=false; } }while(correct==false); Would be great if someone could help me :) 回答1: When std::cin fails to read the input, the appropriate error flags are set. Therefore you want to reset the flags using std::cin.clear() so that the next input operation

Interrupt cin while loop without the user entering input

女生的网名这么多〃 提交于 2019-12-12 12:23:47
问题 In main , I give the user the ability to enter a command to stop the application: while( run_processes && cin >> command_line_input ){ I'd also like to stop the application elsewhere by setting run_processes = false; . However, when I set run_processes to false , the above loop doesn't stop without the user entering input. How can I properly interrupt the loop without the user entering input? 回答1: It is not possible to interrupt std::cin in a portable way. You could however resolve to non

How to get input from another file for my Sudoku game C using a .txt file

瘦欲@ 提交于 2019-12-12 05:27:30
问题 Alright so my problem is that I have no idea where to go in my situation with my Sudoku Program. As of right now I have multiple programs that work together to create my Sudoku program which is SUPPOSED to take input of 9 lines with 9 characters each whether it be just spaces or numbers. example: 53 7 6 195 98 6 8 6 3 4 8 3 1 7 2 6 6 28 419 5 8 79 What the program would yield would be: 534678912 672195348 198342567 859761423 426853791 713924856 961537284 287419635 345286179 My current

Unexpected Output of C++ Code

点点圈 提交于 2019-12-12 04:59:22
问题 Ok Guys... Im Just trying to practice structs here and i made this C++ Code: #include <iostream> #include <cstring> using namespace std; struct DATE { int year; int month; int date; }; struct Book { char name[50]; char author[50]; int id; DATE date; }; int main() { Book book1; DATE date1; char bookName, bookAuthor,*bookNamePointer = "", *bookAuthorPointer = ""; int date, year, month; cout << "Date Of Publishing? " << endl; cin >> date; cout << "Month Of Publishing?" << endl; cin >> month;

In C++ code, how do I get the user to input a number in between 2 strings of text [duplicate]

佐手、 提交于 2019-12-12 04:24:36
问题 This question already has answers here : How do I input variables using cin without creating a new line? (7 answers) Closed 2 years ago . Suppose we have the following sentence: The last time I shopped was __ days ago. How would I change this into C++ code, so that __ is an integer that the user inputs? So far, I have come up with the following, which puts the first string, the integer input, and the second string on separate lines: #include <iostream> using namespace std; int main(){ cout <<

Why is this code snippet infinitely looping?

前提是你 提交于 2019-12-12 02:53:45
问题 I'm fairly new to programming, and I'm trying to filter inputs from cin (for no particular reason; It is the only input method I know of right now) such that one can only enter 1-10 and get the "positive" response. Here is my code: #pragma region Check cin is a valid input (an integer between 1 and 10, not a string or number out of range) int input; bool valid2=false; while (!valid2) { cout << "Enter an integer (1-10): "; cin >> input; if (!cin || input > 10 || input < 1) { cout << "Error:

Allow user to skip entering input and just pressing Enter key

一世执手 提交于 2019-12-12 02:09:50
问题 Is there any way I can allow the user to press only the Enter key without entering any input and proceed with the program in std::cin ? Thanks! _tprintf(_T("\nENTER CHOICE: ")); cin>>ch; cin.ignore(); When I run the program with this code segment, chances are when I really don't want to enter anything on that field at all, when I press the enter key the cursor will just produce new lines. 回答1: if you "read in" std::noskipws , then reads will stop skipping whitespace. This means when you read

Valgrind error with std::cin

邮差的信 提交于 2019-12-12 01:09:19
问题 Here is my code: std::string getword() { std::string temp; std::cin >> temp; return temp; } Valgrind throws an error on the line std::cin >> temp. Here is the valgrind output for those who asked: HEAP SUMMARY: ==18490== in use at exit: 33 bytes in 1 blocks ==18490== total heap usage: 397 allocs, 396 frees, 12,986 bytes allocated ==18490== ==18490== 33 bytes in 1 blocks are possibly lost in loss record 1 of 1 ==18490== at 0x4C2AF8E: operator new(unsigned long) (in /usr/lib/valgrind/vgpreload

Checking for valid type input using std::cin (C++)

流过昼夜 提交于 2019-12-12 00:19:44
问题 I am working on the game 'Hex' for a C++ class, and am having trouble understanding how to make sure cin is reading the correct type. I have done some research and I'm close, but here is my problem: int i, j; while(true){ cin >> i; if(cin.fail() ){ //if type wasn't right cin.clear(); //clear stream cin.ignore(); //ignore left over data cout << "First value is invalid input! Valid coordinates are in range 0 to " << size - 1 << endl; continue; } cin >> j; if(cin.fail() ){ //if type wasn't right

Ignoring commas in c++ cin

余生长醉 提交于 2019-12-11 23:01:41
问题 I have the following code: float x1 = 0,x2 = 0,y1 = 0,y2 = 0; cout << "Enter coordinates as \"(x1,y1) (x2,y2)\"\n"; cin >> x1; cin.ignore(1, ','); cin >> y1; cin >> x2; cin.ignore(1, ','); cin >> y2; cout << "Coordinates registered as (" << x1 << "," << y1 << "), (" << x2 << "," << y2 << ").\n"; But this always returns (0,0) (0,0). What would the correct implementation of cin.ignore be? 回答1: If you are literally entering in the data in the form of (x1,y1) (x2,y2) like (10,20) (30,40) Then you