Read input parms in from comma delimeted file with ints and doubles.
input file is myInput.txt which has the following data starting from col#1:
2014,0.5,1850.4788,
Note: None of doubles are negative and can assume no spaces between commas.
Also can assume first value is an 'int' and the next two values are
'double' type.
The code below does exactly what I want as the first step in the processing:
#include <string>
#include <fstream>
.........
const int NUM_DATA1 = 3;
string inputDataStr[NUM_DATA];
ifstream inputsFile(myInput.txt");
for(int i = 0; i < NUM_DATA; i++)
{
getline( inputsFile, inputDataStr[i], ',' );
}
inputsFile.close();
Now that I have the array of strings I need to convert them to
either int or double.
I found the following code below in your Forum for getting string to double
but there is one problem. The string is defined as 'char buffer[256]'
and Not 'string'. The same is true for atoi(buffer).
HOW CAN I RESOLVE THIS ISSUE.
I use to program in c and c++ about 15 years ago and I am trying to show
someone else what I want here. Seems like there should be an easy you
to convert a 'string' to 'int' or 'double' assuming my input is exactly
formated as describled above.
CODE from Forum:
/* atof example: sine calculator */
#include <stdio.h> /* printf, fgets */
#include <stdlib.h> /* atof */
#include <math.h> /* sin */
int main ()
{
double n;
char buffer[256];
printf ("Enter degrees: ");
fgets (buffer,256,stdin);
n = atof (buffer);
printf ("The sine of %f degrees is %f\n" , n);
return 0;
}