#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <cstdlib>
#include "Action.h"
#include "Action.cpp"
usingnamespace 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;
}
#include <iostream>
#include "Action.h"
usingnamespace 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;
}
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.
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?
#include "Action.h"
usingnamespace 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
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.
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <cstdlib>
#include "Action.h"
#include "Action.cpp"
usingnamespace 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;
}
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.