I have problem with getline()
.
I tried many examples and read other solutions, but that didn’t solve my problem. I still have information 'getline: identifier not found'
.
I included
<stdio.h> <tchar.h> <iostream> <conio.h> <stdlib.h> <fstream>
and still nothing.
#include "stdafx.h"
using namespace std;
int main(int argc, _TCHAR* argv[])
{
string line;
getline(cin, line);
cout << "You entered: " << line << endl;
}
What do I need to do now?
I use Windows 7 64 bit and Visual Studio 2013.
richlime 0 / 0 / 0 Регистрация: 05.12.2018 Сообщений: 16 |
||||
1 |
||||
02.12.2019, 19:00. Показов 19753. Ответов 2 Метки нет (Все метки)
Подскажите, пожалуйста, выдает ошибку «E0020 идентификатор «getline» не определен» и «C3861 getline: идентификатор не найден». Что исправить надо? Строчка 83
0 |
Kislorod1234 0 / 0 / 0 Регистрация: 15.05.2016 Сообщений: 12 |
||||
1 |
||||
02.02.2017, 19:04. Показов 14916. Ответов 4 Метки нет (Все метки)
Ругается на getline , пишет идентификатор не найден
Подскажите в чем проблема
__________________ 0 |
zss Модератор 12627 / 10125 / 6097 Регистрация: 18.12.2011 Сообщений: 27,158 |
||||
02.02.2017, 19:28 |
2 |
|||
Сообщение было отмечено Kislorod1234 как решение РешениеНету
а cstring Не надо Да, и зачем Вам локализация, если русский текст не используете 1 |
0 / 0 / 0 Регистрация: 15.05.2016 Сообщений: 12 |
|
03.02.2017, 08:35 [ТС] |
3 |
Спасибо. Подскажите можно ли открыть файл для чтения и для записи в конец одновременно? Добавлено через 31 минуту 0 |
5094 / 2279 / 332 Регистрация: 20.02.2013 Сообщений: 5,598 Записей в блоге: 19 |
|||||
03.02.2017, 09:19 |
4 |
||||
. 0 |
Модератор
12627 / 10125 / 6097 Регистрация: 18.12.2011 Сообщений: 27,158 |
|
03.02.2017, 09:54 |
5 |
cin.getline(f,str,»); std::getline и istream::getline — это разные функции. 0 |
For your first error, I think that the problem is in this declaration:
const float APRICE = 6.00,
float CPRICE = 3.00;
In C++, to declare multiple constants in a line, you don’t repeat the name of the type. Instead, just write
const float APRICE = 6.00,
CPRICE = 3.00;
This should also fix your last error, which I believe is caused by the compiler getting confused that CPRICE
is a constant because of the error in your declaration.
For the second error, to use getline
, you need to
#include <string>
not just
#include <cstring>
Since the getline
function is in <string>
(the new C++ string header) and not <cstring>
(the old-style C string header).
That said, I think you’ll still get errors from this, because movieName
is declared as an int
. Try defining it as a std::string
instead. You might also want to declare your other variables as float
s, since they’re storing real numbers. More generally, I would suggest defining your variables as you need them, rather than all up at the top.
Hope this helps!
For your first error, I think that the problem is in this declaration:
const float APRICE = 6.00,
float CPRICE = 3.00;
In C++, to declare multiple constants in a line, you don’t repeat the name of the type. Instead, just write
const float APRICE = 6.00,
CPRICE = 3.00;
This should also fix your last error, which I believe is caused by the compiler getting confused that CPRICE
is a constant because of the error in your declaration.
For the second error, to use getline
, you need to
#include <string>
not just
#include <cstring>
Since the getline
function is in <string>
(the new C++ string header) and not <cstring>
(the old-style C string header).
That said, I think you’ll still get errors from this, because movieName
is declared as an int
. Try defining it as a std::string
instead. You might also want to declare your other variables as float
s, since they’re storing real numbers. More generally, I would suggest defining your variables as you need them, rather than all up at the top.
Hope this helps!
#include<iostream>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include<fstream>
#include<string>
using namespace std;
int countLines( ifstream& in )
{
int count = 0;
char line[80];
if ( in.good() )
{
//while ( !feof( in ) )
while( getline( in,line ) ) count++;
in.seekg(ios::beg);
}
return count;
}
No matching function for call get line
What does it mean?
I have included all of the headers but why I still can not call get line?
asked Jul 16, 2013 at 6:58
1
You need to use a std::string instead of a character array for calling string’s getline()
function. Replace your char line[80]
with a std::string line
and it will work.
Check the documentation here http://www.cplusplus.com/reference/string/string/getline/
answered Jul 16, 2013 at 6:59
SalgarSalgar
7,5471 gold badge25 silver badges39 bronze badges
1
#include<iostream>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include<fstream>
#include<string>
using namespace std;
int countLines( ifstream& in )
{
int count = 0;
char line[80];
if ( in.good() )
{
//while ( !feof( in ) )
while( getline( in,line ) ) count++;
in.seekg(ios::beg);
}
return count;
}
No matching function for call get line
What does it mean?
I have included all of the headers but why I still can not call get line?
asked Jul 16, 2013 at 6:58
1
You need to use a std::string instead of a character array for calling string’s getline()
function. Replace your char line[80]
with a std::string line
and it will work.
Check the documentation here http://www.cplusplus.com/reference/string/string/getline/
answered Jul 16, 2013 at 6:59
SalgarSalgar
7,5471 gold badge25 silver badges39 bronze badges
1
У меня проблема с getline()
,
Я пробовал много примеров и читал другие решения, но это не решило мою проблему. У меня еще есть информация 'getline: identifier not found'
,
я включен
<stdio.h> <tchar.h> <iostream> <conio.h> <stdlib.h> <fstream>
и до сих пор ничего.
#include "stdafx.h"
using namespace std;
int main(int argc, _TCHAR* argv[])
{
string line;
getline(cin, line);
cout << "You entered: " << line << endl;
}
Что мне нужно сделать сейчас? Я использую Win7 64bit, MSVS2013.
1
Решение
Привыкнуть просто чтение документации для языковых функций, которые вы используете.
Соотношение совершенно ясно, что std::getline
может найти в string
,
#include <string>
8
Другие решения
Это должно исправить это:
#include "stdafx.h"#include <iostream>
#include <string>
using namespace std;
int main(int argc, _TCHAR* argv[]){
string line;
getline(cin, line);
cout << "You entered: " << line << endl;
}
5
#include "stdafx.h"#include <iostream>
#include <string>
using namespace std;
int main(int argc, _TCHAR* argv[]){
string line;
/*To consume the whitespace or newline between the end of a token and the
beginning of the next line*/
// eat whitespace
getline(cin >> ws, line);
cout << "You entered: " << line << endl;
}
0
#include <string>
Кстати, в учебнике «Программирование С ++» Мурача ошибочно говорится, что getline () находится в заголовке iostream (стр. 71), что может привести к некоторой путанице.
0
Here’s the code:
#include "stdafx.h"
#include <iostream>
using namespace std;
int i, j, l, r1, r2, r3, n1, n2, n3, n4, rII, rIII;
string msg;
int rotor1[26] = { 4,10,12,5,11,6,3,16,21,25,13,19,14,22,24,7,23,20,18,15,0,8,1,17,2,9 };
int rotor2[26] = { 0,9,3,10,18,8,17,20,23,1,11,7,22,19,12,2,16,6,25,13,15,24,5,21,14,4 };
int rotor3[26] = { 1,3,5,7,9,11,2,15,17,19,23,21,25,13,24,4,8,22,6,0,10,12,20,18,16,14 };
int reflec[26] = { 24,17,20,7,16,18,11,3,15,23,13,6,14,10,12,8,4,1,5,25,2,22,21,9,0,19 };
char base[26] = { 'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z' };
void RotorInput() {
cout << "Input initial positions (0 to 25):n";
cout << "First rotor: ";
cin >> r1;
cout << "Second rotor: ";
cin >> r2;
cout << "Third rotor: ";
cin >> r3;
}
void RotorInitialization() {
rotate(rotor1, rotor1 + r1, rotor1 + 26);
rotate(rotor2, rotor2 + r2, rotor2 + 26);
rotate(rotor2, rotor2 + r2, rotor2 + 26);
}
void MessageInput() {
cout << "Input your message/code:n";
getline(cin, msg);
getline(cin, msg);
}
void Encription() {
l = msg.size();
for (j = 0; j < l; j++) {
}
for (i = 0; i < l; i++) {
if (msg.at(i) == ' ') {
cout << " ";
}
else {
int x = distance(base, find(base, base + 26, msg.at(i)));
n1 = rotor1[x];
n2 = rotor2[n1];
n3 = rotor3[n2];
n4 = reflec[n3];
n3 = distance(rotor3, find(rotor3, rotor3 + 26, n4));
n2 = distance(rotor2, find(rotor2, rotor2 + 26, n3));
n1 = distance(rotor1, find(rotor1, rotor1 + 26, n2));
cout << base[n1];
rII = (i + r2 + 1) % 26;
rIII = (i + r3 + 1) % 676;
rotate(rotor1, rotor1 + 1, rotor1 + 26);
if (rII == 0) {
rotate(rotor2, rotor2 + 1, rotor2 + 26);
}
else {}
if (rIII == 0) {
rotate(rotor3, rotor3 + 1, rotor3 + 26);
}
else {}
}
}
}
int main()
{
RotorInput();
RotorInitialization();
MessageInput();
Encription();
cout << endl;
system("pause");
return 0;
}
I’m using VS windows, my friend who coded this was using another program, and it worked, but not on mine… I’m going crazy!
- Remove From My Forums
-
Question
-
My codes are similar to below snippet. I got error of «…error C3861: ‘getline’: identifier not found …».
If I added #include <iostream> and change getline() as std::getline(), I got error:
«‘std::getline’: no matching overloaded function found».
‘std::basic_istream<_Elem,_Traits> &std::getline(std::basic_istream<_Elem,_Traits> &,std::basic_string<_Elem,_Traits,_Alloc> &)’: expects 2 arguments — 3 providedHow should I use getline() correctly? My platform is VS2015.
#include <stdio.h>
#include <stdlib.h>FILE *file
= fopen(filename, "r");.....
//
read each line and print it to the screen
int line_number = 0;
while(-1 != getline(&buffer,
&buffer_size, file))
{
printf("%d: %s",
++line_number, buffer);
}
-
Edited by
Monday, August 21, 2017 6:25 AM
-
Edited by
Answers
-
Hello,
use the std-lib like this:
#include <fstream> CString szFilename = _T(" <your filename> "); std::ifstream fi; fi.open((LPCTSTR)szFilename); if (!fi.is_open()) { return; } char szline[10240]; while (fi.getline(szline, 10240)) { // ... } fi.close();
Regards, Guido
-
Edited by
Guido Franzke
Monday, August 21, 2017 7:42 AM -
Marked as answer by
Stan Huang at Taiwan
Tuesday, August 22, 2017 2:57 AM
-
Edited by
-
On 8/21/2017 2:24 AM, Stan Huang at Taiwan wrote:
FILE *file = fopen(filename, «r»);
std::getline is designed to read from std::istream or a class derived therefrom; not from FILE*. And it reads into std::string, not a raw buffer. If you insist on using FILE* and raw buffer, there’s fread(). Otherwise:
std::ifstream file(filename); std::string line; int line_number = 0; while (std::getline(file, line)) { std::cout << ++line_number << ": " << line; }
-
Marked as answer by
Stan Huang at Taiwan
Tuesday, August 22, 2017 3:30 AM
-
Marked as answer by
-
How should I use getline() correctly? My platform is VS2015.
#include <stdio.h>
#include <stdlib.h>FILE *file
= fopen(filename, "r");.....
//
read each line and print it to the screen
int line_number = 0;
while(-1 != getline(&buffer,
&buffer_size, file))
{
printf("%d: %s",
++line_number, buffer);
}
By way of fleshing out some of the details, take note that there are
two getline() functions:(1) istream::getline
https://msdn.microsoft.com/en-us/library/aa277361(v=vs.60).aspxThis is a member function of basic_istrean and its derivatives.
It reads the string into a «raw» or «native» character array.
The reply from Guido uses this version of getline.(2) std::getline (from <string>)
https://msdn.microsoft.com/en-CA/library/2whx1zkx(v=vs.100).aspx
https://msdn.microsoft.com/library/1a4ffd11-dce5-4cc6-a043-b95de034c7c4.aspx#getline_template_functionUnlike (1) above, this is not a member function of a class.
It reads the string into a std::string.
The reply from Igor uses this version of getline.As noted by Igor, if you must use the input streams from the C Standard
Library (fopen, FILE*) then the preferred route would be to use the
functions provided for manipulating such streams. To read strings, see:fgets, fgetws
https://msdn.microsoft.com/en-us/library/c37dh6kf.aspx— Wayne
-
Marked as answer by
Stan Huang at Taiwan
Tuesday, August 22, 2017 3:36 AM
-
Marked as answer by
-
On 8/21/2017 11:33 PM, Stan Huang at Taiwan wrote:
It works basically. But why the line «
<p>std::cout << ++line_number << <span style="margin:0px; padding:0px; border:0px; vertical-align:baseline; color:#a31515">": "</span> << line;"</p><p></p><p>I got error of</p>«error C2039: ‘cout’: is not a member of ‘std'»
You are probably missing #include <iostream>
-
Marked as answer by
Stan Huang at Taiwan
Tuesday, August 22, 2017 4:12 AM
-
Marked as answer by
-
I learned the usage of getline() form http://timmurphy.org/2010/10/31/reading-from-a-file-in-c-2/
I guessed it worked at some situation. So, how come it doesn’t work at VS environment?
That uses a non-standard C extension that is supported by some compilers
on some platforms. It is not part of the ISO C99 Standard nor the ISO
C++ Standard, and is not supported by Visual C++. See:undefined reference to `getline’ in c
https://stackoverflow.com/questions/13112784/undefined-reference-to-getline-in-c— Wayne
-
Marked as answer by
Stan Huang at Taiwan
Tuesday, August 22, 2017 5:23 AM
-
Marked as answer by
Get the Reddit app
Log In
Log in to Reddit
Open settings menu
-
Log In / Sign Up
-
Advertise on Reddit