getline

cin>> not work with getline()

烂漫一生 提交于 2019-11-27 08:49:11
问题 #include <iostream> #include <string> using namespace std; int main () { string str; int age; cout << "Please enter age: "; cin>>age; cout << "Please enter full name: "; getline (cin,str); cout << "Thank you, " << str << ".\n"; } Why function getline() not work when I using uperator >> to input integer ? What is better use for int input ? 回答1: You still have a newline in the stream after cin>>age; , which is giving you an empty string for the name. You could solve it by just adding another

cin.getline() is skipping an input in C++

末鹿安然 提交于 2019-11-27 08:09:31
If I use the following code, getline doesn't take the last input(for last iteration of "for" loop, it simply skips it) - int main() { int n; map<string, set<string> > lst; string c,s,c2; cin>>n; for(int i=0;i<n;i++) { getline(cin,c); // here it skips the input for last iteration stringstream ss; ss<<c; bool f=1; while(ss>>s) { if(f) { c2=s; f=0; } else lst[c2].insert(s); } } for (map<string, set<string> >::const_iterator ci = lst.begin(); ci != lst.end(); ++ci) { cout<< (*ci).first <<" "<< (*ci).second.size() <<endl; } } To get rid of it, I put cin.ignore() after getline. Now its taking all

getline() does not work if used after some inputs [duplicate]

前提是你 提交于 2019-11-27 07:39:26
Possible Duplicate: Need help with getline() getline() is not working, if I use it after some inputs, i.e. #include<iostream> using namespace std; main() { string date,time; char journal[23]; cout<<"Date:\t"; cin>>date; cout<<"Time:\t"; cin>>time; cout<<"Journal Entry:\t"; cin.getline(journal,23); cout<<endl; system("pause"); } where as if I use getline() on top of inputs, it does work i.e. cout<<"Journal Entry:\t"; cin.getline(journal,23); cout<<"Date:\t"; cin>>date; cout<<"Time:\t"; cin>>time; What might be the reason? Characters are extracted until either (n - 1) characters have been

Going through a text file line by line in C

筅森魡賤 提交于 2019-11-27 06:22:48
I have been working on a small exercise for my CIS class and am very confused by the methods C uses to read from a file. All that I really need to do is read through a file line by line and use the information gathered from each line to do a few manipulations. I tried using the getline method and others with no luck. My code is currently as follows: int main(char *argc, char* argv[]){ const char *filename = argv[0]; FILE *file = fopen(filename, "r"); char *line = NULL; while(!feof(file)){ sscanf(line, filename, "%s"); printf("%s\n", line); } return 1; } Right now I am getting a seg fault with

1022 Digital Library (30 分)

只谈情不闲聊 提交于 2019-11-27 05:43:31
1022 Digital Library (30 分) A Digital Library contains millions of books, stored according to their titles, authors, key words of their abstracts, publishers, and published years. Each book is assigned an unique 7-digit number as its ID. Given any query from a reader, you are supposed to output the resulting books, sorted in increasing order of their ID's. Input Specification: Each input file contains one test case. For each case, the first line contains a positive integer N ( ≤) which is the total number of books. Then N blocks follow, each contains the information of a book in 6 lines: Line

getline使用问题

南楼画角 提交于 2019-11-27 04:42:54
1.输入string string s1; getline(cin,s1); cin>>s1; //注意cin遇到空格会终止,而getline不会 2.关于吞回车问题 输入n后要记得吞回车,不然只能输入两个字符串就结束 cin>>n; getchar();//加getchar()把输入n时的回车吃掉 getline(cin,s1); getline(cin,s2); getline(cin,s3); 来源: https://www.cnblogs.com/1911087165zzx/p/11343709.html

checking for eof in string::getline

我的未来我决定 提交于 2019-11-27 03:34:23
How do I check for end-of-file using the std::getline function? If I use eof() it won't signal eof until I attempt to read beyond end-of-file. The canonical reading loop in C++ is: while (getline(cin, str)) { } if (cin.bad()) { // IO error } else if (!cin.eof()) { // format error (not possible with getline but possible with operator>>) } else { // format error (not possible with getline but possible with operator>>) // or end of file (can't make the difference) } Just read and then check that the read operation succeeded: std::getline(std::cin, str); if(!std::cin) { std::cout << "failure\n"; }

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

一曲冷凌霜 提交于 2019-11-26 23:15:34
问题 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

undefined reference to `getline' in c

大兔子大兔子 提交于 2019-11-26 22:51:23
I am learning to use getline in C programming and tried the codes from http://crasseux.com/books/ctutorial/getline.html #include <stdio.h> #include <stdlib.h> #include <string.h> int main(int atgc, char *argv[]) { int bytes_read = 1; int nbytes = 10; char *my_string; my_string = (char *)malloc(nbytes+1); puts("Please enter a line of text"); bytes_read = getline(&my_string, &nbytes, stdin); if (bytes_read == -1) { puts ("ERROR!"); } else { puts ("You typed:"); puts (my_string); } return 0; } However, the problem is that the compiler keeps returning errors of this: undefined reference to

getline not working properly ? What could be the reasons? [duplicate]

让人想犯罪 __ 提交于 2019-11-26 22:00:55
Possible Duplicate: getline not asking for input? There is some unique thing happening in my program. Here are some set of commands : cout << "Enter the full name of student: "; // cin name getline( cin , fullName ); cout << "\nAge: "; // cin age int age; cin >> age ; cout << "\nFather's Name: "; // cin father name getline( cin , fatherName ); cout << "\nPermanent Address: "; // cin permanent address getline( cin , permanentAddress ); When i try to run this snippet along with the whole code.The output program works like : output: Enter the full name of student: Age: 20 Father's Name: Permanent