getline

Getting user input in C++ [closed]

馋奶兔 提交于 2019-11-28 10:39:07
I am writing a program that allows a student to write a question and store that Question (or string) in a variable, can anyone please tell me the best way to get user input thanks for your answers and comments Formatted I/O; taken from Baby's First C++ : #include <string> #include <iostream> int main() { std::string name; std::cout << "Enter your name: "; std::getline(std::cin, name); std::cout << "Thank you, '" << name << "'." << std::endl; } This isn't quite satisfactory, as many things can (and thus will) go wrong. Here's a slightly more watertight version: int main() { std::string name;

C++ 中getline(),getchar(),cin.getline,cin.get(),cin>>的区别

為{幸葍}努か 提交于 2019-11-28 03:56:50
getline的原型是:istream& getline(istream &is,string &str,char delim);第一个参数是输入流,第二个参数是储存读入内容的地址,第三个是终结标识 另外一个重载 :istream& getline(istream &is,string &str);其余和上面的相同,只是默认终结标识为‘\n’。 getchar:返回值为int,getchar函数读入的是缓冲区的字符,也就是当你在键盘上输入一系列的数,用回车结束,getchar会读取第一个字符,下次调用getchar,会自动调用下一个字符。 cin.getline():cin.getline(字符指针(char*),字符个数,结束符(char));结束符的默认值为‘\n’。功能:在输入流中读取字符串到第一个参数的字符数组中,可以读取空白字符,直到字符读取了‘n-1’个字符。如果没有设置第三个参数,则碰到回车键结束。 其中第‘n’个参数是‘\0’。所以它只能读取n-1个字符。 cin.get():cin.get与cin.getline类似,不同点在于前者把换行符丢在队列中,下次会继续读换行符。另外附上get的原型:读取单个字符:int get(); istream& get(char& c)//在流中读取一个字符,保存在c中,如果到文件末尾就返回一个空字符 读取字符串:istream&

difference between cin.get() and cin.getline()

天大地大妈咪最大 提交于 2019-11-28 03:45:04
问题 I am new to programming, and I have some questions on get() and getline() functions in C++. My understanding for the two functions: The getline() function reads a whole line, and using the newline character transmitted by the Enter key to mark the end of input. The get() function is much like getline() but rather than read and discard the newline character, get() leaves that character in the input queue. The book(C++ Primer Plus) that I am reading is suggesting using get() over getline() . My

Can an ANSI C-compliant implementation include additional functions in its standard library?

霸气de小男生 提交于 2019-11-27 23:07:21
Is an ANSI C-compliant implementation allowed to include additional types and functions in its standard library, beyond those enumerated by the standard? (An ideal answer would reference the relevant part of the ANSI standard.) I ask particularly because Mac OS 10.7 declares the getline function in stdio.h, even when compiling with gcc or clang using the -ansi flag. This breaks several older programs that define their own getline function. Is this a fault of Mac OS 10.7? (The man page for getline on Mac OS 10.7 says that getline conforms to the POSIX.1 standard, which came in 2008.) Edit: To

Reading getline from cin into a stringstream (C++)

我与影子孤独终老i 提交于 2019-11-27 18:32:05
问题 So I'm trying to read input like this from the standard input (using cin ): Adam English 85 Charlie Math 76 Erica History 82 Richard Science 90 My goal is to eventually store each data piece in its own cell in a data structure I have created, so basically I want to parse the input so each piece of data is individual. Since each row of input is inputted by the user one at a time, each time I get an entire row of input that I need to parse. Currently I am trying something like this:

C++ using getline() prints: pointer being freed was not allocated in XCode

青春壹個敷衍的年華 提交于 2019-11-27 18:06:03
问题 I'm trying to use std:getline() but getting a strange runtime error: malloc: * error for object 0x10000a720: pointer being freed was not allocated * set a breakpoint in malloc_error_break to debug This is the code that produces this error: //main.cpp #include <iostream> #include <sstream> int main (int argc, char * const argv[]) { std::istringstream my_str("demo string with spaces"); std::string word; while (std::getline(my_str, word, ' ')) { std::cout << word << std::endl; } return 0; }

awk

送分小仙女□ 提交于 2019-11-27 16:38:23
awk 是一种编程语言,用于在linux/unix下对文本和数据进行处理。数据可以来自标准输入(stdin)、一个或多个文件,或其它命令的输出。它支持用户自定义函数和动态正则表达式等先进功能,是linux/unix下的一个强大编程工具。它在命令行中使用,但更多是作为脚本来使用。awk有很多内建的功能,比如数组、函数等,这是它和C语言的相同之处,灵活性是awk最大的优势。 awk命令格式和选项 语法形式 awk [options] 'script' var=value file(s) awk [options] -f scriptfile var=value file(s) 常用命令选项 -F fs fs指定输入分隔符,fs可以是字符串或正则表达式,如-F: -v var=value 赋值一个用户定义变量,将外部变量传递给awk -f scripfile 从脚本文件中读取awk命令 -m[fr] val 对val值设置内在限制,-mf选项限制分配给val的最大块数目;-mr选项限制记录的最大数目。这两个功能是Bell实验室版awk的扩展功能,在标准awk中不适用。 awk模式和操作 awk脚本是由模式和操作组成的。 模式 模式可以是以下任意一个: /正则表达式/:使用通配符的扩展集。 关系表达式:使用运算符进行操作,可以是字符串或数字的比较测试。 模式匹配表达式:用运算符 ~ (匹配

Getline keeps on getting newline character. How can I avoid this?

谁说胖子不能爱 提交于 2019-11-27 15:11:34
问题 Basically I first takes an integer as input and then test case follows. My each test case is an string. I am suppose to print the string back if the starting patten of string matches "HI A" and it is case-insensitive. I wrote the code below to accomplish to this. My problem is that when I press enter after each input, getline takes newline character as new input. I have tried to tackle this by using extra getline after each input but the issue is still there. Program gets stuck in the loop

Are there alternate implementations of GNU getline interface?

心不动则不痛 提交于 2019-11-27 12:55:20
The experiment I am currently working uses a software base with a complicated source history and no well defined license. It would be a considerable amount of work to rationalize things and release under a fixed license. It is also intended to run a a random unixish platform, and only some of the libc's we support have GNU getline, but right now the code expects it. Does anyone know of a re-implementation of the GNU getline semantics that is available under a less restrictive license? Edit:: I ask because Google didn't help, and I'd like to avoid writing one if possible (it might be a fun

std::getline() returns

不想你离开。 提交于 2019-11-27 11:06:33
问题 I have a loop that reads each line in a file using getline() : istream is; string line; while (!getline(is, line).eof()) { // ... } I noticed that calling getline() like this also seems to work: while (getline(is, line)) What's going on here? getline() returns a stream reference. Is it being converted to a pointer somehow? Is this actually a good practice or should I stick to the first form? 回答1: The istream returned by getline() is having its operator void*() method implicitly called, which