I have been trying to figure this little section of my larger project out for a while now. It has stumped me pretty well too... I am trying to take information from an Excel CSV File (See Example.CSV) and have it input into the Cpp project.. Now before I get to the way I have been working on it, I thought I might first run some other ways by ya guys and see which you thought would be most efficient?
1: Example1.csv: Item Values still following the; sSwordName, lvlReq, minAtk, maxAtk, atkRate, sPrice, sValue, strReq, atkReq, intReq. Column Headers as shown in Example.csv.. Would it be ideal to make like a header that pulls Item Information from the CSV file when told to, For example: If the player is at a shop, he wants to appraise his sword. When he clicks on the value button, it searches the 'Sword.csv' file for the item ID(Maybe) and will return the '5th', given the value of the 5th column in that items row is the Value it's worth.. And just create some kind of function that does that for everything? If so, any ideas how to do that.
2: The main way I have been trying to get this to work: in Example.csv I have all the items values I use in my program.. They are assigned to int and char variables, at least that's my goal.. Please direct to Example.cpp for my (fail) code and explanation..
Example.cpp:
#include "stdafx.h"
#include <cstdlib>
#include <fstream>
#include <ios>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <string>
#include <sstream>
using namespace std;
/*** These are the Variables I use for a weapon, they corrospond according to number in list: Wooden Shortsword is first in the
*** sSwordName Array, so it's lvl requirment would be 1 since it is first.
*** Idealy this is the way I want things to work... Unless anyone has a better idea, I am open to hear them. I can always
*** learn more, especially from others. ***/
char sSwordName[10][25] = {"Wooden Shortsword", "Bronze Shortsword", "Iron Shortsword", "Steel Shortsword", "Titanium Shortsword"};
int sSwordLvlR[10] = {1, 3, 5, 6, 10};
int sSwordV[10] = {5, 10, 18, 25, 50};
int sSwordP[10] = {10, 20, 40, 60, 100};
int sSwordMinAtk[10] = {0, 0, 0, 0, 0};
int sSwordMaxAtk[10] = {4, 6, 10, 14, 20};
int sSwordAtkRate[10] = {0, 2, 3, 4, 6};
int sSwordStrR[10] = {1, 6, 11, 16, 22};
int sSwordAtkR[10] = {1, 6, 11, 16, 22};
int sSwordIntR[10] = {1, 3, 5, 8, 12};
/*** Now this is as close as I have been able to get to get this to work, sadly it is still off base...
*** I need it to take for example: Row 1 > Exclude Column 1 > Input Data > Assign to sSwordName[5][25] Array.
*** My issue is, finding a way to loop it where it takes all the cells in Row 1, ignores the first cell
*** (I think making it do like "\n" for it would work? Not sure) and then loop through the rest of the rows
*** repeating the same algorithm, Ignoring the first cell, inputting, assinging them to the variable arrays?
*** It is much much easier to edit item stats and add new items in Excel, than having to do it via code as you
*** may imagine..
*** I am pretty sure my code is far from what would be best, I bet it needs to be re-written a new way as well..
*** I would greatly appreciate Anyone who can help me accomplish this task.. It would really make my life
*** A lot easier, as well as make my project code considerbly shorter.. */
int main()
{
int sSwordLvlR;
double y;
string data;
string a;
ifstream wInv("Example.csv");
while (getline(wInv, data))
{
if (data.empty()) continue;
istringstream ss( data );
{
string inf;
getline( ss, inf );
stringstream( inf ) >> sSwordLvlR;
cout<<" "<<sSwordLvlR;
}
}
system("Pause");
return 0;
}
/* I have not be able to figure out how to make it take the data for the Names yet either */
Example.csv
sSwordName,Wooden Shortsword,Bronze Shortsword,Iron Shortsword,Steel Shortsword,Titanium Shortsword
sSwordLvlR,1,3,5,6,10
sSwordMinAtk,0,0,0,0,0
sSwordMaxAtk,4,6,10,14,20
sSwordAtkRate,0,2,3,4,6
sSwordP,10,20,40,60,100
sSwordV,5,10,18,25,50
sSwordStrR,1,6,11,16,22
sSwordAtkR,1,6,11,16,22
sSwordIntR,1,3,5,8,12
Example1.csv
sSwordName,lvlReq,minAtk,maxAtk,atkRate,sPrice,sValue,strReq,atkReq,intReq
Wooden Shortsword,1,0,4,0,10,5,1,1,1
Bronze Shortsword,3,0,6,2,20,10,6,6,3
Iron Shortsword,5,0,10,3,40,18,11,11,5
Steel Shortsword,6,0,14,4,60,25,16,16,8
Titanium Shortsword,10,0,20,6,100,50,22,22,12
As I said in the cpp, I would really really really appreciate anyone who can help me work out that code to it's ideal working state... As well as anyone who wishes to contribute ideas to improve it's overall flow..
Thanks everyone, Leaum
External Links:
Example.cpp - http://pastebin.com/URWTGVq6 Example.csv - http://pastebin.com/924wvVX2