Dec 9, 2018 at 4:02am UTC
I am writing a program that takes a diner menu from a file and writes the information into an array of a structure, im trying to use getline() but i dont know if i am using it correctly, when i run the program the counsel crashes immediately. why is this and how do i stop it from happening?
here is my code:
Header:
1 2 3 4 5 6 7 8 9 10
#include <string>
using namespace std;
struct menuItemType {
string menuItem;
double menuPrice;
};
menuItemType MenuList[8];
function:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
#include "Header.h"
using namespace std;
int main() {
int counter = 0; // to count how many
double x; // placeholder for prices
const int MAX_ITEMS = 20;
ifstream menuItemData; // Definition of menuItemData file containing menu items
ifstream menuPriceData;// Definition of menuPriceData file containing menu items
menuItemType MenuList[8]; //array for menu
cout << fixed << showpoint << setprecision(2);
menuItemData.open("MenuItem.dat" ); // opens file with menu items
menuPriceData.open("MenuPrice.dat" ); // opens file with menu prices
while (!menuItemData.eof()) {
getline(menuItemData,MenuList[counter].menuItem );
counter++;
}
counter = 0; // sets counter back to 0;
//cout << counter; // debugging
while (counter < MAX_ITEMS && menuPriceData >> x) { // reads information into array MenuList[].menuPrice
MenuList[counter].menuPrice = x;
//cout << MenuList[counter].menuPrice << endl; // debugging
counter++;
//cout << x << endl;
}
//for (int i = 0; i < counter; i++) {
// cout << MenuList[i].menuPrice << endl; // Tests to see if array works
// }
*/
system("pause" );
return 0;
}
If anyone could help me understand this that would be appreciated
Thanks
Last edited on Dec 9, 2018 at 4:02am UTC
Dec 9, 2018 at 4:29am UTC
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
#include <iostream>
#include <string>
#include <fstream>
struct menu_item {
std::string name ;
double price = 0 ;
};
int main()
{
const int MAX_ITEMS = 20 ;
menu_item menu_list[MAX_ITEMS] ;
std::ifstream names_file( "MenuItem.dat" ) ;
if ( !names_file.is_open() ) {
std::cerr << "unable to open names file\n" ;
return 1 ;
}
std::ifstream prices_file( "MenuPrice.dat" ) ;
if ( !prices_file.is_open() ) {
std::cerr << "unable to open prices file\n" ;
return 1 ;
}
int counter = 0 ;
while ( counter < MAX_ITEMS && // array is not full, and
std::getline( names_file, menu_list[counter].name ) && // successfully read item name, and
!menu_list[counter].name.empty() && // it is a valid item name, and
prices_file >> menu_list[counter].price && // successfully read item price, and
menu_list[counter].price > 0 ) { // we have a valid price
// we have successfully read a valid item name and price
++counter ; // increment the counter for number of items read
}
if ( counter == 0 ) {
std::cerr << "no items were read.\n" ;
return 1 ;
}
// ...
}
Last edited on Dec 9, 2018 at 4:31am UTC
Dec 9, 2018 at 4:51am UTC
i am still confused cause this doesnt seem to work either. like it runs but when i try to cout to see if the file put the information into the array it doesnt show anything.
what about my code is incorrect. the while loop for the menuitem works when i don't have a counter in the loop but that doesn't write to the array properly, but having the counter in my loop makes it so the counsel crashes
Last edited on Dec 9, 2018 at 5:00am UTC