(Solved) : M Attempting Read Data Text File Format Plain Egg145bacon Egg245muffin099french Toast199fr Q29361600 . . .
I’m attempting to read data from a text file in the format:
Plain Egg1.45Bacon and Egg2.45Muffin0.99French Toast1.99Fruit Basket2.49Cereal0.69Coffee0.50Tea0.75
etc….
I’m doing this by attempting to read the data into an arraymenuList[] of type menuItemType, within my function getData, usinga while loop with either ‘eof()’, ‘ifstream’, or ‘ifstream >>variable’ as parameters. The issue is that the while loop stopsafter the first iteration…Source Code:
#include <iostream>#include <iomanip>#include <string>#include <cmath>#include <fstream>using namespace std;//Structure Definitionsstruct menuItemType{ string menuItem; // menu item name double menuPrice; // menu item price};//Function prototypevoid getData(menuItemType menuList[], ifstream&);void showMenu(menuItemType menuList[]);//Global Variables//Main Functionint main() { //Declarations & Initializations menuItemType menuList[5]; //Array with datastructure elements of type menuItemType menuItemType menuOrder[] = {}; // Gen Format// cout << fixed << showpoint << setprecision(2); //Output //Process ifstream inFile; //Declare input variable inFile.open(“input.txt”); // open file cout << “Please select from the following items in our menunnnn”; getData(menuOrder, inFile); return 0;}//Function Definitionsvoid getData( menuItemType menuList[], ifstream& inFile){ int i = 0; while (inFile) { getline(inFile, menuList[i].menuItem); cout << setw(10) << left << menuList[i].menuItem; inFile >> menuList[i].menuPrice; cout << setw(10) << right << menuList[i].menuPrice; i++; } inFile.close(); // close file}
At the moment I’m getting the following output:
Plain Egg 1.45 0Program ended with exit code: 0
I’m still kind of new to coding especially data structures butnever really had issues with while loops before so I could use someexperienced set of eyes.
Thanks in advance, I appreciate it
Expert Answer
Answer to M Attempting Read Data Text File Format Plain Egg145bacon Egg245muffin099french Toast199fr Q29361600 . . .
OR