cin

How to cin Space in c++?

余生颓废 提交于 2019-11-26 11:21:55
Say we have a code: int main() { char a[10]; for(int i = 0; i < 10; i++) { cin>>a[i]; if(a[i] == ' ') cout<<"It is a space!!!"<<endl; } return 0; } How to cin a Space symbol from standard input? If you write space, program ignores! :( Is there any combination of symbols (e.g. '\s' or something like this) that means "Space" that I can use from standard input for my code? It skips all whitespace (spaces, tabs, new lines, etc.) by default. You can either change its behavior, or use a slightly different mechanism. To change its behavior, use the manipulator noskipws , as follows: cin >> noskipws >

Checking cin input stream produces an integer

家住魔仙堡 提交于 2019-11-26 11:21:08
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 << "Please enter two integers and then press Enter:" << endl; cin >> firstvariable; cin >> secondvariable;

Why does stringstream >> change value of target on failure?

北城以北 提交于 2019-11-26 10:33:54
From Stroustrup's TC++PL, 3rd Edition, Section 21.3.3: If we try to read into a variable v and the operation fails, the value of v should be unchanged (it is unchanged if v is one of the types handled by istream or ostream member functions). The following example appears to contradict the above quote. Based on the above quote, I was expecting the value of v to remain unchanged -- but it gets zeroed. What's the explanation for this apparent contradictory behaviour? #include <iostream> #include <sstream> int main( ) { std::stringstream ss; ss << "The quick brown fox."; int v = 123; std::cout <<

How to signify no more input for string ss in the loop while (cin >> ss)

一曲冷凌霜 提交于 2019-11-26 09:51:27
问题 I used \"cin\" to read words from input stream, which like int main( ){ string word; while (cin >> word){ //do sth on the input word } // perform some other operations } The code structure is something like the above one. It is compilable. During the execution, I keep inputting something like aa bb cc dd My question is how to end this input? In other words, suppose the textfile is just \"aa bb cc dd\". But I do not know how to let the program know that the file ends. 回答1: Your code is correct

Correct way to use cin.fail()

邮差的信 提交于 2019-11-26 09:51:10
问题 What is the correct way to use cin.fail(); ? I am making a program where you need to input something. It is not very clear if you need to input a number or character. When a user inputs a character instead of a number the console goes crazy. How can I use cin.fail() to fix this? Or is there a better way? 回答1: cin.fail() returns true if the last cin command failed, and false otherwise. An example: int main() { int i, j = 0; while (1) { i++; cin >> j; if (cin.fail()) return 0; cout << "Integer

getline not asking for input? [duplicate]

自古美人都是妖i 提交于 2019-11-26 08:57:22
This question already has an answer here: Need help with getline() 7 answers This is probably a very simple problem but forgive me as I am new. Here is my code: #include <iostream> #include <string> #include <sstream> using namespace std; int main () { string name; int i; string mystr; float price = 0; cout << "Hello World!" << endl; cout << "What is your name? "; cin >> name; cout << "Hello " << name << endl; cout << "How old are you? "; cin >> i; cout << "Wow " << i << endl; cout << "How much is that jacket? "; getline (cin,mystr); stringstream(mystr) >> price; cout << price << endl; system(

Why is this cin reading jammed?

北慕城南 提交于 2019-11-26 07:47:06
问题 I\'ve singled out a failure on my program that prevents me from assigning a value to the variable addAntonymAnswer1 . I\'ve tried running cin.clear() before the statement to get the thing read my yes/no answer, but the code just won\'t respond. The program bit that\'s failing is located inside void dictionaryMenu(vector <WordInfo> &wordInfoVector) and reads cin.clear(); cout<<\">\"; cin>>addAntonymAnswer1; // cin reading STUCK, why!? to get to that point of the program the user has to choose

When and why do I need to use cin.ignore() in C++?

自作多情 提交于 2019-11-26 06:37:45
I wrote a very basic program in C++ which asked the user to input a number and then a string. To my surprise, when running the program it never stopped to ask for the string. It just skipped over it. After doing some reading on StackOverflow, I found out that I needed to add a line that said: cin.ignore(256, '\n'); before the line that gets the string input. Adding that fixed the problem and made the program work. My question is why does C++ need this cin.ignore() line and how can I predict when I will need to use cin.ignore() ? Here is the program I wrote: #include <iostream> #include <string

Read binary data from std::cin

筅森魡賤 提交于 2019-11-26 04:28:06
问题 What is the easiest way to read binary (non-formated) data from std::cin into either a string or a stringstream ? 回答1: std::cin is not opened with ios_binary. If you must use cin, then you need to reopen it, which isn't part of the standard. Some ideas here: http://compgroups.net/comp.unix.programmer/How-can-I-reopen-std-cin-and-std-cout-in-binary-mode. Once it's binary, you can use cin.read() to read bytes. If you know that in your system, there is no difference between text and binary (and

changing the delimiter for cin (c++)

风流意气都作罢 提交于 2019-11-26 04:22:56
I've redirected "cin" to read from a file stream cin.rdbug(inF.rdbug()) When I use the extraction operator it reads until it reaches a white space character. Is it possible to use another delimiter? I went through the api in cplusplus.com, but didn't find anything. It is possible to change the inter-word delimiter for cin or any other std::istream , using std::ios_base::imbue to add a custom ctype facet . If you are reading a file in the style of /etc/passwd, the following program will read each : -delimited word separately. #include <locale> #include <iostream> struct colon_is_space : std: