2
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <vector>
#include <iterator>

#define ADDR "c:\\Users\\Library2\\Desktop\\Books record\\"

using namespace std;

int main()
{
   ifstream fin(ADDR "reportcard.csv", ios::binary);
   string line;
   int rowCount=0;
   int rowIdx=0; 

   while(getline(fin,line)){
       rowCount++;
   }

   vector<string> data[**rowCount**];//this rowCount gave me "expression must have a constant value"

   fin.clear(); 
   fin.seekg(fin.beg); 

   while(getline(fin,line)) 
   {
      stringstream ss(line);  
      string value;
      while(getline(ss,value,',')){       
         data[rowIdx].push_back(value);
      }
      rowIdx++;   
   }

   fin.close();

   int colNum;
   string colName = "LAST PERSON";
   static int it;

   for(vector<string>::iterator it = data[0].begin(); it != data[0].end(); ++it)
   {
       if ((*it)== colName)
       {
           colNum = distance(data[0].begin(),it);//distance() gave me "no instances of function templates matches argument"
           break;
       }
   }
   cout << data[1][colNum] << "\t";

   return 0;
}
  1. im trying to figure out why it gave me the expression must have a constant value.
  2. im trying to find another function that works the same as distance() that can be used in visual studio 2012.

note: this code is for finding and getting the value from the first cell under a column called "LAST PERSON". This code is already okay when using codeblocks. but i need to use visual studio.

genpfault
  • 51,148
  • 11
  • 85
  • 139
  • What errors does it give to you? – Alfabravo Dec 15 '19 at 23:35
  • i did put the error message that visual studio gave me as a comment on that line. the error was for line; vector data[**rowCount**];//this rowCount gave me "expression must have a constant value" then, colNum = distance(data[0].begin(),it);//distance() gave me "no instances of function templates matches argument" – SectumSempra Dec 15 '19 at 23:38
  • u can refer back to the code, hehe – SectumSempra Dec 15 '19 at 23:39
  • 1
    Does this answer your question? [Why aren't variable-length arrays part of the C++ standard?](https://stackoverflow.com/questions/1887097/why-arent-variable-length-arrays-part-of-the-c-standard) – alter_igel Dec 15 '19 at 23:44
  • wait. let me read and check that one out. btw, thanks for ur feedback guys – SectumSempra Dec 15 '19 at 23:46

2 Answers2

5

This

vector<string> data[rowCount];

is a declaration of a variable length array.

Variable length arrays is not a standard C++ feature. Some compilers have their own language extensions that allow to use variable length arrays. Other compilers do not have such a language extension.

Instead of the array you could use a vector of vectors as for example

std::vector<std::vector<std::string>> data;

Pay attention to that as the file is opened in the binaru mode

ifstream fin(ADDR "reportcard.csv", ios::binary);

then using the function std::getline is not correct in general case.

while(getline(fin,line)){
    rowCount++;
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • 1
    `std::vector> data(rowCount);` looks like it would be the correct declaration, given the rest of the code. – Ken Y-N Dec 15 '19 at 23:41
  • thank you both of you. actually i havent fully understand yet what does vector> is. but i just tried and applied it. and yes. both of the errors gone. thank you so much guys. now i need to read something about this to understand it. i will start to google about it. – SectumSempra Dec 16 '19 at 00:38
0

When the compiler sees this:

vector<string> data[rowCount];

it starts from data and looks to the right. It sees '[' and interprets data to be a C-style array. Then it sees rowCount and checks if it is a compile-time constant. If not, this code is against the standard. Still, some compilers allow this as a language extension.

All in all, you've defined a C-style array of rowCount elements of type vector<string>. You must have mistaken [] for () or {}. Most likely you wanted to write

std::vector<std::string> data(rowCount); // a vector of rowCount strings 
zkoza
  • 2,644
  • 3
  • 16
  • 24