getline

What am I not understanding about getline+strings?

蹲街弑〆低调 提交于 2019-11-26 04:54:53
问题 This is my first time using stackoverflow. I\'ve been unable to find out the information I need regarding getline. I\'m in a simple programming class for engineering transfers so the code that we write is pretty simple. All i\'m trying to do here is put a user-defined number of questions and answers into two different arrays. My while loop looks like this (I was using a for loop but switched to while just to see if it would stop breaking): int main () { srand((unsigned)time(0)); string quest1

Using getline() in C++

自闭症网瘾萝莉.ら 提交于 2019-11-26 03:54:55
问题 I have a problem using getline method to get a message that user types, I\'m using something like: string messageVar; cout << \"Type your message: \"; getline(cin, messageVar); However, it\'s not stopping to get the output value, what\'s wrong with this? 回答1: If you're using getline() after cin >> something , you need to flush the newline character out of the buffer in between. You can do it by using cin.ignore() . It would be something like this: string messageVar; cout << "Type your message

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

不想你离开。 提交于 2019-11-26 03:44:32
问题 When should std::cin.getline() be used? What does it differ from std::cin ? 回答1: In case with char*, std::cin.getline getting line, instead of std::cin getting first word. 回答2: 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

getline not asking for input? [duplicate]

笑着哭i 提交于 2019-11-26 02:01:49
问题 This question already has answers here : Need help with getline() [duplicate] (7 answers) Closed 4 years ago . 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;

C getline() - how to deal with buffers / how to read unknown number of values into array

青春壹個敷衍的年華 提交于 2019-11-26 01:47:47
问题 First of all, some background: I\'m trying to get in a list of integers from an external file and put them into an array. I am using getline to parse the input file line by line: int lines = 0; size_t * inputBuffer = (size_t *) malloc(sizeof(size_t)); char * storage = NULL; I am calling getline like so: getline(&storage, &s, input) I heard from the man page on getline that if you provide a size_t * buffer, you can have getline resize it for you when it exceeds the byte allocation. My question

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

孤者浪人 提交于 2019-11-26 01:13:08
问题 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

Need help with getline() [duplicate]

冷暖自知 提交于 2019-11-25 23:37:55
问题 This question already has answers here : Why does std::getline() skip input after a formatted extraction? (3 answers) Closed 15 days ago . Is there a reason why if in my program I am asking the user for input, and I do: int number; string str; int accountNumber; cout << \"Enter number:\"; cin >> number; cout << \"Enter name:\"; getline(cin, str); cout << \"Enter account number:\"; cin >> accountNumber; Why after inputting the first number, it outputs \"Enter Name\", followed immediately by \

Why is reading lines from stdin much slower in C++ than Python?

蹲街弑〆低调 提交于 2019-11-25 23:01:44
问题 I wanted to compare reading lines of string input from stdin using Python and C++ and was shocked to see my C++ code run an order of magnitude slower than the equivalent Python code. Since my C++ is rusty and I\'m not yet an expert Pythonista, please tell me if I\'m doing something wrong or if I\'m misunderstanding something. (TLDR answer: include the statement: cin.sync_with_stdio(false) or just use fgets instead. TLDR results: scroll all the way down to the bottom of my question and look at

C getline() - how to deal with buffers / how to read unknown number of values into array

心不动则不痛 提交于 2019-11-25 22:54:05
First of all, some background: I'm trying to get in a list of integers from an external file and put them into an array. I am using getline to parse the input file line by line: int lines = 0; size_t * inputBuffer = (size_t *) malloc(sizeof(size_t)); char * storage = NULL; I am calling getline like so: getline(&storage, &s, input) I heard from the man page on getline that if you provide a size_t * buffer, you can have getline resize it for you when it exceeds the byte allocation. My question is, what can you use this buffer for? Will it contain all of the items that you read with getline()? Is

Using getline(cin, s) after cin

旧巷老猫 提交于 2019-11-25 21:57:13
问题 I need the following program to take the entire line of user input and put it into string names: cout << \"Enter the number: \"; int number; cin >> number; cout << \"Enter names: \"; string names; getline(cin, names); With the cin >> number command before the getline() command however (which I\'m guessing is the issue), it won\'t allow me to input names. Why? I heard something about a cin.clear() command, but I have no idea how this works or why this is even necessary. 回答1: cout << "Enter the