#include <iostream>
#include <stdlib.h>
#include <string.h>
using namespace std;
struct product{
string productName;
float price;
};
int main()
{
struct product *article;
int n=2; // n represent here the number of products
article= (product*) malloc(n * sizeof(product));
for(int i=0;i<n;i++)
{
cin >> article[i].productName; // <=> (article+i)->productName;
cin >> article[i].price;
}
for(int i=0;i<n;i++)
{
cout << article[i].productName <<"\t" << article[i].price << "\n";
}
return 0;
}
My question is why this is wrong, because when I try to run this I get a segmentation fault. I used the GDB debugger to see which line is causing the problem, and this is the line that was causing this:
cin >> article[i].productName;
Why? This was bothering me for days...