raw data file.

i am trying to ifstream variable values from an outside file.
only problem is that it stops after a space.

ex. "plain eggs 1.49"

will only output to the screen plain. then closes program.

any help please?
Post your code.
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>

using namespace std;

int main()
{
struct menuItemType{//struct
string menuItem;
int price;
};
int index;
menuItemType menuList[8];

ifstream inFile;
inFile.open("menu_data.txt");




for(index = 0; index <8; index++){
inFile >> menuList[index].menuItem;
inFile >> menuList[index].price;
cout << menuList[index].menuItem << endl;
}


system("PAUSE");
return EXIT_SUCCESS;
}










here is the raw data


Plain Egg $1.49
Bacon and Egg $2.45
Muffin $0.99
French Toast $1.99
Fruit Basket $2.49
Cereal $0.69
Coffie $0.50
Tea $0.75

Use getline(inFile,menuList[index].menuItem,'$'); instead of
inFile >> menuList[index].menuItem; and it should be fine.

Oh, and change int price; to double price; in your structure definition.

EDIT: Let me explain this a bit... You see, operator >> stops when encountering space characters, (space, tab, newline) so you can't use it to get "Plain Egg" in your string. Using getline like I suggested above, will put "Plain Egg " in your string and get rid of the '$' character setting you up to get the price. Truth is there is an extra space character in your string, but you can ditch it like this: str.erase(str.end()-1); supposing str is your string object.
Last edited on
oh my word, thank you so much. i know that will work. thanks again.
Topic archived. No new replies allowed.