I have this section for a program I'm working on where I am inputting into a vector within a structure. The syntax all seems correct, but I get this weird error I've never seen before.
(I'm using XCode)
It reads: Thread 1:EXC_BAD_ACCESS (code=1, address 0x0)
I've done a little reading, and I guess this has something to do with memory, but I'm too new at this to fully understand this.
#include <vector>
struct fileSave
{
vector<string> date;
}
int main ()
{
fileSave receipt;
//receipt.trip is defined earlier in the code and works fine
for (int i=0; i<receipt.trip; i++)
{
cout<<"Input date (mm/dd/yy): ";
cin>>receipt.date[i];
}
}
Is there a way to change the syntax or logic to avoid this type of error?
You can't use the operator[] with an empty vector, instead use a temporary variable to get the input from the user then push the value into the vector.
By the way that snippet shouldn't compile because of a missing semicolon that terminates the structure definition.
You can't use the operator[] with an empty vector, instead use a temporary variable to get the input from the user then push the value into the vector.