getline

Coding a getline() implementation - Valgrind errors

对着背影说爱祢 提交于 2019-12-18 09:42:05
问题 I have to recode an implementation of the getline() function, but using the file descriptor of the file and not a FILE * . I am only allowed to use malloc() and free() , along with 5 functions being 25 lines long at most. I think I've done correctly the project although I am a beginner in C and my code isn't probably good. When I run it, it works fine, but valgrind shows that I definetely lost x bytes , x depending of the file length and the READ_SIZE (macro defined in the header). According

Reading multiple lines from a file using getline()

半城伤御伤魂 提交于 2019-12-18 08:20:51
问题 I am trying to read in and then output the contents of a text file with three lines, as follows: Bob Dylan 10 9 John Lennon 8 7 David Bowie 6 5 For each line, I just want to output the line, i.e. firstName LastName number1 number2. I'm using the following code for this: int num1; int num2; string firstName; string lastName; string fullName; ifstream inFile; inFile.open("inputFile.txt"); while (getline(inFile, firstName)) { inFile >> firstName >> lastName >> num1 >> num2; fullName = firstName

How to read until ESC button is pressed from cin in C++

狂风中的少年 提交于 2019-12-18 05:17:16
问题 I'm coding a program that reads data directly from user input and was wondering how could I read all data until ESC button on keyboard is pressed. I found only something like this: std::string line; while (std::getline(std::cin, line)) { std::cout << line << std::endl; } but need to add a portable way (Linux/Windows) to catch a ESC button pressed and then break a while loop. How to do this? EDIT: I wrote this, but still - works even if I press an ESC button on my keyboard: #include <iostream>

std::getline does not work inside a for-loop

寵の児 提交于 2019-12-18 04:17:36
问题 I'm trying to collect user's input in a string variable that accepts whitespaces for a specified amount of time. Since the usual cin >> str doesn't accept whitespaces, so I'd go with std::getline from <string> Here is my code: #include <iostream> #include <vector> #include <string> #include <algorithm> using namespace std; int main() { int n; cin >> n; for(int i = 0; i < n; i++) { string local; getline(cin, local); // This simply does not work. Just skipped without a reason. //...............

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

本小妞迷上赌 提交于 2019-12-17 09:36:25
问题 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 <<" "<< (

cin and getline skipping input [duplicate]

狂风中的少年 提交于 2019-12-16 19:03:06
问题 This question already has answers here : Why does std::getline() skip input after a formatted extraction? (3 answers) Closed 3 years ago . earlier i posted a question about cin skipping input, and I got results to flush, and use istringstream , but now I tried every possible solution but none of them work. here is my code: void createNewCustomer () { string name, address; cout << "Creating a new customer..." << endl; cout << "Enter the customer's name: "; getline(cin, name); cout << "Enter

getline() method no instance of overloaded function

江枫思渺然 提交于 2019-12-14 03:34:34
问题 I am having some problem when trying to do classes for C++. This is my header file.h: #include <iostream> #include <string> #ifndef MESSAGES__H__ #define MESSAGES__H__ class Message { public: Message(std::string recipient, std::string sender); void append(std::string text); std::string to_string() const; void print() const; private: std::string recipient; std::string sender; std::string message; std::string text_input; char* timestamp; }; #endif And when I run the main method, the getline(cin

How do I stop program from skipping over getline? [duplicate]

我是研究僧i 提交于 2019-12-13 15:55:45
问题 This question already has answers here : Why does std::getline() skip input after a formatted extraction? (3 answers) Closed 5 years ago . This is my main program, int main () { string command; cin>>command; if(command == "keyword") { string str, str2, str3, str4; cout << "Enter first name: "; getline (cin,str); cout << "Enter last name: "; getline (cin,str2); cout << "Enter age: "; getline (cin,str3); cout<<"Enter country: "; getline (cin,str4); cout << "Thank you, " << str <<" "<<str2 <<" "

How to take a line as an input and not include spaces

陌路散爱 提交于 2019-12-13 06:55:53
问题 I'm working on a project to convert from postfix to infix expressions. I was stuck for a while but I had part of it working then I realized I needed to inlcude a space between each of the operands when I input it to the user.I'm not sure how to take in a string and not include spaces how would I go about doing that. getline doesn't work as it includes spaces. therefore instead of ab+ I need to accept it as: a b +. i'm not sure how to do this not include the strings. Here is my code so far.

Assigning the output of cin to variables

こ雲淡風輕ζ 提交于 2019-12-13 00:25:05
问题 I'm trying to learn how to use cin and getline to write a paper grading program that I can use at school. It's kind of a tricky project for a beginner but it lets me know what I need to learn and this is the first thing I need to do. int main() { string grader; int x; cout << "Who will I be assisting today? "; getline (cin, grader); cout << "Hello " << grader << ".\n"; cout << "How manny questions are on the test you will be grading? "; getline (cin, x); cout << "this is a " << x << "question