cin

The C++ equivalent of C's format string [closed]

安稳与你 提交于 2019-12-02 23:56:34
问题 Closed . This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post. Closed 3 years ago . I have a C program that reads from keyboard, like this: scanf("%*[ \t\n]\"%[^A-Za-z]%[^\"]\"", ps1, ps2); For a better understanding of what this instruction does, let's split the format string as follows: %*[ \t\n]\" => read all spaces, tabs and newlines ( [ \t\n] ) but not store them in any

C++设计缺陷 : cin/cout wcin/wcout

匿名 (未验证) 提交于 2019-12-02 22:56:40
#include<iostream> using std::cin; using std::cout; using std::wcin; using std::wcout; using std::endl; int main() { char *a = new char[100]; wchar_t *b = new wchar_t[100]; cin >> a; wcin >> b; cout << a << endl; wcout << b <<endl; delete[] a; delete[] b; return 0; } #include<iostream> #include<string.h> using namespace std; int main() { ios::sync_with_stdio(false); std::locale::global(std::locale("zh_CN.UTF8")); char *a = new char[100]{'\0'}; wchar_t *b = new wchar_t[100]{L'\0'}; //cin >> a; //cin.clear(); wcin >> b; //cout << a << endl; //cout.clear(); wcout << b <<endl; //wcout.clear(); /

C++ Primer Plus(四)——复合类型

余生颓废 提交于 2019-12-02 20:15:29
只能在定义数组时才能初始化,不能将一个数组赋值给另一个数组,但可以使用下标分别赋值给数组元素,但可以将一个string对象赋值给另一个string对象 如果只对数组的一部分初始化,其他元素自动设置为0 C++11可在初始化的大括号里不包含任何东西,这将把所有元素设置为0 C++11在数组列表初始化时,禁止缩窄转换 C-风格字符串以\0结尾,不是\0结尾的字符数组不是字符串 任何两个由空白(空格,制表符,换行符)分隔的字符串常量都将自己拼成一个 sizeof运算符指出整个数组的长度,而strlen()指出存储在数组中的字符串的长度 cin使用 空白(空格,制表符,换行符)确定字符串的结束位置,面向单词 cin的get( )函数和getline( )函数,面向行: getline( )函数丢弃换行符,在读取指定数目-1的字符或遇到换行符时停止读取 get( )函数的一种变体和getline函数参数相同,但保留换行符,在再次读取字符时要先使用无参数的get()函数读取换行符后才能正确读取字符,再使用clear( )函数来恢复输入 可以使用C-风格字符串来初始化string对象,使用cin来将键盘输入存储到string对象中,使用cout来显示string对象,使用数组表示法来访问存储在string对象中的字符 C++将"(和)"作为界定符, 使用前缀R 来标识原始字符串(不转义)

Cout of a string is giving an error and a hard time some insight help pls?

爱⌒轻易说出口 提交于 2019-12-02 18:34:34
问题 I cant find the error in this piece of code could anyone please insight me? I ran the debugging but the errors are un-understandable.. #include "stdafx.h" #include <iostream> using namespace std; int main() { string name; cout << "Input your name please?" << endl; cin >> name; if { (name == "Bart Simpson") cout << "You have been very naughty" << endl; } return 0; } 回答1: Problems: You have some missing #include s, which probably caused your initial compiler errors. You have a simple syntax

C++: how do I check if the cin buffer is empty?

耗尽温柔 提交于 2019-12-02 17:30:15
How do you check to see if the user didn't input anything at a cin command and simply pressed enter? When reading from std::cin , it's preferable not to use the stream extraction operator >> as this can have all sorts of nasty side effects. For example, if you have this code: std::string name; std::cin >> name; And I enter John Doe , then the line to read from cin will just hold the value John , leaving Doe behind to be read by some future read operation. Similarly, if I were to write: int myInteger; std::cin >> myInteger; And I then type in John Doe , then cin will enter an error state and

Limiting the number of characters of user input

只谈情不闲聊 提交于 2019-12-02 17:00:35
问题 I'm trying to limit # of characters a user can input. It's not like when user inputs abcde , and I limit the input length to be 3, and only abc is taken into account. Is there a way to physically limit user from inputting more than certain amount of characters? For example, if user trys to type 12345 , and if I limit it to 3 characters, only 123 gets typed. I've tried the following code: cin.width(5); cin >> n; But I've realized it doesn't physically limit the user input, but only limits the

Why does it look like that cin converts my double input into integer? C++

北战南征 提交于 2019-12-02 16:44:50
问题 I have the following code: //... int variable_1, variable_2; cout << "Please enter the 2 numbers: "; try { if ( !(cin >> variable_1) ) throw Invalid_number(); //empty class just for exceptions if ( !(cin >> variable_2) ) throw Invalid_number(); //problem is here. } catch (Invalid_number) {cerr << "You must enter integers\n"; return -1;} //... So if I enter an integer for variable_1 and a double for variable_2, the programme doesn't throw the exception, it just continues executing the rest of

while loop to infinity when the input of a cin is a 'dot'

送分小仙女□ 提交于 2019-12-02 16:40:33
问题 I am having trouble using the cin method to acquire a variable. When the input is a number there is no problem, but when it is a special character like a dot [.], the whileloop loops into infinity. What am I doing wrong? cout << "What is your race" <<endl<<"1.Human\n2.troll\n3.zombie"<<endl; cin >> *race; while(*race<1||*race>3) { system("cls"); cout << "Wrong choice"<<endl<< "What is your race" <<endl<<"1.Human\n2.troll\n3.zombie"<<endl; cin >> *race; } I searched for the answer and i should

The C++ equivalent of C's format string [closed]

本秂侑毒 提交于 2019-12-02 15:17:06
I have a C program that reads from keyboard, like this: scanf("%*[ \t\n]\"%[^A-Za-z]%[^\"]\"", ps1, ps2); For a better understanding of what this instruction does, let's split the format string as follows: %*[ \t\n]\" => read all spaces, tabs and newlines ( [ \t\n] ) but not store them in any variable (hence the ' * '), and will keep reading until encounter a double quote ( \" ), however the double quote is not input. Once scanf() has found the double quote, reads all caracters that are not letters into ps1. This is accomplished with... %[^A-Za-z] => input anything not an uppercase letter 'A'

How to read a single line of numbers into different variables? [closed]

左心房为你撑大大i 提交于 2019-12-02 14:51:37
Program looks like: #include <iostream> #include <string> using namespace std; int main() { //Code int num1, num2, num3, num4, num5, num6; int num[6] = { num1, num2, num3, num4, num5, num6 }; cout << "Enter one line containing at least 6 integers." << endl; getline(cin, num); Line of input: 1 2 87 1 2 123 44 And I need to store each number into variables Num1, Num2, Num3, etc. From your output message, it seems like you're expecting at least 6 integers as input. That means you want a container that you can add an arbitrary number of elements to, like std::vector<int> Nums; . You can then use