.csv with searching

My .csv file is like:
Bone,origin,deep/superficial,location, action.
with list of 38 bones.

This is my main.cpp:
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
55
56
57
58
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <cstdlib>
#include "Action.h"
#include "Action.cpp"

using namespace std;

void readCSV(std::istream &input, std::vector< std::vector<std::string> > &output)
{
        std::string csvLine;
        // read every line from the stream
        while( std::getline(input, csvLine) )
        {
                std::istringstream csvStream(csvLine);
                std::vector<std::string> csvColumn;
                std::string csvElement;
                // read every element from the line that is seperated by commas
                // and put it into the vector or strings
                while( std::getline(csvStream, csvElement, ',') )
                {
                        csvColumn.push_back(csvElement);
                }
                output.push_back(csvColumn);
        }
}

int main()
{
        std::fstream file("bones.csv", ios::in);
        if(!file.is_open())
        {
                std::cout << "File not found!\n";
                return 1;
        }
        // typedef to save typing for the following object
        typedef std::vector< std::vector<std::string> > csvVector;
        csvVector csvData;

        readCSV(file, csvData);
        // print out read data to prove reading worked
        for(csvVector::iterator i = csvData.begin(); i != csvData.end(); ++i)
        {
                for(std::vector<std::string>::iterator j = i->begin(); j != i->end(); ++j)
                {
                        std::cout << *j << ", ";
                }
                std::cout << "\n";
        }
        Action action;
        Strengthening strengthening;
        Diagnostic diagnostic;

system ("PAUSE");
return 0;
}


This is my Action.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include "Action.h"

using namespace std;


Action::Action()
{
      cout << "Please enter a action to find bones involved." <<endl;
      cin >> action;
}

Strengthening::Strengthening()
{
      cout << "Please enter the word strengthen followed by a body part to know which bones are involved" << endl;
      cin >> location;
}

Diagnostic::Diagnostic()
{
      cout << "Please enter the bone to know the location and deep/superficial bone" <<endl;
      cin >> bones;
}


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
#ifndef _ACTION_H_
#define _ACTION_H_

#include <iostream>
#include <cstdlib>
using namespace std;

class Action {
public:
     Action();
private:
    string bones;
    string origin;
    string location;
    string action;
    string deeporsuperficial;
};

class Strengthening {
public:
      Strengthening();
private:
    string bones;
    string origin;
    string location;
    string action;
    string deeporsuperficial;
};

class Diagnostic {
public:
       Diagnostic();
private:
    string bones;
    string origin;
    string location;
    string action;
    string deeporsuperficial;
};

#endif
#endif 


For action, if user enters action, it will output the bones involved with that action.
For strengthening, if user enters location, it will output bones involved with that location.
For dagnostic, if user enters bone, it will output location of bone and whether bone is deep/superficial.
Please someone help me how I could search in my .csv file for these.
Last edited on
Please Help :]
Is there any reason why you make three identical classes? They simply describe a row.

Searching within a two dimensinal vector is simple:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
  column_index = ...
  std::string value = ...
...
  for(int i = 0; i != csvData.size(); ++i)
  {
    if(csvData[i][column_index] == value)
    {
      // The row is found
      for(std::vector<std::string>::iterator j = csvData[i].begin(); j != csvData[i].end(); ++j) // Output the row items
      {
        std::cout << *j << ", ";
      }
    }
    std::cout << "\n";
  }
I put three classes for the three queries:
For action, if user enters action, it will output the bones involved with that action.
For strengthening, if user enters location, it will output bones involved with that location.
For dagnostic, if user enters bone, it will output location of bone and whether bone is deep/superficial.

Do I need three classes? I could do it in one class right?
What would I put for column_index and std::string value?
Okay so I changed my Action. cpp file to:#include <iostream>
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
#include "Action.h"

using namespace std;


Action::Action()
{

      cout << "Please enter a action to find bones involved." <<endl;
      cin >> action;
}

Strengthening::Strengthening()
{
      cout << "Please enter the word strengthen followed by a body part to know which bones are involved" << endl;
      cin >> location;

      column_index = 4;
      std::string value = 38;

  for(int i = 0; i != csvData.size(); ++i)
  {
    if(csvData[i][column_index] == value)
    {
      // The row is found
      for(std::vector<std::string>::iterator j = csvData[i].begin(); j != csvData[i].end(); ++j) // Output the row items
      {
        std::cout << *j << ", ";
      }
    }
    std::cout << "\n";
  }

}

Diagnostic::Diagnostic()
{
      cout << "Please enter the bone to know the location and deep/superficial bone" <<endl;
      cin >> bones;
}


So, first I'm trying to do the second part "For strengthening, if user enters location, it will output bones involved with that location. "

It gives me these errors:
line 18: 'column_index was not declared in this scope'
line 19: invalid conversion from 'int' to 'const char*' [-fpermissive]
line 21: 'csvData' was not declared in this scope

Please someone help :]
Last edited on
Please someone help me :] I would greatly appreciate it.
Do I need three classes? I could do it in one class right?
Yes

What would I put for column_index and std::string value?
Ask the user

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int column_index; // Note: index is always numeric!
std::string value;
std::cout << "What type do you want to find?" << std::end
  << "(0) Bone" << std::endl
  << "(1) origin" << std::endl
...
// Note: column_index for Bone -> 0,origin -> 1,deep/superficial -> 2 etc.
// If you want the user to enter other values you have to calculate correspondingly
std::cin >> column_index;


// Here you might involve your classes according to the user input
// Or simply...
std::cout << "What is the value for that type you want to find?" << std::end
std::cin >> value;


line 21: 'csvData' was not declared in this scope
csvData is local variable within main(). You need to pass it to any function that want to use it or make it a global variable.
boneproject.cpp:
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
55
56
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <cstdlib>
#include "Action.h"
#include "Action.cpp"

using namespace std;

void readCSV(std::istream &input, std::vector< std::vector<std::string> > &output)
{
        std::string csvLine;
        // read every line from the stream
        while( std::getline(input, csvLine) )
        {
                std::istringstream csvStream(csvLine);
                std::vector<std::string> csvColumn;
                std::string csvElement;
                // read every element from the line that is seperated by commas
                // and put it into the vector or strings
                while( std::getline(csvStream, csvElement, ',') )
                {
                        csvColumn.push_back(csvElement);
                }
                output.push_back(csvColumn);
        }
}

int main()
{
        std::fstream file("bones.csv", ios::in);
        if(!file.is_open())
        {
                std::cout << "File not found!\n";
                return 1;
        }
        // typedef to save typing for the following object
        typedef std::vector< std::vector<std::string> > csvVector;
        csvVector csvData;

        readCSV(file, csvData);
        // print out read data to prove reading worked
        for(csvVector::iterator i = csvData.begin(); i != csvData.end(); ++i)
        {
                for(std::vector<std::string>::iterator j = i->begin(); j != i->end(); ++j)
                {
                        std::cout << *j << ", ";
                }
                std::cout << "\n";
        }
        Queries queries;

system ("PAUSE");
return 0;
}


Action.cpp:
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
#include <iostream>
#include "Action.h"

using namespace std;


Queries::Queries()
{
      int column_index;
      std::string value;
      std::cout << "What type do you want to find?" << std::endl
     << "(0) Bone" << std::endl
     << "(1) origin" << std::endl
     << "(2) deeporsuperior" << std::endl
     << "(3) location" << std::endl
     << "(4) action" << std::endl;

      std::cin >> column_index;

    std::cout << "What is the value for that type you want to find?" << std::endl;
    std::cin >> value;

  for(int i = 0; i != csvData.size(); ++i)
  {
    if(csvData[i][column_index] == value)
    {
      // The row is found
      for(std::vector<std::string>::iterator j = csvData[i].begin(); j != csvData[i].end(); ++j) // Output the row items
      {
        std::cout << *j << ", ";
      }
    }
    std::cout << "\n";
  }
}


Action.h:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
 #ifndef _ACTION_H_
#define _ACTION_H_

#include <iostream>
#include <cstdlib>
using namespace std;

class Queries {
public:
     Queries();
private:
    string bones;
    string origin;
    string location;
    string action;
    string deeporsuperficial;
};

#endif  
Now, how do I do this?
line 23: 'csvData' was not declared in this scope
csvData is local variable within main(). You need to pass it to any function that want to use it or make it a global variable.
Last edited on
Now, how do I do this?
What?

Passing csvData as a parameter? Look at line 11/42 of boneproject.cpp

how to make global variable?
So, I could just mention those functions in my Action.h file?
I don't know what function you mean. If you want to use csvData withing Queries do it like so:

Action.cpp:
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
#include <iostream>
#include "Action.h"

using namespace std;


Queries::Queries(const std::vector< std::vector<std::string> > &csvData)
{
      int column_index;
      std::string value;
      std::cout << "What type do you want to find?" << std::endl
     << "(0) Bone" << std::endl
     << "(1) origin" << std::endl
     << "(2) deeporsuperior" << std::endl
     << "(3) location" << std::endl
     << "(4) action" << std::endl;

      std::cin >> column_index;

    std::cout << "What is the value for that type you want to find?" << std::endl;
    std::cin >> value;

  for(int i = 0; i != csvData.size(); ++i)
  {
    if(csvData[i][column_index] == value)
    {
      // The row is found
      for(std::vector<std::string>::iterator j = csvData[i].begin(); j != csvData[i].end(); ++j) // Output the row items
      {
        std::cout << *j << ", ";
      }
    }
    std::cout << "\n";
  }
}


Action.h:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
 #ifndef _ACTION_H_
#define _ACTION_H_

#include <vector>
#include <string>
#include <iostream>
#include <cstdlib>

class Queries {
public:
     Queries(const std::vector< std::vector<std::string> > &csvData);
private:
    string bones;
    string origin;
    string location;
    string action;
    string deeporsuperficial;
};

#endif   
Topic archived. No new replies allowed.