i want to store different type of value in an 2d array using getline. however, t
ID: 668442 • Letter: I
Question
i want to store different type of value in an 2d array using getline.
however, there is an error..
the error message is " error: no matching function for call to ‘getline(std::istream&, double&, char)’"
below is the whole error message..
pj2.cpp: In function ‘int main()’:
pj2.cpp:37:59: error: no matching function for call to ‘getline(std::istream&, double&, char)’
pj2.cpp:37:59: note: candidates are:
/usr/include/c++/4.6/bits/basic_string.tcc:1070:5: note: template<class _CharT, class _Traits, class _Alloc> std::basic_istream<_CharT, _Traits>& std::getline(std::basic_istream<_CharT, _Traits>&, std::basic_string<_CharT, _Traits, _Alloc>&, _CharT)
/usr/include/c++/4.6/bits/basic_string.h:2734:5: note: template<class _CharT, class _Traits, class _Alloc> std::basic_istream<_CharT, _Traits>& std::getline(std::basic_istream<_CharT, _Traits>&, std::basic_string<_CharT, _Traits, _Alloc>&)
and then below is my codes....
can someone help me to store without error message?
#include <iostream>
#include <string>
using namespace std;
struct data
{
string name;
double area;
int population;
double dencity;
};
int main()
{
struct data a;
a.name;
a.area;
a.population;
a.dencity;
string array[ 3 ][ 4 ];
int i, j;
for( i = 0; i < 4; i++ )
{
for( j = 0; j <= 4; j++ )
{
if( j == 1)
{
getline( cin, a.area, '|' );
array[ i ][ j ] = a.area;
}
if( j == 3 )
{
getline( cin, a.dencity, '|' );
array[ i ][ j ] = a.dencity;
}
getline( cin, a.name, '|' );
array[ i ][ j ] = a.name;
cout << array[ i ][ j ];
}
}
return 0;
}
Explanation / Answer
#include <bits/stdc++.h>
using namespace std;
struct data{
string name;
double area;
int population;
double dencity;
};
int main(){
data a;
string array[3][4];
int i, j;
string s;
for( i = 0; i < 3; i++ ){
for( j = 0; j < 4; j++ ){
if( j == 1){
getline(cin,s, '|' );
a.area = atof(s.c_str());
array[i][j] = a.area;
}
else if( j == 3 ){
getline( cin, s, '|' );
a.dencity = atof(s.c_str());
array[i][j] = a.dencity;
}
else if (j == 0){
getline( cin, s, '|' );
a.population = atoi(s.c_str());
array[i][j] = a.population;
}
else{
getline( cin, a.name, '|' );
array[i][j] = a.name;
}
}
}
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.