cin

Checking cin input stream produces an integer

ぐ巨炮叔叔 提交于 2019-12-17 02:12:11
问题 I was typing this and it asks the user to input two integers which will then become variables. From there it will carry out simple operations. How do I get the computer to check if what is entered is an integer or not? And if not, ask the user to type an integer in. For example: if someone inputs "a" instead of 2, then it will tell them to reenter a number. Thanks #include <iostream> using namespace std; int main () { int firstvariable; int secondvariable; float float1; float float2; cout <<

Infinite loop with cin when typing string while a number is expected

老子叫甜甜 提交于 2019-12-16 19:09:45
问题 In the following loop, if we type characters as the cin input instead of numbers which are expected, then it goes into infinite loop. Could anyone please explain to me why this occurs? When we use cin , if the input is not a number, then are there ways to detect this to avoid abovementioned problems? unsigned long ul_x1, ul_x2; while (1) { cin >> ul_x1 >> ul_x2; cout << "ux_x1 is " << ul_x1 << endl << "ul_x2 is " << ul_x2 << endl; } 回答1: Well you always will have an infinite loop, but I know

cin and getline skipping input [duplicate]

狂风中的少年 提交于 2019-12-16 19:03:06
问题 This question already has answers here : Why does std::getline() skip input after a formatted extraction? (3 answers) Closed 3 years ago . earlier i posted a question about cin skipping input, and I got results to flush, and use istringstream , but now I tried every possible solution but none of them work. here is my code: void createNewCustomer () { string name, address; cout << "Creating a new customer..." << endl; cout << "Enter the customer's name: "; getline(cin, name); cout << "Enter

Erorr: no operator “>>” matches these arguments

不羁的心 提交于 2019-12-14 03:26:50
问题 int main() { char* NamePointer = new char; std::cout << "Enter the file you want to edit (MUST BE IN THE PROJECT'S DIRECTORY): "; std::cin >> &NamePointer; const char* FileName = NamePointer; delete &NamePointer; return 0; } This returns me the error in the title: std::cin >> &NamePointer; Any ideas? This is probably something simple I'm missing, but when I looked it up I didn't get my answer. JUST FIXED IT, SORRY FOR BOTHERING! I took out the "&" in "&NamePointer". Yeah I just found out

how to ask for input again c++

▼魔方 西西 提交于 2019-12-13 09:26:47
问题 My question is how I ask the user if he/she wants to input again. ex. do you want to calculate again? yes or no. Can someone explain what I am doing wrong and fix the error. int main() { } int a; cout << endl << "Write 1 for addition and 0 for substraction:" << endl; cin >> a; // addition if (a == 1) { cout << "You are now about to add two number together, "; cout << "enter a number: " << endl; int b; cin >> b; cout << "one more: " << endl; int c; cin >> c; cout << b + c; } //Substraction

std::codecvt::do_in method overloading vs the rest of base methods

假装没事ソ 提交于 2019-12-13 04:39:00
问题 I have overloaded do_in method of std::codecvt : #include <iostream> #include <locale> #include <string> class codecvt_to_upper : public std::codecvt<char, char, std::mbstate_t> { public: explicit codecvt_to_upper(size_t r = 0) : std::codecvt<char, char, std::mbstate_t>(r) {} protected: result do_in(state_type& state, const extern_type* from, const extern_type* from_end, const extern_type*& from_next, intern_type* to, intern_type* to_end, intern_type*& to_next) const; result do_out(state_type

Conditionally Breaking A Long Sequence Of Inputs?

元气小坏坏 提交于 2019-12-13 04:33:35
问题 I have a personal project I've been working on. To work, it needs to accept a lot of data (relatively) from the user, in the form of four different kinds of data for 12 users. As such, I have quite a lengthy sequence of statements similar to this: cout << '\n' << "Monster A's name is: "; cin >> nameA; cout << '\n' << "Monster A rolled: "; cin >> rollM_A; cout << '\n' << "Monster A's Dex is: "; cin >> DexA; cout << '\n' << "Monster A's Mod is: "; cin >> ModA; cout << '\n' << "Monster A's Level

Assigning the output of cin to variables

こ雲淡風輕ζ 提交于 2019-12-13 00:25:05
问题 I'm trying to learn how to use cin and getline to write a paper grading program that I can use at school. It's kind of a tricky project for a beginner but it lets me know what I need to learn and this is the first thing I need to do. int main() { string grader; int x; cout << "Who will I be assisting today? "; getline (cin, grader); cout << "Hello " << grader << ".\n"; cout << "How manny questions are on the test you will be grading? "; getline (cin, x); cout << "this is a " << x << "question

cin >> i error in input with symbol +

不打扰是莪最后的温柔 提交于 2019-12-12 22:21:32
问题 In a C++ program I'm trying to process user input which consists of integer operands interspersed with operators (+ - / *). I have the luxury of requiring users to put whitespace(s) before and after each operator. My approach is to assume that anything that's not an int is an operator. So as soon as there's a non eof error on the stream, I call cin.clear() and read the next value into a string. #include <iostream> #include <string> //in some other .cpp i have these functions defined void

Cin has no operand >>

 ̄綄美尐妖づ 提交于 2019-12-12 16:08:02
问题 I don't understand why this isn't working. For some reason I'm getting the error: error C2678: binary '>>' : no operator found which takes a left-hand operand of type 'std::istream' (or there is no acceptable conversion) I'm doing this in Visual Studio2010 C++ Express if that helps. Not sure why its handing me this error I've done other programs using cin ... My Code: #include <iostream> #include <iomanip> #include <fstream> using namespace std; int main(int argc, char* argv){ string file; if