1

Basically, I want to make a simple function for a larger project, this will actually end up as a Header file that handles all my inventory. Any who, I just want it to be able to read/pull/input the Data from a .csv file format... Or I can even do a .txt if it is easier to make this function work, As long as I can open it up in MS Excel and edit the items and add new items, when the function is ran it will Open up "Example.csv" and for example, the function will look for the cell in Column 1 - 'sSwordName' and it will pull the data in the row FOLLOWING that cell, excluding that first column completely in all the inputs essentially... It will group it together, or what ever it may have to do in order to ASSIGN it to that Variable. Please see Code.h (Comments) to see my questions about my source code.

Code.h -

#include "stdafx.h"
#include <string>
#include <fstream>
#include <sstream>
#include <iostream>
#include <vector>
using namespace std;

char sSwordName[100][25] = {};
int sSwordLvlR[100] = {};

vector<string> split_at_commas(const string & row)
{
    vector<string> res;
    istringstream buf(row);
    string s;
    while (getline(buf, s, ','))
        res.push_back(s);
    return res;
    system("pause");
}

/* Question 1: Where & How do I properly add 'ifstream wInv("Example.csv");' in order to Load the CSV file it is reading? */
/* Question 2: Line 38 & 46. */
/* Question 3: Line 39 & 47 */

int main()
{
    int i = 1;

    string line;
    string row;
    vector<string> values = split_at_commas(line);
    if (values[0] == "sSwordName")
    {
        for(int i = 1; i < values.size(); ++i);
        {
            /*int i error: Object must have a pointer-to-object type*/ 
            sSwordName[100][25][i - 1] = /*How do I convert string to char?*/(vector[i]);
        }
    }
    else if (values[1] == "sSwordLvlR")
    {
        for(int i = 1; i < values.size(); ++i);
        {
            /*int i error: Object must have a pointer-to-object type*/
            sSwordLvlR[i - 1] = /*How do I Convert string to int?*/(vector[i]);
        }
    }
}
/* Question 4: Is there anything else that is wrong in this? If so, how would I fix it */

Example.csv -

sSwordName,Wooden Shortsword,Bronze Shortsword,Iron Shortsword,Steel Shortsword,Titanium Shortsword
    sSwordLvlR,1,3,5,6,10

More Information On The CSV:

sSwordName,Wooden Shortsword,Bronze Shortsword,Iron Shortsword,Steel Shortsword,Titanium Shortsword
sSwordLvlR,1,3,5,6,10

^Is not the ONLY way I can format it, for convenience I can do something like this (Below, E2.csv). If it is easier to make the function, function.;

sSwordName,"Wooden Shortsword","Bronze Shortsword","Iron Shortsword","Steel Shortsword","Titanium Shortsword"
sSwordLvlR,"1,","3,","5,","6,","10,"

I can even format it in this way;

sSwordName,"{"Wooden Shortsword","Bronze Shortsword","Iron Shortsword","Steel Shortsword","Titanium Shortsword"};
sSwordLvlR,"{1,3,5,6,10};"

Once again, I greatly appreciate any help. I thank you in Advanced! -Leaum

Leaum
  • 33
  • 1
  • 1
  • 7
  • Do you have only 1 line of data ? If not gave part of the answer. – Jagannath Aug 28 '11 at 13:34
  • @jagansai Actually no, There are 10+ Lines of data, for the sake of figuring it out though I removed all of them and only did 1 line that has to be converted to char and 1 to int – Leaum Aug 28 '11 at 13:35
  • @jagansai that is, if you were talking about data in the CSV file – Leaum Aug 28 '11 at 13:36
  • I think it's worth keeping in mind that Excel can not necessarily be relied on to open a CSV file. If Excel gets it into its very tiny little mind that "3,14" means 3.14, e.g. for a Norwegian installation, then trouble ensues. And using some other character e.g. like semicolon, can also be problematic. So first of all, before wasting time on this, I'd test to see whether the the idea of relying on and trusting Excel is a good idea. Perhaps (I'm just speculating) the spreadsheet from OpenOffice might be better. – Cheers and hth. - Alf Aug 28 '11 at 13:53
  • possible duplicate of [Visual Studio C++ Data from CSV File into Variable Arrays](http://stackoverflow.com/questions/7220503/visual-studio-c-data-from-csv-file-into-variable-arrays) – Bo Persson Aug 28 '11 at 15:35
  • @Alf Excel is such terrible software. That must be the reason why people pay to use it over the free open office one... yeah... right. – Mranz Aug 28 '11 at 16:09
  • @Mranz: I think you've misunderstood both the point (at specification level) and why people have used various software programs such as MS-DOS. – Cheers and hth. - Alf Aug 28 '11 at 16:15
  • @All It's not really the program itself that is useful in my case, it is the fact that it puts it into a clean, easily edited format that I can save the output in many forms. With that I can easily add, edit, or remove certain items and keep a 'database' of all the items parameters without having TONS of code in a header file. Ya follow? – Leaum Aug 30 '11 at 06:49

0 Answers0