cin

std::getline input not working properly in C++ [duplicate]

狂风中的少年 提交于 2020-01-05 04:38:10
问题 This question already has answers here : Closed 8 years ago . Possible Duplicate: Need help with getline() getline not asking for input? I am working on the following code: int main() { int num; string str; cin>>num; int points[num][2]; for(int i=0;i<num;i++) { cout<<"\nPoint"<<i<<":"; getline (cin,str); points[i][0]=atoi(&str[0]); points[i][1]=atoi(&str[2]); } for(int i=0;i<num;i++) { cout<<"\npoint"<<i<<" = "<<points[i][0]<<" "<<points[i][1]; } The problem with the above code that I am

checking cin input, clearing input buffer

主宰稳场 提交于 2019-12-31 05:39:04
问题 New to c++ - trying to check for format of input. Have tried everything, at wit's end. Any help would be appreciated. I've broken down my problem to this basic case: while(1) { cin >> x; cout << "asked!" << endl; cin.ignore(1000, 'n'); } will result in infinite loop of "asked!" after the first invalid input (entering not int for x). I want to handle incorrect input. The following will not work: do { cin.clear(); cin >> x >> y; if (cin.fail()) { cout << "Invalid input." << endl; cin.ignore

std::cin:: and why a newline remains

烈酒焚心 提交于 2019-12-31 02:59:28
问题 Reference Why is the Console Closing after I've included cin.get()? I was utilizing std::cin.get() #include<iostream> char decision = ' '; bool wrong = true; while (wrong) { std::cout << "\n(I)nteractive or (B)atch Session?: "; if(std::cin) { decision = std::cin.get(); if(std::cin.eof()) throw CustomException("Error occurred while reading input\n"); } else { throw CustomException("Error occurred while reading input\n"); } decision = std::tolower(decision); if (decision != 'i' && decision !=

Is it possible to read an empty string from cin and still get true from cin.good()?

耗尽温柔 提交于 2019-12-30 23:21:31
问题 My question is based on this simple code: #include <string> using namespace std; int main() { string buf; while (cin >> buf && !buf.empty()) { cout << "input is " << buf << "\n"; } return 0; } The operator>> of cin (which is an object of type basic_istream) reads and discards any leading whitespace (e.g. spaces, newlines, tabs). Then operator>> reads characters until the next whitespace character is encountered. The operators returns finally the stream itself, cin. It shouldn't be possible to

How to properly use cin.peek()

心不动则不痛 提交于 2019-12-30 08:25:10
问题 This function is supposed to read a fraction and place it in an array. If the user enters '0' the function is supposed to exit. I am trying to do this using the cin.peek() function but execution always goes into the if statement and doesn't allow the user to exit. How should I properly code this (I am open to not using peek(), I thought it was the simplest way of doing it.) Thanks! void enterFrac(Fraction* fracs[], int& index) { int n, d; char c, slash; cout << "Enter fractions (end by

Why doesn't std::noskipws work, or what is it supposed to do?

断了今生、忘了曾经 提交于 2019-12-29 08:32:13
问题 First off my understanding is that cin >> std::noskipws >> str; should stick a whole line from cin like "i have spaces" into str . However this only puts "i" into str . This could be a false assumption in which case what does std::noskipws do? I know there is a function std::getline and that does work but simply for educational purposes I decided I would try to get std::noskipws to work for me. I have tried in the past and it just never works so I normally move on and use std::getline . What

Why does this work? Using cin to read to a char array smaller than given input

帅比萌擦擦* 提交于 2019-12-29 07:57:28
问题 I'm reading C++ Primer Plus (6th Edition) and I've come across some sample code in chapter 4 which I have a question about: Listing 4.2 strings.cpp // strings.cpp -- storing strings in an array #include <iostream> #include <cstring> // for the strlen() function int main() { using namespace std; const int Size = 15; char name1[Size]; // empty array char name2[Size] = "C++owboy"; // initialized array // NOTE: some implementations may require the static keyword // to initialize the array name2

Why does this work? Using cin to read to a char array smaller than given input

一笑奈何 提交于 2019-12-29 07:57:26
问题 I'm reading C++ Primer Plus (6th Edition) and I've come across some sample code in chapter 4 which I have a question about: Listing 4.2 strings.cpp // strings.cpp -- storing strings in an array #include <iostream> #include <cstring> // for the strlen() function int main() { using namespace std; const int Size = 15; char name1[Size]; // empty array char name2[Size] = "C++owboy"; // initialized array // NOTE: some implementations may require the static keyword // to initialize the array name2

How to reset std::cin when using it?

半世苍凉 提交于 2019-12-28 06:52:05
问题 I have some problem with following code. I use it in Xcode (OS X). [removed my first try of that code] How to input reset std::cin? I try to input another values but I can't because std::cin seems to not work anymore after my wrong value. UPD2 In my second try I use this code: for ( unsigned char i = 0; i < 5; i++ ) { int value = 0; std::cout << "\n>> Enter \"value\": "; std::cin >> value; if ( std::cin.fail() ) { std::cin.clear(); std::cin.ignore(); std::cout << "Error: It's not integer

Getting input from user using cin [duplicate]

自古美人都是妖i 提交于 2019-12-28 04:35:34
问题 This question already has answers here : C++ “cin” only reads the first word [duplicate] (5 answers) Closed last year . I am using Turbo C++ 3.0 Compiler While using the following code .. char *Name; cin >> Name; cout << Name; When I gave input with space ... its only saving characters typed before space .. like if I gave input "QWERT YUIOP" ... Name will contain "QWERT"; Any explaination why ?? 回答1: You need to allocate space for the char array into which you want to read the Name. char