I am trying to read from a text file and input the data into a struct. I have read through all the threads on this topic and have not been able to work out where I am going wrong. My text file is:
Plain_Egg 1.45
Bacon_and_Eggs 2.45
Muffin .99
French_Toast 1.99
Fruit_Basket 2.49
Cereal .69
Coffee .50
Tea .75
My code:
// Session9.cpp
//
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
const int NUM_SIZE = 7;
struct menuItemType
{
string menuItem;
double menuPrice;
};
void myPause();
void getData(ifstream& indata, menuItemType menu[], int listSize);
int main()
{
ifstream inData;
menuItemType menuList[7];
inData.open("menu.txt");
getData(inData, menuList, NUM_SIZE);
inData.close();
inData.clear();
myPause();
return 0;
}
// Function to pause the program to view window contents
void myPause()
{
cout << "Press ENTER to continue...";
cin.clear();
cin.sync();
cin.get();
}
// Function to store data into struct
void getData(ifstream& indata, menuItemType list[], int listSize)
{
int index;
for(index=0; index < listSize; index++)
{
indata >> list[index].menuItem;
indata >> list[index].menuPrice;
}
for(index=0; index < listSize; index++)
{
cout << list[index].menuItem << ", ";
cout << list[index].menuPrice << endl;
}
}
Attached is the output I get.
I am not sure how C++ reads in the text file. Do I need to put commas inbetween the data? From the output I see that the first string data isn't being read at all. I am guess the second isn't either. I just need a subtle hint as to what I am doing wrong please.