标签: getline

在c ++中读取文本文件到数组的输入

好吧,温柔,因为我非常喜欢编程.到目前为止,我只学习了C++,并且我正在运行Visual Studio 2010作为我的编译器.对于这个程序,我试图从文本输入文件中读取并将信息写入一组三个数组.一个阵列将处理名称列表,另外两个阵列分别用于工作小时数和小时工资率.我将使用后两者来计算一组收入,并将整个事物输出到另一个文本文件作为报告.然而,我的问题是获取第一个数组的输入.我正在使用的输入文件的文本排列如下:

J. Doe*35 12.50 J. Dawn*20 10.00 .........

由于我试图使用ifstream getline来获取星号充当分隔符的名称,并将以下两个数字写入另外两个数组,因此这些名称会被星号所拖尾.后两个值由空格分隔,所以我认为它们不会引起任何问题.我确定还有其他错误需要处理,但我需要先处理第一个错误才能开始调试其余的错误.

我的问题出现在我调用inFile.getline的行,因为我收到以下错误:错误C2664:'std :: basic_istream <_Elem,_Traits>&std :: basic_istream <_Elem,_Traits> :: getline(_Elem*,std :: streamsize,_Elem)':无法将参数1从'std :: string'转换为'char*'.

从我在其他地方读到的,(我认为)问题源于尝试将字符串写入char数组,由于它们具有不同的数据类型,因此无法工作.我不确定是否存在其他可行的方法来获取名称,因为我需要使用分隔符将名称与数值分开.如何解决这个问题的任何建议将不胜感激.

这是我写的来源:

#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;

const int EMP_NUM = 5;
const int BASE_HOURS = 40;
const char N_SIZE = 8;

int main()
{
 int i;
 double rEarnings, oEarnings, tEarnings,
 trEarnings, toEarnings, ttEarnings;
 ifstream inFile;
 ofstream outFile;
 inFile.open("records.txt");
 outFile.open("report.txt");

 outFile << setprecision(2) << showpoint << fixed;

 outFile << …
Run Code Online (Sandbox Code Playgroud)

c++ arrays fstream getline ifstream

-1
推荐指数
1
解决办法
5660
查看次数

用C++获取用户输入

我正在编写一个程序,允许学生写一个问题并将问题(或字符串)存储在变量中,任何人都可以告诉我获取用户输入的最佳方法

感谢您的回答和评论

c++ string cin getline

-1
推荐指数
1
解决办法
3万
查看次数

C++问题 - getline跳过第一个输入

问题是使用的部分getline()第一次没有输入,它只是说"输入一个字符串:输入一个字符串:"然后你可以把输入放在那里.

#include <iostream>
#include <string>

using namespace std;

int main()
{
  int nums[100], key=0, num = 0;

  while(num != -1)
  {
    cout << "Enter a positive integer (-1 to exit): ";
    cin >> num;

    if(num != -1)
    {
      nums[key] = num;
      key++;
    }

  }

    if(num != -1)
    {
      nums[key] = num;
      key++;
    }

    int numElements = key;
    string inStrings[100];

    for(int i=0; i < numElements; i++)
    {
      cout << "\n";
      cout << "Enter a string: ";
      getline(cin, inStrings[i]);
    } …
Run Code Online (Sandbox Code Playgroud)

c++ getline

-1
推荐指数
1
解决办法
5072
查看次数

尝试从getline分配字符时出现细分错误

所以,我正在使用getline(cin, str);当时的句子,然后string text[str.length()];在for语句中,当我想要组合它们时(text[e] = text[e] + str[i];)我得到了分段错误.完整代码:

#include <cstdlib>
#include <stdio.h>
#include <iostream>

using namespace std;

int main()
{
    string str;
    int i, e, space = 0;

    getline(cin, str);
    string text[str.length()];

    for(i=0; i<str.length(); i++) {
        if(str[i]==' ') {
            space++;
            e++;
        }
        else {
            text[e] = text[e] + str[i];
        }
    }

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c++ getline segmentation-fault

-1
推荐指数
1
解决办法
145
查看次数

C:变量变为0,只需在for循环中调用getline

在下面的代码(我的源代码的简化版本)中,在声明时lv初始化为1右的变量,getlinefor循环内部调用时神秘地变为0 .printf如果您输入任何内容"cl",或者通过gdb,您可能会注意到它.变量在getline调用后立即更改值.最奇怪的是,当"cl"提示输入时,你可以通过gdb看到之后lv转向的值,然后在语句中正确设置,然后保存用于后续循环,无论你输入什么.0getline3if

此外,如果您将声明更改为static int lv = 1;然后预期的行为会出现(lv保持到1"手动"更改).

有人可以告诉我到底是怎么回事?

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main ( void ) {
  char* line=NULL;
  int len;
  int lv = 1;
  for(;;) { 
    printf("Level: %d\n", lv);

    getline(&line, (size_t*)&len, stdin);

    if (line[strlen(line) - 1] == '\n') {
      line[strlen(line) - 1] = '\0';
    }
    if (strcmp(line, "cl")==0) …
Run Code Online (Sandbox Code Playgroud)

c variables static for-loop getline

-1
推荐指数
1
解决办法
74
查看次数

来自文本文件错误的getline()

对于学校的实验室,我试图从文本文件中获取行并显示它们.

我的开始:

#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
#include <playlist.h>

using namespace std;

void readLine(vector<string> playlist);
int totalTime();
void displayData();

int main()
{
    vector<string> playlist;

    readLine(playlist);
    totalTime();
    displayData();

    system("pause");

    return 0;
}
void readLine(vector<string> playlist)
{
    string currentline;
    int i = 0;
    while (getline("Playlist.txt", currentline) && !empty(currentline))
    {
        getline("Playlist.txt", playlist[i]);
        i = i + 1;
    }
}
int totalTime() 
{

}
void displayData()
{

}
Run Code Online (Sandbox Code Playgroud)

我似乎误用了getline?这两个"getline"有错误强调说:

函数getline

错误:没有重载函数"getline"匹配参数列表参数类型的实例是:(const char [13],std :: string)

我不知道错误的原因.

c++ text getline

-1
推荐指数
1
解决办法
127
查看次数

具有多个测试用例的 getline 问题

我想打印字符串中每个单词的第一个字母。我已经使用 getline 函数来获取带空格的字符串。它适用于单个测试用例,但不适用于多个测试用例。请帮助为什么会发生这种情况,并在可能的情况下提出解决方案以获得多个测试用例的答案。

#include<bits/stdc++.h> 
using namespace std; 

string firstLetterWord(string str) { 
      string result = "";  
      if(str[0]!=' ')result.push_back(str[0]); 
    for (int i=1; i<str.length(); i++) {  
        if (str[i] != ' ' && str[i-1] == ' ') { 
            result.push_back(str[i]);    
        } 
    } 
    return result; 
} 
int main() { 
   string str;
   getline(cin,str);
   cout << firstLetterWord(str); 
   return 0; 
} 
Run Code Online (Sandbox Code Playgroud)

如果我输入 't' 测试用例的数量,然后找到字符串的答案,那么代码只给出第一个测试用例的答案。

c++ string getline

-1
推荐指数
1
解决办法
1142
查看次数

getline没有匹配功能

当我只想在blankspace之前读取字符时,使用:' cin >> buffer.但是,我想读取用户输入的所有内容,包括空格.所以,我从使用">>"更改为getline调用.我似乎传递了正确的参数,我已经完成了#include和#include.编译时的错误消息是:

Driver.cpp:在函数'int main(int,char*const*)'中:Driver.cpp:49:44:错误:没有用于调用'getline(std :: istream&,char [1024])'getline的匹配函数(cin,buffer); ^ Driver.cpp:49:44:注意:候选者是:/usr/include/wchar.h:4:0中包含的文件,来自/usr/lib/gcc/x86_64-pc-cygwin/4.8.2/include/c ++/cwchar:44,来自/usr/lib/gcc/x86_64-pc-cygwin/4.8.2/include/c++/bits/postypes.h:40,来自/ usr/lib/gcc/x86_64-pc-cygwin来自/ usr/lib/gcc/x86_64 -cygwin/4.8.2/include/c ++/ostream:38,来自/usr/lib/gcc/x86_64-pc-cygwin/4.8.2/include/c++/iostream:39,来自Driver.cpp:1:/ usr /include/sys/stdio.h:37:9:注意:ssize_t getline(char**,size_t*,FILE*)ssize_t _EXFUN(getline,(char**,size_t*,FILE*)); ^ /usr/include/sys/stdio.h:37:9:注意:候选人需要3个参数,2提供来自/usr/lib/gcc/x86_64-pc-cygwin/4.8.2/include/c++/的文件字符串:52:0,来自/usr/lib/gcc/x86_64-pc-cygwin/4.8.2/include/c++/bits/locale_classes.h:40,来自/ usr/lib/gcc/x86_64-pc-cygwin/4.8.2/include/c ++/bits/ios_base.h:41,来自/ usr/lib/gcc /来自/usr/lib/gcc/x86_64-pc-cygwin/4.8.2/include/c++/ios:42 x86_64-pc-cygwin/4.8.2/include/c ++/ostream:38,来自/usr/lib/gcc/x86_64-pc-cygwin/4.8.2/include/c++/iostream:39,来自Driver.cpp:1 :/usr/lib/gcc/x86_64-pc-cygwin/4.8.2/include/c++/bits/basic_string.h:2793:5:注意:模板std :: basic_istream <_CharT,_Traits>&std :: getline( std :: basic_istream <_CharT,_Traits>&,std :: basic_string <_CharT,_Traits,_Alloc> &&)getline(basic_istream <_CharT,_Traits>&__ is,^

 ^ /usr/lib/gcc/x86_64-pc-cygwin/4.8.2/include/c++/bits/basic_string.tcc:1068:5:
Run Code Online (Sandbox Code Playgroud)

注意:模板参数推断/替换失败:Driver.cpp:49:44:注意:不匹配的类型'std :: basic_string <_CharT,_Traits,_Alloc>'和'char [1024]'getline(cin,buffer); ^ Makefile:24:目标'Driver.o'的配方失败make:* [Driver.o]错误1

以下是删除了不属于此案例的代码的代码:

#include <iostream>
#include <cstdio>
#include <string>
#include <getopt.h>
#include "Driver.hpp"
#include "SymTab.hpp"

using namespace std;

#ifdef NULL
#undef NULL
#define NULL 0
#endif …
Run Code Online (Sandbox Code Playgroud)

c++ getline

-2
推荐指数
1
解决办法
4000
查看次数

如何使用 C++ 中的 getline() 函数从多行文本文件中每行读取 3 行?

我正在尝试使用该函数从文本文件中读取 3 行getline()

当我读取两行时,它工作得很好,因为我能够通过计算它是奇数行还是偶数行来区分。但对于 3 条线来说,这是不可能的。那么,有什么方法可以使用该getline()功能来做到这一点吗?

这是数据在文本文件中的样子:

图像

我想以每组 3 行代表代码中一个单独的节点的方式阅读它。因此,节点的第一行是联系人的姓名,节点的第二行是联系人组,第三行是电话号码。这样,我就可以将多个节点的数据存储在一个系列中。

void reopenCB() {
    bool isEmpty;
    ifstream myfile("contactbook.txt");
    if (myfile.is_open() & myfile.peek() != EOF) {
        int i = 0;
        while (getline(myfile, x)) {
            if (i % 2 == 0) {
                if (head == NULL) {
                    Node *newer = new Node;
                    newer->name = x;

                    newer->next = NULL;
                    newer->prev == NULL;
                    head = newer;
                } else {
                    Node *newer = new Node;

                    newer->name = x;
                    newer->next = NULL;

                    Node …
Run Code Online (Sandbox Code Playgroud)

c++ getline

-2
推荐指数
1
解决办法
474
查看次数

错误C3861:'getLine':找不到标识符

我从SEE开始编程抽象(CS106B).我很难开始任务.这是热身练习的代码.我已经咨询了各种不同的解决方案,但没有一个能够工作,因此,我们不得不在这里发布.

#include <iostream>
#include <cstring>
#include "console.h"
using namespace std;

/* Constants */

const int HASH_SEED = 5381;               /* Starting point for first cycle */
const int HASH_MULTIPLIER = 33;           /* Multiplier for each cycle      */
const int HASH_MASK = unsigned(-1) >> 1;  /* All 1 bits except the sign     */

/* Function prototypes */

int hashCode(string name);

/* Main program to test the hash function */

int main() {
   string name = getLine("Please enter your name: ");
   int code …
Run Code Online (Sandbox Code Playgroud)

c++ string getline

-3
推荐指数
1
解决办法
9918
查看次数

标签 统计

getline ×10

c++ ×9

string ×3

arrays ×1

c ×1

cin ×1

for-loop ×1

fstream ×1

ifstream ×1

segmentation-fault ×1

static ×1

text ×1

variables ×1