2

This question follows on from : c++ reading in text file into vector<vector> then writing to vector or array depending on first word in internal vector . Im editing this question because the first issue was simply a typo error (couldn't ask a separate Q because Ive tried that before and got down voted for duplicate??, also couldnt delete Q because has answers..), and the more important question is about the cygwin c++ compiler not having access to c99 libraries. When using stod instead of strtod i get an compilation error. The issue is _GLIB_CXX_USE_C99 is undefined??

Code so far:

#include <algorithm>
#include <fstream>
#include <iostream>
#include <iterator>
#include <sstream>
#include <string>
#include <vector>
#include <cstdlib>

#if __cplusplus < 201103L
#warning No C++11 support
#endif

#if !defined(_GLIBCXX_USE_C99)
#warning No C99 library functions
#endif

#if defined(_GLIBCXX_HAVE_BROKEN_VSWPRINTF)
#warning Broken vswprintf
#endif

std::vector<double> GetValues(const std::vector<std::string>& src, int start, int end, std::string typeline)
{
    std::vector<double> ret;
    for(int i = start; i <= end; ++i)
    {
      if(typeline == "E3T" && i == 5)
    {
      ret.push_back(std::strtod(src[2].c_str(), nullptr));
      ret.push_back(std::strtod(src[i].c_str(), nullptr));
    }
      else
    {
          ret.push_back(std::strtod(src[i].c_str(), nullptr));
    }
    }
    return ret;
}

void PrintValues(const std::string& title, std::vector<std::vector<double>>& v)
{
    std::cout << title << std::endl;
    for(size_t line = 0; line < v.size(); ++line)
    {
        for(size_t val = 0; val < v[line].size(); ++val)
        {
            std::cout << v[line][val] << " ";
        }
        std::cout << std::endl;
    }
    std::cout << std::endl;
}

int main()
{
    std::vector<std::vector<std::string>> values;
    std::ifstream fin("example.2dm");
    for (std::string line; std::getline(fin, line); )
    {
        std::istringstream in(line);
        values.push_back(
            std::vector<std::string>(std::istream_iterator<std::string>(in),
            std::istream_iterator<std::string>()));
    }

    std::vector<std::vector<double>> cells;
    std::vector<std::vector<double>> nodes;
    for (size_t i = 0; i < values.size(); ++i) 
    {
        if(values[i][0] == "E3T")
        {
      cells.push_back(GetValues(values[i], 1, 5, "E3T"));
        }
        else if(values[i][0] == "E4Q")
        {
      cells.push_back(GetValues(values[i], 1, 6, "E4Q"));
        }
        else if(values[i][0] == "ND")
        {
      nodes.push_back(GetValues(values[i], 1, 4, "ND"));
        }
    }

    PrintValues("Cells", cells);
    PrintValues("Nodes", nodes);

    return 0;
}

Compilation warning (cygwin gcc c++):

$ g++ read_csv3.cpp -std=c++11
read_csv3.cpp:15:2: warning: #warning No C99 library functions [-Wcpp]

Anyone know how to fix this in cygwin?

Community
  • 1
  • 1
Alex Byasse
  • 322
  • 2
  • 5
  • 16
  • 1
    Not sure what is your main issue, but in `&& i = 5)` you do NOT want `=` you want `==`. – Karthik T Sep 18 '13 at 01:45
  • `std::stod` exists as well. It's easier to use than `std::strtod`. – chris Sep 18 '13 at 01:46
  • 4
    This question appears to be off-topic because it is about a typo, and [we close typo questions](http://meta.stackexchange.com/questions/167342/close-all-the-typo-questions). – Beta Sep 18 '13 at 01:52
  • Im using strtod because of the second issue, I started with stod but comiler says stod not in std so i ran with strtod – Alex Byasse Sep 18 '13 at 01:58

1 Answers1

2

You probably want this :

 if(typeline == "E3T" && i == 5)
                            ^^ equality check

if(typeline == "E3T" && i = 5) complains for lvalue

because typeline == "E3T" && i cant be assigned as 5

However

if(typeline == "E3T" && (i = 5)) compiles but this is not what you need

P0W
  • 46,614
  • 9
  • 72
  • 119
  • I dont think this directly caused the issue, but this is the right line, and perhaps `==` is a different precedence to `=` and lack of brackets caused issues, so this might fix it, +1 – Karthik T Sep 18 '13 at 01:48
  • Ah, looks like we are thinking alike :) – Karthik T Sep 18 '13 at 01:49