getline

Weird behaviour with std::getline and std::vector<std::string>

匿名 (未验证) 提交于 2019-12-03 02:41:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I have the following code: std::vector<std::string> final_output; std::string input; int tries = 0; std::cin >> tries; int counter = 0; while(counter < tries) { std::getline(std::cin, input); final_output.push_back(input); ++counter; } Given the input: 3 Here Goes Here Goes 2 The output is: <blank line> Here Goes Here Goes 2 Weirdly, it seems to enter a blank line as input for the first time it runs. However, if I have the code as: int tries = 3; // explicitly specifying the number of tries int counter = 0; while(counter < tries) {} It works

c++ Read from .csv file

匿名 (未验证) 提交于 2019-12-03 02:31:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I have this code which is supposed to cout in console the information from the .csv file; while(file.good()) { getline(file, ID, ','); cout 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 program the console will display this: My question is why isn't the program repeating those cout messages in every line instead of only in the first one Btw , nome is name, idade is age, and genero/sexo is gender, forgot to translate before creating this post 回答1: You can follow this

undefined reference to `getline&#039; in c

匿名 (未验证) 提交于 2019-12-03 02:23:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I am learning to use getline in C programming and tried the codes from http://crasseux.com/books/ctutorial/getline.html #include #include #include 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

how to read extreme long lines from text file fast and safe in C++?

匿名 (未验证) 提交于 2019-12-03 02:20:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: There is a large text file of 6.53 GiB. Each line of it can be a data line or comment line. Comment lines are usually short, less than 80 characters, while a data line contains more than 2 million characters and is variable-length. Considering each data line needs to be dealt with as a unit, is there a simple way to read lines safe and fast in C++? safe (safe for variable-length data lines): The solution is as easy to use as std::getline() . Since the length is changing, it is hoped to avoid extra memory management. fast : The solution can

getline: template argument deduction/substitution failed

匿名 (未验证) 提交于 2019-12-03 01:44:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: I'm getting this error when I try to read the output of a command line by line: std :: string exec ( const char * cmd ) { FILE * pipe = popen ( cmd , "r" ); if (! pipe ) return "" ; char buffer [ 128 ]; std :: string result = "" ; while (! feof ( pipe )) { if ( fgets ( buffer , 128 , pipe ) != NULL ) { result += buffer ; } } pclose ( pipe ); return result ; } string commandStr = "echo Hello World" ; const char * command = commandStr . c_str (); std :: string output = exec ( command ); std :: string line ; while ( std :: getline (

cin&gt;&gt; not work with getline()

匿名 (未验证) 提交于 2019-12-03 01:39:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: #include #include 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 getline() call after getting the age and throwing

How to use the getline command in c++?

拥有回忆 提交于 2019-12-03 01:36:41
I'm trying to turn a cout command into a getline command in c++. This is my code that I'm trying to changes.... for (int count=0; count < numberOfEmployees; count++) { cout << "Name: "; cin >> employees[count].name; cout << "Title: "; cin >> employees[count].title; cout << "SSNum: "; cin >> employees[count].SSNum; cout << "Salary: "; cin >> employees[count].Salary; cout << "Withholding Exemptions: "; cin >> employees[count].Withholding_Exemptions; } I'm trying to change this line: cin >> employees[count].name; and this line: cin >> employees[count].title; into getlines. Can anyone help? Thanks

Nested do syntax

匿名 (未验证) 提交于 2019-12-03 01:22:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: In this question Will's answer states that the following code (let's call it code A ): reverse2lines :: IO () reverse2lines = do line1 <- getLine line2 <- getLine putStrLn ( reverse line2 ) putStrLn ( reverse line1 ) can be transformed into the following (let's call it code B ) : reverse2lines = do { line1 <- getLine ; do { line2 <- getLine ; do { putStrLn ( reverse line2 ) ; do { putStrLn ( reverse line1 ) } } } } I am confused. I understand, for example, that addOneInt :: IO () addOneInt = do line <- getLine putStrLn ( show ( 1 +

realloc: invalid next size and malloc: memory corruption (fast)

匿名 (未验证) 提交于 2019-12-03 00:57:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I am doing an exercise for fun from K and R C programming book. The program is for finding the longest line from a set of lines entered by the user and then prints it. Inputs: This is a test This is another long test this is another long testthis is another long test Observation: It runs fine for the first two inputs but fails for the larger string (3rd input) Errors: Error in `./longest': realloc(): invalid next size: 0x000000000246e010 *** Error in `./longest': malloc(): memory corruption (fast): 0x000000000246e030 *** My efforts: I have

Iterating with sscanf

女生的网名这么多〃 提交于 2019-12-02 23:19:10
问题 I do not know much about the sscanf function, but what I am trying to do is iterate through a line of integers. Given the variable char *lineOfInts I have made this the line that registers the users input. I am able to get the input fine, but when I try to use sscanf, I want to iterate through every int. I know that I can account through all the ints if i know how many ints there will be before hand like this.. sscanf(lineOfInts, "%d %d %d..etc", &i, &j, &k...) but what if I don't know how