Tag: getline

cin和getline跳过input

早些时候,我发布了一个关于cin跳过input的问题,我得到的结果是刷新,并使用istringstream ,但现在我尝试了所有可能的解决scheme,但没有一个工作。 这里是我的代码: void createNewCustomer () { string name, address; cout << "Creating a new customer…" << endl; cout << "Enter the customer's name: "; getline(cin, name); cout << "Enter the customer's address: "; getline(cin, address); Customer c(name, address, 0); CustomerDB::addCustomer(c); cout << endl; } 但我仍然得到同样的东西,跳过input,当它确实需要input时,它将把它们和名字存储在空的地方,并且在地址上,我把它写在名字上,但从第二个字母到结尾 我的代码有什么问题? 我尝试了cin.ignore() , cin.get()和cin.clear()所有这些都一起,独自一人,没有一个工作 编辑: main.cpp中的main方法仅调用mainMenu() void mainMenu () […]

在C ++中使用getline()

我有一个问题,使用getline方法来获取用户types的消息,我正在使用类似于: string messageVar; cout << "Type your message: "; getline(cin, messageVar); 然而,它并没有停止获得输出值,这是什么问题?

当多次调用时,c ++ getline()不会等待来自控制台的input

我试图从控制台,两个string,两个整数和一个双重获得一些用户input参数。 我试图使用的相关代码是这样的: #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 >> inputUInt; tempDVD.setDuration(inputUInt); cout << "Year: "; cin >> inputUInt; tempDVD.setYear(inputUInt); cout << "Price: $"; […]

从ifstream读取一行到一个stringvariables

在下面的代码中: #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 由于>>运算符读取第一个空白我得到这个输出。 我怎样才能提取行回到string? 我知道这种forms的istream& getline (char* s, streamsize n ); 但我想存储在一个stringvariables。 我怎样才能做到这一点?

C getline() – 如何处理缓冲区/如何将未知数量的值读入数组

首先,一些背景:我试图从外部文件中得到一个整数列表,并把它们放入一个数组中。 我正在使用getline逐行parsinginput文件: int lines = 0; size_t * inputBuffer = (size_t *) malloc(sizeof(size_t)); char * storage = NULL; 我打电话给getline像这样: getline(&storage, &s, input) 我从getline的man页面听到,如果你提供了一个size_t *缓冲区,当getline超过字节分配时,你可以让getline调整它。 我的问题是,你可以使用这个缓冲区? 它是否包含您使用getline()读取的所有项目? 从这个缓冲区中读取,或者在将这些整数放入一个数组时,以不同的方式来遍历input是更简单的吗? 谢谢!

何时以及为什么我需要在C ++中使用cin.ignore()?

我用C ++编写了一个非常基本的程序,要求用户input一个数字,然后input一个string。 令我惊讶的是,在运行程序时,它从来没有停止要求string。 它只是跳过它。 在对StackOverflow进行一些阅读之后,我发现我需要添加一行说: cin.ignore(256, '\n'); 在获取stringinput的行之前。 增加了这个问题,并使程序工作。 我的问题是为什么C ++需要这个cin.ignore()行,我怎么能预测什么时候我需要使用cin.ignore() ? 这是我写的程序: #include <iostream> #include <string> using namespace std; int main() { double num; string mystr; cout << "Please enter a number: " << "\n"; cin >> num; cout << "Your number is: " << num << "\n"; cin.ignore(256, '\n'); // Why do I […]

我不了解getline +string?

这是我第一次使用stackoverflow。 我一直无法find我需要关于getline的信息。 我在一个简单的编程类工程转让,所以我们写的代码是非常简单的。 我所要做的就是将用户定义的问题和答案数量放到两个不同的数组中。 我的while循环看起来像这样(我正在使用for循环,但切换到只是为了看看它会停止打破): int main () { srand((unsigned)time(0)); string quest1[100], answ1[100]; int size1, x = 0, num, count1, visit[100], shuffle[100]; fstream flashcard1; cout << "flashcard.cpp by NAME\n" << endl; cout << "This program allows user to manipulate questions and answers for studying.\n" << endl; cout << "\nHow many flash cards will be entered(MAX […]

getline不要求input?

这可能是一个非常简单的问题,但请原谅我,因为我是新的。 这是我的代码: #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? […]

为什么阅读stdin中的代码比C ++慢得多?

我想比较使用Python和C ++的stdinstringinput的读取行,并且震惊地看到我的C ++代码比等效的Python代码慢了一个数量级。 由于我的C ++是生锈的,我还不是一个专家Pythonista,请告诉我,如果我做错了什么或者我误解了一些东西。 (TLDR答案:包括声明: cin.sync_with_stdio(false)或者只是使用fgets代替。 TLDR的结果:滚动到我的问题的底部,看看表。) C ++代码: #include <iostream> #include <time.h> using namespace std; int main() { string input_line; long line_count = 0; time_t start = time(NULL); int sec; int lps; while (cin) { getline(cin, input_line); if (!cin.eof()) line_count++; }; sec = (int) time(NULL) – start; cerr << "Read " << line_count […]

在cin之后使用getline(cin,s)

我需要下面的程序来获取用户input的全部内容,并将其input到string名称中: cout << "Enter the number: "; int number; cin >> number; cout << "Enter names: "; string names; getline(cin, names); 然而,在getline()命令之前使用cin >> number命令(我猜是这个问题),它不会允许我input名字。 为什么? 我听说过一个cin.clear()命令,但我不知道这是如何工作的,或者为什么这是必要的。