array of structure using new

#include<iostream>
#include<conio.h>
#include<string>
using namespace std;
struct car
{
string make;
int year;

};
void main()
{
int noOfCar;
cout<<"Enter the no. of car to cataloge: ";
cin>>noOfCar;
car * detail = new car[noOfCar];
for(int i=0; i<noOfCar;i++)
{
cout<<"\ncar #"<<i++;
cout<<"\nEnter the make: ";
cin.ignore();
getline(cin, detail.make);
cout<<"\nEnter the year made: ";
cin.ignore();
(cin>>detail[i].year).get();
}
//output
for(int i =0;i<noOfCar;i++)
{
cout<<detail[i].year<<" "<<detail[i].make<<endl;
}
delete[] detail;
_getch();
}

im getting problem with a error,
[i]Unhandled exception at 0x011A54D6 in u5ex6.exe: 0xC0000005: Access violation writing location 0xABABABAB.
and for loop
You shall not change the control variable i inside the body of the loop. So this statement

cout<<"\ncar #"<<i++;

is invalid.

You can substitute it for

cout<<"\ncar #"<<i + 1;
Last edited on
Topic archived. No new replies allowed.