2D compilation error - Advise me how to input char data into this vector.

Hello programmers. I am stuck in this and am unable to proceed.

Kindly advise how to to implement the areas i have cited in the comments in the program below. I have included the error message at the end.

#include<iostream>
#include<vector>
#include<iomanip>
#include<string>
#include<limits>

usingnamespace std;


int main (){
int cols =15;
int i,j,rows;
char reader;


cout<<"How many records do you want to create ?\n";

cin>>rows;

cin.ignore ( std::numeric_limits<std::streamsize>::max(), '\n' );
cout<<"\n";
std::vector< vector<string>> dynamicArray(rows, vector<string>(cols));

if (rows> 0){

cout<<"opening Vector !"; //checking compilation error...
cout<<"\n\n";

dynamicArray [0][0] = "LR_UNIT_NUMBER";
dynamicArray [0][1] = "NAME_OF_PROPRIETOR";
dynamicArray [0][2] ="NATIONAL_ID_CARD_NUMBER";
dynamicArray [0][3] = "ADDRESS_OF_PROPRIETOR";
dynamicArray [0][4] = "PIN_NUMBER_OF_PROPRIETOR";
dynamicArray [0][5] = "DATE_LAND_WAS_ACQUIRED";
dynamicArray [0][6] = "DATE_OF_ISSUE_OF_TITLE_DEED";
dynamicArray [0][7] = "LAND_CATEGORY";
dynamicArray [0][8] = "TYPE_OF_OWNERSHIP";
dynamicArray [0][9] = "COUNTY_LOCATED";
dynamicArray [0][10] = "DISTRICT_LOCATED";
dynamicArray [0][11] = "DIVISION_LOCATED";
dynamicArray [0][12] = "LOCATION_LOCATED";
dynamicArray [0][13] = "SUBLOCATION_LOCATED";
dynamicArray [0][14] = "VILLAGE_LOCATED";

for (j=0;j<cols; j++)
cout<<dynamicArray [0][j]<<"||";//<--Help to improve the outlook so that the information may appear as if in a table. //i would like this row 0 column to be arranged horizontally and appears as if it is the first row of a table with the strings initialized above to appear as the respective column headings e.g.

// LR_UNIT_NUMBER || NAME_OF_PROPRIETOR || NATIONAL_ID_CARD_NUMBER || ADDRESS_OF_PROPRIETOR || PIN_NUMBER_OF_PROPRIETOR || DATE_LAND_WAS_ACQUIRED || DATE_OF_ISSUE_OF_TITLE_DEED||

//Row 1 ->User iput at runtime || User iput at runtime ||User iput at runtime||User iput at runtime ||User iput at runtime ||User iput at runtime ||User iput at runtime||User iput at runtime

//Row 2 -> User iput at runtime || User iput at runtime ||User iput at runtime||User iput at runtime ||User iput at runtime ||User iput at runtime ||User iput at runtime||User iput at runtime

cout<<endl<<"\n\n";
cout<<"Table headings (First row i.e Row 0 columns) have been displayed."; //checking compilation error...

cout<<endl<<"\n\n";


for (i=1; i<rows; i++) {

for (j=0;j<cols; j++){


cout<<"Enter details for proprietor/ owner:"<<(i+1)<<"in this record";


cin.getline(cin, reader); //to take user input into a string (reader) temporarily <---problem here. It has failed to take the input strings

dynamicArray [i].push_back(reader); //copy the contents of string array (reader) into the vector <---problem here. It has failed to take the input strings and enter it here.

rows--;

}
cout<<endl;
}
}

cout<<"You have entered this data:";
cout<<endl<<"\n\n";

for (i=0; i<rows; i++) {
for (j=0; j<cols; j++){

cout<<dynamicArray[i][j]<<"||"; // prints the first row (row 0) at first, followed by the specific details in the creted rows below it. Remember, I would have liked it to be a table with first row columns as headings and specific details entered at runtime should start at row 1.
// LR_UNIT_NUMBER || NAME_OF_PROPRIETOR || NATIONAL_ID_CARD_NUMBER || ADDRESS_OF_PROPRIETOR || PIN_NUMBER_OF_PROPRIETOR || DATE_LAND_WAS_ACQUIRED || DATE_OF_ISSUE_OF_TITLE_DEED||

//Row 1 -> Input entered || Input entered ||Input entered ||Input entered ||Input entered ||Input entered ||Input entered ||Input entered

//Row 2 -> Input entered || Input entered ||Input entered ||Input entered ||Input entered ||Input entered ||Input entered ||Input entered



}
cout<<endl<<"\n\n";
cout<<"Record number: "<<(rows-1)<<" has been created"; //A counter...checking compilation error...
cout<<endl<<"\n\n";
}
system ("pause");

EXIT_SUCCESS;

}







ERROR THROWN

1>------ Build started: Project: 2D_vector, Configuration: Debug Win32 ------
1>Compiling...
1>myVector.cpp
1>.\myVector.cpp(68) : error C2664: 'std::basic_istream<_Elem,_Traits>&std::basic_istream<_Elem,_Traits>::getline(_Elem *,std::streamsize)' : cannot convert parameter 1 from 'std::istream' to 'char *'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
1>.\myVector.cpp(70) : error C2664: 'std::vector<_Ty>::push_back' : cannot convert parameter 1 from 'char' to 'const std::string &'
1> with
1> [
1> _Ty=std::string
1> ]
1> Reason: cannot convert from 'char' to 'const std::string'
1> No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
1>Build log was saved at "file://c:\Users\dell\Documents\Visual Studio 2005\Projects\2D_vector\2D_vector\Debug\BuildLog.htm"
1>2D_vector - 2 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>
#include <string>
#include <vector>

std::istream& get_non_empty_line( std::istream& stm, std::string& line )
{
    if( std::getline( stm, line ) ) if( line.empty() ) return get_non_empty_line(stm,line) ;
    return stm ;
}

std::istream& get_row( std::istream& istm, std::vector<std::string>& row,
                       const std::vector<std::string>& fld_names, std::ostream& ostm = std::cout )
{
    row.clear() ;
    ostm << '\n' ;
    for( const std::string tag : fld_names )
    {
        std::string input ;
        ostm << "enter " << tag << ": " ;
        if( get_non_empty_line( istm, input ) ) row.push_back(input) ; // std::move(input)
        else { row.clear() ; return istm ; }
    }

    return istm ;
}

int main()
{
    const std::vector<std::string> fld_names { "unit number", "name of proprietor", "national id card number",
                                               "address of proprietor", "pin number of proprietor" /* etc.*/ } ;

    std::vector< std::vector<std::string> > data_set ;

    std::size_t nrows ;
    std::cout << "number of records? " ;
    std::cin >> nrows ;

    std::vector<std::string> row ;
    while( data_set.size() < nrows && get_row( std::cin, row, fld_names ) ) data_set.push_back(row) ; // std::move(row)

    std::cout << "\n-------- data set contains ---------\n" ;
    for( const auto& row : data_set )
    {
        for( std::size_t i = 0 ; i < fld_names.size() ; ++i ) std::cout << fld_names[i] << ": " << row[i] << '\n' ;
        std::cout << "--------------\n" ;
    }
}

Thanx big, JLBorges (7445) for your insight .
There are these six six errors the compiler (Visual C++) is throwing for your code. I am trying to understand them but cant get them, would you please correct them and put some commentaries in them please ? I am a student and still learning to code.
I thought the errors have something to do with the vector fld_names since the errors affect the parts where the fld_name variable has been used.

Code part 1:
for( const std::string tag : fld_names )

Error message:
1>.\2D_vector.cpp(18) : error C2143: syntax error : missing ',' before ':'
1>.\2D_vector.cpp(19) : error C2143: syntax error : missing ';' before '{'

Code part 2:
const std::vector<std::string> fld_names { "unit number", "name of proprietor", "national id card number", "address of proprietor", "pin number of proprietor" /* etc.*/ } ;


Error message:
1>.\2D_vector.cpp(33) : error C2601: 'fld_names' : local function definitions are illegal
1>.\2D_vector.cpp(32): this line contains a '{' which has not yet been matched
1>.\2D_vector.cpp(34) : error C2143: syntax error : missing ';' before '}'

Code part 3:
while( data_set.size() < nrows && get_row( std::cin, row, fld_names ) )
Error message:

1>.\2D_vector.cpp(43) : error C2065: 'fld_names' : undeclared identifier
1>.\2D_vector.cpp(43) : fatal error C1903: unable to recover from previous error(s); stopping compilation


Finally also clarify the use of the arguments std::istream& istm and std::ostream& ostm = std::cout which you have provided in the below function:


std::istream& get_row( std::istream& istm, std::vector<std::string>& row,
const std::vector<std::string>& fld_names, std::ostream& ostm = std::cout )


Thanks in advance.



JLB... iostreams as function arguments...have never used that but am getting a hint from net resources. Still waiting for your reply.
> There are these six six errors the compiler (Visual C++) is throwing for your code.

The errors ae being thrown by an obsolete version of the Microsoft compiler.
Update to the current version Visual Studio 2015 (the community edition is free) and the errors will go away.


> Code part 1: for( const std::string tag : fld_names )

This uses a C++11 feature: range-based loops http://www.stroustrup.com/C++11FAQ.html#for


> Code part 2: const std::vector<std::string> fld_names { "unit number", ... /* etc.*/ } ;

This too uses a C++11 feature: list initialisation http://www.stroustrup.com/C++11FAQ.html#init-list


The legacy C++ version of the code (which should compile cleanly with old compilers) would be:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <iostream>
#include <string>
#include <vector>

std::istream& get_non_empty_line( std::istream& stm, std::string& line )
{
    if( std::getline( stm, line ) ) if( line.empty() ) return get_non_empty_line(stm,line) ;
    return stm ;
}

std::istream& get_row( std::istream& istm, std::vector<std::string>& row,
                       const std::vector<std::string>& fld_names, std::ostream& ostm = std::cout )
{
    row.clear() ;
    ostm << '\n' ;
    // for( const std::string tag : fld_names )
    for( std::size_t i = 0 ; i < fld_names.size() ; ++i )
    {
        std::string input ;
        // ostm << "enter " << tag << ": " ;
        ostm << "enter " << fld_names[i] << ": " ;
        if( get_non_empty_line( istm, input ) ) row.push_back(input) ; // std::move(input)
        else { row.clear() ; return istm ; }
    }

    return istm ;
}

int main()
{
    const char* const fld_names_array[] = { "unit number", "name of proprietor", "national id card number",
                                            "address of proprietor", "pin number of proprietor" /* etc.*/ } ;
    const std::size_t n_fld_names = sizeof(fld_names_array) / sizeof(fld_names_array[0]) ;

    const std::vector<std::string> fld_names( fld_names_array, fld_names_array + n_fld_names ) ;

    std::vector< std::vector<std::string> > data_set ;

    std::size_t nrows ;
    std::cout << "number of records? " ;
    std::cin >> nrows ;

    std::vector<std::string> row ;
    while( data_set.size() < nrows && get_row( std::cin, row, fld_names ) ) data_set.push_back(row) ; // std::move(row)

    std::cout << "\n-------- data set contains ---------\n" ;
    // for( const auto& row : data_set )
    for( std::size_t i = 0 ; i < data_set.size() ; ++i )
    {
        const std::vector<std::string>& row = data_set[i] ;
        for( std::size_t i = 0 ; i < fld_names.size() ; ++i ) std::cout << fld_names[i] << ": " << row[i] << '\n' ;
        std::cout << "--------------\n" ;
    }
}
Thanks soo much JLBorges even for the update. You are a great resource yourself ! I am on way to work and will keep in touch as i proceed with the project. Once again, thank you.
Thanks soo much JLBorges.

I am now working on outputting this data to a text file.
Also reading from the text file during runtime (sort of writing to a database but in this case a text file e.g "Land_register.txt") and then reading the contents already in the register.

If for example i was to:

fstream myregister ("Land_register.txt", ios::in || ios::out || ios::app);

for( std::size_t i = 0 ; i < data_set.size() ; ++i )

myregister<<data_set [i]; // i may not be correct here.


Kindly demonstrate how to do this for the above code you guided, i would much appreciate.


Finally, what is the syntax for searching for a SPECIFIC record/ data e.g. a person's name that has been entered into the "Land_register.txt", without reading the whole data entered into the register ?

Is it:




size_t find (const string& str, size_t pos = 0) const;


How do i do it here ?


And is it possible to read the whole of this data in the register at once (i.e. opening and displaying the whole register) ? Is there an implementation (may be using fstream) that can be written to output this data to an accessory device e.g. printer for hardcopy printing ?
Last edited on
Topic archived. No new replies allowed.