cin

Basic String input

核能气质少年 提交于 2019-12-05 19:56:46
问题 I've just came across this bit of code that allows users to input strings in the command prompt. I'm aware of what they do and it's all great. But I have a question in regards to the cin and getline() functions. string name ; cout << "Please enter your full name: " ; cin >> name ; cout << "Welcome " << name << endl ; cout << "Please enter your full name again please: " ; getline(cin , name) ; cout << "That's better, thanks " << name << endl ; return 0 ; Now when this is output, I get

C++ Primer Plus(五)——循环和关系表达式

久未见 提交于 2019-12-05 08:41:33
优先级表表明,赋值运算符是从右向左结合的 定义一个const值来表示数组中元素的个数是一个好办法 对同一条语句的同一个值递增或递减多次,C++没有定义这种行为,也就是说这条语句在不同的系统上将生成不同的结果。 前缀运算符的效率高于后缀运算符 前缀运算符和解除引用运算符的优先级相同,以从右向左的方式结合; 后缀运算符的优先级高于前缀运算符和引用运算符,以从左向右的方式结合。 当C++语法只允许放一个表达式时,可使用逗号运算符将几个表达式合为一个,但不能将两个声明组合起来,可使用一个声明语句表达式来创建并初始化两个变量。 在所有运算符中,逗号表达式的优先级是最低的:它确保首先计算第一个表达式再计算第二个表达式,它的值是最后一个表达式的值。 C++提供了6种运算符来对数字进行比较,也可将这些运算符用于字符,但不能用于C-风格字符串,但可用于string类对象。 C-风格字符串应用strcmp( )函数来比较,若第一个字符串小于第二个则返回赋值,若大于返回正值,若相等返回0,即不等时是true,相等时是false。 C-风格的两个字符串即使存储在长度不同的数组中,也可能是相同的,这是因为C-风格字符串是通过结尾的空值字符定义的,而不是其所在的数组长度定义的。 C++一般使用for循环进行循环计数,在无法预先知道将执行的次数时,使用while循环。 for循环中省略了测试条件时

My cin is being ignored inside a while loop

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-05 08:12:52
I am trying to code a simple question and number checker into my first C++ program. Problem is, when I type a string like one two, or three, the program becomes and infinite loop and it ignores the cin function to re-assign lives to a number. cout << "How many lives would you like 1 (hard), 2 (medium), or 3 (easy)?" << endl; cin >> lives; while(lives != 1 && lives != 2 && lives != 3 && !isdigit(lives)) { cout << "You need to input a number, not words." << endl; cout << "How many lives would you like 1 (hard), 2 (medium), or 3 (easy)?" << endl; cin >> lives; } Here is my current code with your

Using cin in QtCreator

和自甴很熟 提交于 2019-12-05 02:07:00
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> using namespace std; int main() { string name; cout << "Enter name: "; cin >> name; cout << "Your name

Cin and Boolean input

三世轮回 提交于 2019-12-04 20:53:16
问题 I am new to C++ and I was wondering how the function cin in case of a boolean data works. Let's say for instance : bool a; cin >> a; I understand that if I give 0 or 1, my data a will be either true or false. But what happens if I give another integer or even a string ? I was working on the following code : #include <iostream> using namespace std; int main() { bool aSmile,bSmile; cout << "a smiling ?" << endl; cin >> aSmile; cout << "b smiling ?" << endl; cin >> bSmile; if (aSmile && bSmile =

C++ Input a char instead of int lead to endless loop. How to check the wrong input?

荒凉一梦 提交于 2019-12-04 19:31:00
One part of my program: Let user input a series of integer, and then put them into an array. int n=0; cout<<"Input the number of data:"; cin>>n; cout<<"Input the series of data:"; int a[50]; for(i=0; i<n; i++) { cin>>a[i]; } Then, when user input wrong data such as a character 'a' or 'b'. The program will go into an infinite loop. How to catch the wrong cin? How to clear the buffer and give user the chance to input a right data again? Simply check if the input is a number first and then append it to the array int x; std::cin >> x; while(std::cin.fail()) { std::cin.clear(); std::cin.ignore(std:

std::cin really slow

旧巷老猫 提交于 2019-12-04 15:13:57
问题 So I was trying to write myself a command for a linux pipeline. Think of it as a replica of gnu 'cat' or 'sed', that takes input from stdin, does some processing and writes to stdout. I originally wrote an AWK script but wanted more performance so I used the following c++ code: std::string crtLine; crtLine.reserve(1000); while (true) { std::getline(std::cin, crtLine); if (!std::cin) // failbit (EOF immediately found) or badbit (I/O error) break; std::cout << crtLine << "\n"; } This is exactly

C++ having cin read a return character

末鹿安然 提交于 2019-12-04 10:27:39
问题 I was wondering how to use cin so that if the user does not enter in any value and just pushes ENTER that cin will recognize this as valid input. 回答1: You will probably want to try std::getline : #include <iostream> #include <string> std::string line; std::getline( std::cin, line ); if( line.empty() ) ... 回答2: I find that for user input std::getline works very well. You can use it to read a line and just discard what it reads. The problem with doing things like this, // Read a number: std:

C++ cin fails when reading more than 127 ASCII values

て烟熏妆下的殇ゞ 提交于 2019-12-04 06:48:34
I've created a text file that has 256 characters, the first character of the text file being ASCII value 0 and the last character of the text value being ASCII value 255. The characters in between increment from 0 to 255 evenly. So character #27 is ASCII value 27. Character #148 should be ASCII value 148. My goal is to read every character of this text file. I've tried reading this with cin . I tried cin.get() and cin.read() , both of which are supposed to read unformatted input. But both fail when reading the 26th character. I think when I used an unsigned char , cin said it was reading read

std::cin skips white spaces

雨燕双飞 提交于 2019-12-04 06:46:22
So I am trying to write a function to check whether a word is in a sentence, by looping through a char array and checking for the same string of char's. The program works as long as the Sentence doesn't have any spaces. I googled around and they are all the same suggestions; cin.getline But however I implement it, it either doesn't run or skips the entire input and goes straight towards the output. How can I account for spaces? #include <iostream> using namespace std; bool isPartOf(char *, char *); int main() { char* Word= new char[40]; char* Sentence= new char[200]; cout << "Please enter a