I am doing a project where I am writing automated billing system for a fake restaurant. The program is supposed to take a text file containing the menu, put it into a array or vector of structs, show the menu, let the customer order, and print a receipt. I am using a global vector of structs for the menu.
This block of code is everything related to the problem.
`
#include <iostream>
#include <fstream>
#include <vector>
//there is more code to this program, but the fault occurs very soon in the program
//and none of the rest of the code has any relevance.
//also, I don't really think that the problem is with trying to input, but I don't have enough experience to rule it out.
using namespace std;
struct menuItemType
{
string menuItem; //this is the name of the item
double menuPrice; // this is the price of the item
int menuCount;
};
vector<menuItemType> menuList; //the menu can be any size so I don't know how big it will be at this point. I'm using a vector to avoid having to declare a size
// I also have 2 other functions and some extra code in main that all need to access this vector. That is why I made it global
void getData() //this function opens the text file containing the menu and tries to read in each line.
{
ifstream input;
input.open("inData.txt");
input.peek();
int i = 0;
string item;
double price;
while(!input.eof())
{
getline(input,menuList[i].menuItem); //This is the line creating the fault.
input >> menuList[i].menuPrice;
i++;
input.peek();
}
}
int main()
{
getData();
return 0;
}
`
I have tried debugging and have determined that the segmentation fault is not specific to the line commented in the code snippet. The fault seems to occur whenever I try to input to a member of a struct inside the vector. I've tried using cin
as well so I don't believe the text file stream is the problem.
The text file looks like this:
Bacon and eggs
1.00
Muffin
0.50
Coffee
0.90
Specifically, my question is: Why does trying to input to a member of a struct inside the vector cause a segmentation error and how can I fix it.
Sorry for the long explanation and awkward formatting. I am fairly new to both stack overflow, and c++.