标签: getline

为什么有两个不同的getline()函数(如果确实存在)?

每次我做一个C++代码行的快速片段

std::string s;
cin >> s;
Run Code Online (Sandbox Code Playgroud)

我诅咒自己,因为我忘了它停在空白处,而不是整条线.

然后,在记忆中getline,我总是对这两个品种感到困惑:

std::string s;
getline (std::cin, s);
Run Code Online (Sandbox Code Playgroud)

和:

char cs[256];
std::cin.getline (cs, sizeof (cs));
Run Code Online (Sandbox Code Playgroud)

除了数据类型之外,这两者之间是否存在实际差异?

在我看来,C++方式应该是前者.在什么情况下我会使用后者,因为我可能应该使用真正的字符串而不是空终止字符数组?

而且,由于输入应该是输入流的范围,为什么不是前者的一部分istream呢?

c++ string getline

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

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

我试图从控制台获取一些用户输入参数,两个字符串,两个整数和一个double.我试图使用的相关代码是:

#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: $"; 
cin >> inputDouble; 
tempDVD.setPrice(inputDouble);
Run Code Online (Sandbox Code Playgroud)

但是,在运行程序时,代码不会等待输入第一个inputString,而是在第二次getline()调用之前代码不会停止.因此控制台输出如下所示:

标题:类别:

光标出现在类别之后.如果我现在输入,程序然后跳转到年份输入,不允许我输入多个字符串.这里发生了什么事?

c++ getline

21
推荐指数
3
解决办法
4万
查看次数

需要帮助getline()

是否有一个原因,如果在我的程序中我要求用户输入,我这样做:

int number;
string str;
int accountNumber;

cout << "Enter number:";
cin >> number;
cout << "Enter name:";
getline(cin, str);
cout << "Enter account number:";
cin >> accountNumber;
Run Code Online (Sandbox Code Playgroud)

为什么在输入第一个数字后,它输出"输入名称",然后立即输入"输入账号",然后我甚至输入我的"str"为getline(cin,str)线?谢谢!

c++ getline

19
推荐指数
5
解决办法
2万
查看次数

getline无法正常工作?可能是什么原因?

可能重复:
getline没有要求输入?

在我的程序中发生了一些独特的事情.以下是一些命令:

 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 );
Run Code Online (Sandbox Code Playgroud)

当我尝试将此代码段与整个代码一起运行时.输出程序的工作方式如下:

在此输入图像描述

输出:

Enter the full name of student:
Age: 20

Father's Name:
Permanent Address: xyz
Run Code Online (Sandbox Code Playgroud)

如果你注意到,该程序没有问我全名,并直接问我年龄.然后它也跳过父亲的名字并询问永久地址. 这可能是什么原因?

我很难发布整个代码,因为它太大了.

c++ getline visual-c++

19
推荐指数
1
解决办法
5万
查看次数

如果在某些输入后使用,则getline()不起作用

可能重复:
需要帮助getline()

getline() 如果我在一些输入之后使用它,即不起作用

#include<iostream>
using namespace std;

main()
{
string date,time;
char journal[23];


cout<<"Date:\t";
cin>>date;
cout<<"Time:\t";
cin>>time;

cout<<"Journal Entry:\t";
cin.getline(journal,23);


cout<<endl;
system("pause");
}
Run Code Online (Sandbox Code Playgroud)

好像我getline()在输入之上使用它确实有效

cout<<"Journal Entry:\t";
cin.getline(journal,23);
cout<<"Date:\t";
cin>>date;
cout<<"Time:\t";
cin>>time;
Run Code Online (Sandbox Code Playgroud)

可能是什么原因?

c++ char getline

18
推荐指数
2
解决办法
4万
查看次数

istream :: getline返回类型

istream::getline方法返回什么?

我问,因为我已经看到循环文件,它应该这样做:

while ( file.getline( char*, int ) )
{
    // handle input
}
Run Code Online (Sandbox Code Playgroud)

什么回来了?

c++ file-io getline istream ifstream

17
推荐指数
3
解决办法
2万
查看次数

c中对'getline'的未定义引用

我正在学习在C编程中使用getline,并尝试了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 0;
 }
Run Code Online (Sandbox Code Playgroud)

但是,问题是编译器不断返回错误:未定义的引用'getline'.你能告诉我问题是什么吗?谢谢!

我使用的是Win7 64bit + Eclipse Indigo + MinGW

c reference undefined getline mingw32

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

c ++从.csv文件中读取

我有这个代码,它应该在控制台中输入来自.csv文件的信息;

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<< " "  ;

}
Run Code Online (Sandbox Code Playgroud)

还有一个csv文件(当我用记事本打开时):

0,Filipe,19,M

1,Maria,20,F

2,Walter,60,M
Run Code Online (Sandbox Code Playgroud)

每当我运行程序时,控制台都会显示:

意外的输出

我的问题是为什么程序不是在每一行中重复那些cout消息而不是仅在第一行中重复

顺便说一句,nome是名字,idade是年龄,而genero/sexo是性别,在创建这篇文章之前忘了翻译

c++ csv file getline

15
推荐指数
2
解决办法
11万
查看次数

std没有会员'getline'?

我正在尝试使用std :: getline,但是我的编译器告诉我getline没有被识别出来?

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <fstream>
#include <cstdlib>

int main(){
    using namespace std;
    string line;
    ifstream ifile("test.in");
    if(ifile.is_open()){
        while(ifile.good()){
            getline(ifile,line);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

c++ io std getline

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

如何从std :: string中"getline"?

我有个问题.我加载整个文件,然后通过它获取一些信息.但是,在地图格式中,信息可能有0或20"行".我需要知道如何通过std :: string获取.有一个函数(源流,目标字符串,十进制)但我需要(源字符串,目标字符串,十进制).在C++中搜索流是不可能的(只使用很多临时字符串,多次提取和插入,它不清楚,我不想那么乱的方式).所以我想知道如何从std :: string中获取.

Thans

c++ string stream getline

13
推荐指数
1
解决办法
2万
查看次数

标签 统计

getline ×10

c++ ×9

string ×2

c ×1

char ×1

csv ×1

file ×1

file-io ×1

ifstream ×1

io ×1

istream ×1

mingw32 ×1

reference ×1

std ×1

stream ×1

undefined ×1

visual-c++ ×1