getline

std::cin.getline( ) vs. std::cin

帅比萌擦擦* 提交于 2019-11-26 14:39:11
When should std::cin.getline() be used? What does it differ from std::cin ? In case with char*, std::cin.getline getting line, instead of std::cin getting first word. MSalters Let's take std::cin.getline() apart. First, there's std:: . This is the namespace in which the standard library lives. It has hundreds of types, functions and objects. std::cin is such an object. It's the standard character input object, defined in <iostream> . It has some methods of its own, but you can also use it with many free functions. Most of these methods and functions are ways to get one or more characters from

checking for eof in string::getline

廉价感情. 提交于 2019-11-26 10:32:51
问题 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. 回答1: 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) } 回答2: Just read and then

Going through a text file line by line in C

↘锁芯ラ 提交于 2019-11-26 10:18:52
问题 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)){

c++ getline() isn&#39;t waiting for input from console when called multiple times

戏子无情 提交于 2019-11-26 09:00:50
问题 I\'m attempting to get a few user-input parameters from the console, two strings, two ints and a double. The relevant code I\'m trying to use is this: #include <string> #include <iostream> using namespace std; // ... string inputString; unsigned int inputUInt; double inputDouble; // ... cout << \"Title: \"; getline(cin, inputString); tempDVD.setTitle(inputString); cout << \"Category: \"; getline(cin, inputString); tempDVD.setCategory(inputString); cout << \"Duration (minutes): \"; cin >>

getline not asking for input? [duplicate]

自古美人都是妖i 提交于 2019-11-26 08:57:22
This question already has an answer here: Need help with getline() 7 answers This is probably a very simple problem but forgive me as I am new. Here is my code: #include <iostream> #include <string> #include <sstream> using namespace std; int main () { string name; int i; string mystr; float price = 0; cout << "Hello World!" << endl; cout << "What is your name? "; cin >> name; cout << "Hello " << name << endl; cout << "How old are you? "; cin >> i; cout << "Wow " << i << endl; cout << "How much is that jacket? "; getline (cin,mystr); stringstream(mystr) >> price; cout << price << endl; system(

undefined reference to `getline&#39; in c

微笑、不失礼 提交于 2019-11-26 08:29:28
问题 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

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

陌路散爱 提交于 2019-11-26 08:09:01
问题 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

reading a line from ifstream into a string variable

烂漫一生 提交于 2019-11-26 07:38:39
问题 In the following code : #include <iostream> #include <fstream> #include <string> using namespace std; int main() { string x = \"This is C++.\"; ofstream of(\"d:/tester.txt\"); of << x; of.close(); ifstream read(\"d:/tester.txt\"); read >> x; cout << x << endl ; } Output : This Since >> operator reads upto the first whitespace i get this output. How can i extract the line back into the string ? I know this form of istream& getline (char* s, streamsize n ); but i want to store it in a string

c++ Read from .csv file

北城以北 提交于 2019-11-26 06:46:54
问题 I have this code which is supposed to cout in console the information from the .csv file; while(file.good()) { getline(file, ID, \',\'); cout << \"ID: \" << ID << \" \" ; getline(file, nome, \',\') ; cout << \"User: \" << nome << \" \" ; getline(file, idade, \',\') ; cout << \"Idade: \" << idade << \" \" ; getline(file, genero, \' \') ; cout << \"Sexo: \" << genero<< \" \" ; } And a csv file that has this (when I open with notepad): 0,Filipe,19,M 1,Maria,20,F 2,Walter,60,M Whenever I run the

When and why do I need to use cin.ignore() in C++?

自作多情 提交于 2019-11-26 06:37:45
I wrote a very basic program in C++ which asked the user to input a number and then a string. To my surprise, when running the program it never stopped to ask for the string. It just skipped over it. After doing some reading on StackOverflow, I found out that I needed to add a line that said: cin.ignore(256, '\n'); before the line that gets the string input. Adding that fixed the problem and made the program work. My question is why does C++ need this cin.ignore() line and how can I predict when I will need to use cin.ignore() ? Here is the program I wrote: #include <iostream> #include <string