upperbounds issue with c++ from a csv file
i have the Following vb.net 2008 routine i am trying to get c++ to have the same functionality having alot of issues with c++ conversion, any help with this would be great...
Code:
Public Sub New()
Try
If ORIHoles Is Nothing Then
ReDim ORIHoles(49)
Dim ff As Integer = FreeFile()
Dim tempstr As String
Dim atempstr() As String
FileOpen(ff, "postable.csv", OpenMode.Input, OpenAccess.Read, OpenShare.Shared)
Do While Not EOF(ff)
tempstr = LineInput(ff)
atempstr = tempstr.Split(",")
If atempstr.GetUpperBound(0) = 2 Then
ORIHoles(CInt(atempstr(0))).Angle = CDbl(atempstr(1))
ORIHoles(CInt(atempstr(0))).Hypot = CDbl(atempstr(2))
End If
Loop
FileClose(ff)
End If
Catch ex As Exception
GeneralErrorHandler(ex.ToString)
End Try
End Sub
Re: upperbounds issue with c++ from a csv file
I don't know VB but it looks like you are trying to open a file, read a csv line, parse it into 3 values, store values 1 & 2 into a container indexed on value 0 and repeat for all lines in the file? This is fairly easy in C++ if this is what you want.
What is your C++ attempt? Can you post a sample input file?
Re: upperbounds issue with c++ from a csv file
Quote:
Originally Posted by
2kaud
I don't know VB but it looks like you are trying to open a file, read a csv line, parse it into 3 values, store values 1 & 2 into a container indexed on value 0 and repeat for all lines in the file? This is fairly easy in C++ if this is what you want.
What is your C++ attempt? Can you post a sample input file?
so the CSV looks like this 48 Values ,,,
01,-0.84415,0.35416
02,-0.64350,0.29412
03,-0.35877,0.25129
04,0.00000,0.23529
05,0.35877,0.25129
06,0.64350,0.29412
07,0.84415,0.35416
08,-0.68573,0.41802
09,-0.49935,0.36853
10,-0.26625,0.33535
11,0.00000,0.32353
12,0.26625,0.33535
13,0.49935,0.36853
14,0.68573,0.41802
15,-0.57134,0.48951
16,-0.40489,0.44799
17,-0.21109,0.42111
18,0.00000,0.41176
19,0.21109,0.42111
20,0.40489,0.44799
so what i am trying is to replicate the same functionality in c++ in Linux ,
so i have found, this snippet, i could hand code all 48 values in this mp.insert({ -0.84415,0.35416 }); this would be for 1
is there a workaround to this in C++ for linux g++ can we use the map ??
Code:
int main()
{
// initialize container
map<int, int> mp;
// insert elements in random order
mp.insert({ 12, 30 });
mp.insert({ 11, 10 });
mp.insert({ 15, 50 });
mp.insert({ 14, 40 });
// when 11 is present
auto it = mp.upper_bound(11);
cout << "The upper bound of key 11 is ";
cout << (*it).first << " " << (*it).second << endl;
// when 13 is not present
it = mp.upper_bound(13);
cout << "The upper bound of key 13 is ";
cout << (*it).first << " " << (*it).second << endl;
// when 17 is exceeds the maximum key, so size
// of mp is returned as key and value as 0.
it = mp.upper_bound(17);
cout << "The upper bound of key 17 is ";
cout << (*it).first << " " << (*it).second;
return 0;
}
:: OUTPUT of sample
The upper bound of key 11 is 12 30
The upper bound of key 13 is 14 40
The upper bound of key 17 is 4 0
Re: upperbounds issue with c++ from a csv file
I don't understand what this C++ code has to do with the VB code in post #1 :confused:
For a simple C++ conversion of the VB code consider:
Code:
#include <fstream>
#include <vector>
#include <string>
#include <sstream>
#include <iostream>
using namespace std;
int main()
{
ifstream ifs("postable.csv");
if (!ifs.is_open())
return (cout << "Cannot open input file\n"), 1;
struct data {
double angle = 0;
double hypot = 0;
data(double a, double h) : angle(a), hypot(h) {}
};
vector<data> vd;
//Populate the vector from the file
for (string line; getline(ifs, line); ) {
istringstream iss(line);
size_t ind;
double ang, hypot;
char delim;
iss >> ind >> delim >> ang >> delim >> hypot;
vd.emplace_back(ang, hypot);
}
//Display the contents of the vector
for (const auto& v : vd)
cout << v.angle << " " << v.hypot << endl;
}
This will read the file and populate the vector vd with the data. The first csv value (index?) is not used as the values are added sequentially to the vector.
Re: upperbounds issue with c++ from a csv file
0 1 2 3 4 5 6
* * * * * o *
o * * * * * *
* o * * * * *
* * * * * * 0
* * * * o * *
* * * o * * *
* * o * * * *
5 x 1 =
0 x 7 =
1 x 49 =
6 x 343 =
4 x 2401 =
3 x 16807 =
2 x 117649 =
----------------------------
SUM : (CART-ID)
Re: upperbounds issue with c++ from a csv file
this is a vision cart reader as the carts are read it has a certain pattern in the grid
so what the initial goal was to provide an equivalent Class or function to the vb.net snippet earlier in the post it reads a CSV file that has 48 Values pertaining to locations on a 7 x 7 Grid 7^2
Re: upperbounds issue with c++ from a csv file
Quote:
Originally Posted by
ccodebase
the initial goal was to provide an equivalent Class or function to the vb.net snippet earlier in the post it reads a CSV file that has 48 Values pertaining to locations on a 7 x 7 Grid 7^2
Which is my code in post #4. Are we dealing in polar co-ordinates here? So given that the vector now has these 49 polar? values, what is the next step? How are converting these polar? co-ordinates onto the 7 x 7 grid?
PS Doing a simple conversion assuming polar to Cartesian doesn't seem to give anything useful?