Im trying to learn c++ loops and structures with the c++ primer plus book.

Here is the question no6: http://books.google.com/books?id=zuyAIZ9ZIskC&pg=PA205&lpg=PA205&dq=c%2B%2B+primer+plus+programming+exercise+how+many+cars+do+you+wish+to+catalog&source=bl&ots=mbyXM-wZBZ&sig=o0nBYx-ZkCShRJztCVoFTy4S1eM&hl=en&ei=BqxQTMKVK4eosQPi_bynBw&sa=X&oi=book_result&ct=result&resnum=1&ved=0CBIQ6AEwAA#v=onepage&q&f=false

My current code:
#include <iostream>
#include <string>
struct car
{
std::string carname;
int yearmade;
};
int main(void)
{
using namespace std;
int zero = 0;
int vb2 = 1;
int count2;
int zero2 = 0;
int count = 0;

int strcture;
cout << "How many cars do you wish to catalog? ";
cin>>strcture;
car * cars[strcture] = new car;

while(count != strcture)
{
cout <<"Car# " << vb2;
cout <<"Please enter the make: ";
getline(cin, cars[zero]->carname);
cin.get();
cout <<" Please enter the year made: ";
cin.get();
cin >> cars[zero]->yearmade;
cin.get();
count++;
vb2++;
zero++;
}

cout <<"Here is your collection:\n";
while(count2 =! strcture)
{
cout<< cars[zero2]->yearmade << cars[zero2]->carname;
zero++;
}
return 0;
}


im stuck on the part on the while loops, my compiler just keeps telling me: variable-sized object `cars' may not be initialized|

plz help.

thanks.

Your syntax is out of order here:

car * cars[strcture] = new car;

The syntax is incorrect here (look at the examples in your book and even within your code more closely).

while(count2 =! strcture)

If you're making a while loop, make the index variable and the count variable one in the same. In this above example you have count and zero2 and zero and they are all mixed up.

hmm, would this be better?: #include <iostream>
#include <string>
struct car
{
std::string carname;
int yearmade;
};
int main(void)
{
using namespace std;
int zero = 0;
int carcount = 1;
int count = 0;

int strcture = 0;
cout << "How many cars do you wish to catalog? ";
cin>>strcture;
car * cars = new car[strcture];

while(count != strcture)
{
cout <<"Car# " << carcount;
cout <<"Please enter the make: ";
getline(cin, cars[zero].carname);
cin.get();
cout <<"Please enter the year made: ";
cin.get();
cin >> cars[zero].yearmade;
count++;
vb2++;
zero++;
}
count = 0;
zero = 0;
cout <<"Here is your collection:\n";
while(count =! strcture)
{
cout<< cars[zero].yearmade << cars[zero].carname;
count++;
zero++;
}
delete cars;
return 0;
}

i tried it and when i go to input the make of the car, the loop spirals out of control. gives me garbage sometimes.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.