Read and process input parm file

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;
}
Last edited on
I will look at stoi and stod.
Thanks very much for helping me with this problem.

Before I read your reply I found on the Forum another possible solution.
Would my solution below also work?

I am paying someone to run my code but I am doing most of the code myself in order to save money by him not writing it.

I just would like to know if my code will work before he runs it so can save some pain and money. Besides it fun finding answers on the Forum.

Thanks ahead of time for anyone that can answer whether the code below should work.

// input filename: Input.txt
// d_timezone,calcYrs,startYr,Price,
// 5.0,2,1998,180.0

#include <string>
#include <fstream>

const int NUM_DATA1 = 4;
string inputDataStr[NUM_DATA1];
ifstream inputsFile("c:\\myParmInput\\Input.txt");
for ( int i = 0; i < NUM_DATA1; i++)
{
getline( inputsFile, inputDataStr[i], ',' );
}
inputsFile.close();

d_timezone = atof(inputDataStr[0].c_str());
calcYrs = atoi(inputDataStr[1].c_str());
startYr = atoi(inputDataStr[2].c_str());
Price1 = atof(inputDataStr[3].c_str());
Last edited on
Topic archived. No new replies allowed.