Struct Involving Reading From File

This is a homework issue I am attempting to work through.

Text File:
Car,1996.75
Truck,3971.54
Motorcycle,1429.09
Freight Train,356478.98

*****START CODE********

#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>

using namespace std;

const int NUM_OF_ITEM = 8;

struct vehItemType
{
string vehItem;
double vehPrice;
};

void getData(vehItemType vehList[NUM_OF_ITEM], ifstream& menuData);

int main()
{
ifstream vehData;
vehItemType vehList[NUM_OF_ITEM];
int counter;

vehData.open("C:\\veh.txt");
if(!vehData)
{
cout << "Error: file could not be opened." << endl;
return 1;
}

getData(vehList, vehData);
cout << "Welcome To Johnny's Dealership" << endl;
cout << "Vehicle Items:" << endl << endl;

cout << fixed << showpoint << setprecision(2);
for(counter = 0; counter < 8; counter++)
{
cout << setfill(' ') << left << setw(20) << vehList[counter].vehItem << ":" << right << "$" << vehList[counter].vehPrice << endl;
}
cout << endl;

} //end main

void getData(vehItemType vehList[NUM_OF_ITEM], ifstream& menuData)
{
int line = 0;

while(!vehData.EOF())
{
getline(vehData,menuList[line].menuItem,',');
getline(vehData,menuList[line].menuPrice,);
counter++;
}

vehData.close();
} // end getData

******** END CODE ************

I get errors regarding "cannot convert int to string" or something along those lines. I understand that the error will no longer present if I change the struct to:
struct menuItemType
{
string menuItem;
string menuPrice;
};

I am looking to know how to read the text from the file correctly to allow the text string to be read as well as the number. In addition have the item displayed as in the code. I need to retain the double so I can perform calculations down the line.

I've been beating my head against the desk for a week giving it the best try I can. Any help would be appreciated. Thank you!
[2416 byte] By [kc0poc] at [2007-11-20 11:27:02]
# 1 Re: Struct Involving Reading From File
The getline() function only reads data in as a string. Just convert the returned string to a double by using either atof() or strtod(), then assign this double to your structure member.

#include <cmath>
//...
struct vehItemType
{
string vehItem;
double vehPrice;
};

//...
std::string temp;
getline(vehData,menuList[line].menuItem,',');
getline(temp);
menuList[line].vehPrice = atof( temp.c_str() );

Regards,

Paul McKenzie
Paul McKenzie at 2007-11-10 22:25:27 >
# 2 Re: Struct Involving Reading From File
1) to read into numeric data, you can use operator >> For example:

double value;
vehData >> value;

2) There are usually problems with mixing getline() and operator >> ...
After doing the operator >> you need to "skip" the rest of the line.

3) Here is one method ...

while(getline(vehData,menuList[line].menuItem,','))
{
vehData >> menuList[line].menuPrice;
counter++; // ?? why counter ?
string dummy;
getline(vehData,dummy); // skip rest of the line
}

You could use the ignore() function instead of the last getline()
in the loop.
Philip Nicoletti at 2007-11-10 22:26:33 >
# 3 Re: Struct Involving Reading From File
Paul & Phillip-

Thank you both for your quick responses. I've used both examples and they both work well. Thank you VERY MUCH!! Life is much better now.

Phillip, your question about "why counter?". I changed the various variable names when I copied the code over and negated to catch everything.

Thank you again, gentlemen.
kc0poc at 2007-11-10 22:27:32 >