Reputation: 451
This code runs fine but the for statement by the "meant to read back struct line by line for each" comment code at the bottom doesn't work as it should, it is meant to list items, with prices for each seat, outputting seat# before the list of items for each seat, the seat number part works
EDIT: the error is it shows item in the readout loop to be undeclared, causing this to not compile
line 64 'item' undeclared (first use this function)
` in above string changed to ' for code highlight function
Program being used Dev-C++
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
int N_ITEMS;
int N_SEATS;
int seat;
int tItems;
struct ITM_TYPE {
int seatN;
string name;
float price;
};
int dash(int n)
{
int i = 0;
while(i < (n))
{
cout << "-";
i++;
}
cout << "\n";
return 0;
}
int main()
{
cout << "Enter the number of seats: ";
cin >> N_SEATS; //retrieve number of seats
N_SEATS += 1;
for(seat = 1; seat < N_SEATS; seat++) // loop for each seat
{
cout << "Enter number of items for seat" << seat <<": ";
cin >> N_ITEMS; //get number of items for seat
tItems += N_ITEMS;
ITM_TYPE item[N_ITEMS]; //make N number of item structs
int i = 0;
string name;
float price;
while (i < N_ITEMS)
{
item[i].seatN = seat; //blah blah retrive and add data to stucts
cout << "Input item name: ";
cin >> name;
item[i].name = name;
cout << "item[" << i << "].name SET" << endl;
cout << "Input item price: ";
cin >> price;
item[i].price = price;
cout << "item[" << i << "].price SET" << endl;
i++;
}
}
for(seat = 1; seat < N_SEATS; seat++) //meant to read back struct line by line for each item
{
cout << "seat" << seat << endl;
int i = 0;
while (i < tItems)
{
if (item[i].seatN == seat){cout << item[i].name << " " << item[i].price << endl;}
//cout << item[i].name << endl;
i++;
}
}
system("pause");
}
Upvotes: 1
Views: 411
Reputation: 6597
I went through your code line-by-line in Visual Studio. It works now. Commented all my changes and why.
updated version using STL : http://pastebin.com/CYvX9yrn
#include <iostream>
#include <string>
// !! You had no use for iomanip
using namespace std;
struct ITM_TYPE
{
std::string name;
float price;
int seatN;
};
// !! You had an unused function here
int main()
{
// !! These had no business being global variables
int N_ITEMS;
int N_SEATS;
int seat;
// !! You did not initialize this before using it
int tItems = 0;
cout << "Enter the number of seats: ";
cin >> N_SEATS; //retrive number of seats
// !! This is not needed if you iterate starting at 0
N_SEATS += 1;
// !! You can not dynamically create arrays like you did. Do it like below
// !! Also you used N_ITEMS incorrectly as opposed to N_SEATS
// !! This was also out of scope inside the for (the second for did not know it existed)
// !! Plus it was being re-created on every iteration
ITM_TYPE* item = new ITM_TYPE[ N_SEATS ];
for( seat = 1; seat < N_SEATS; seat++ ) // loop for each seat
{
cout << "Enter number of items for seat" << seat <<": ";
cin >> N_ITEMS; //get number of items for seat
tItems += N_ITEMS;
string name;
float price;
// !! If you are initializing an iterator variable, then incrementing on each
// !! loop, then just use a for instead of a while. Thats why it exists
for( int i = 0; i < N_ITEMS; i++ )
{
item[i].seatN = seat; //blah blah retrive and add data to stucts
cout << "Input item name: ";
cin >> name;
item[i].name = name;
cout << "item[" << i << "].name SET" << endl;
cout << "Input item price: ";
cin >> price;
item[i].price = price;
cout << "item[" << i << "].price SET" << endl;
}
}
for( seat = 1; seat < N_SEATS; seat++ ) //meant to read back struct line by line for each item
{
cout << "seat" << seat << endl;
// !! Like the previous ex-while loop, for is much better for the situation
for(int i = 0; i < tItems; i++ )
{
// !! This was just hard to read how you had it
if( item[i].seatN == seat )
cout << item[i].name << " " << item[i].price << endl;
}
}
// !! Destroy our item array to prevent memory leak
delete[ ] item;
system( "pause" );
// !! You did not indicate a successful end of program
return 0;
}
Upvotes: 1
Reputation: 49850
Since you don't know the total item size in advance, you cannot use a C array. Avoiding them is recommended anyway, so you should rewrite your program to use a vector:
#include <vector>
// ...
vector<ITM_TYPE> item;
for(seat = 1; seat < N_SEATS; seat++) // loop for each seat
{
cout << "Enter number of items for seat" << seat <<": ";
cin >> N_ITEMS; //get number of items for seat
tItems += N_ITEMS;
item.reserve(tItems);
int i = 0;
string name;
float price;
while (i < N_ITEMS)
{
ITM_TYPE it;
it.seatN = seat; //blah blah retrive and add data to stucts
cout << "Input item name: ";
cin >> name;
it.name = name;
cout << "item[" << i << "].name SET" << endl;
cout << "Input item price: ";
cin >> price;
it.price = price;
cout << "item[" << i << "].price SET" << endl;
item.push_back(it);
i++;
}
}
Here is a more idiomatic version of your code, using an item vector for each seat:
#include <iostream>
#include <string>
#include <iterator>
#include <algorithm>
#include <vector>
using namespace std;
struct Item {
int seatN;
string name;
float price;
};
template<typename Char, typename Traits>
basic_ostream<Char, Traits>& operator<<(basic_ostream<Char, Traits>& stream, const Item& item) {
return stream << item.name << " " << item.price << endl;
}
struct Seat {
vector<Item> items;
};
void dash(const unsigned int n) {
cout << string(n, '-') << endl;
}
int main() {
cout << "Enter the number of seats: ";
unsigned int seatCount;
cin >> seatCount; //retrive number of seats
vector<Seat> seats;
seats.reserve(seatCount);
for (unsigned int seatIndex = 0; seatIndex < seatCount; ++seatIndex) { // loop for each seat
cout << "Enter number of items for seat " << seatIndex + 1 << ": ";
unsigned int itemCount;
cin >> itemCount; //get number of items for seat
Seat seat;
seat.items.reserve(itemCount);
for (unsigned int itemIndex = 0; itemIndex < itemCount; ++itemIndex) {
Item item;
item.seatN = seatIndex; //blah blah retrive and add data to stucts
cout << "Input item name: ";
cin >> item.name;
cout << "item[" << itemIndex << "].name SET" << endl;
cout << "Input item price: ";
cin >> item.price;
cout << "item[" << itemIndex << "].price SET" << endl;
seat.items.push_back(item);
}
seats.push_back(seat);
}
for (unsigned int seatIndex = 0; seatIndex < seatCount; ++seatIndex) { //meant to read back struct line by line for each item
cout << "seat " << seatIndex + 1 << endl;
const vector<Item>& items = seats[seatIndex].items;
copy(items.begin(), items.end(), ostream_iterator<Item>(cout));
}
}
Upvotes: 1
Reputation: 3358
Move ITM_TYPE item[N_ITEMS];
above your first for
loop to get it to compile. item
has gone out of scope by the time the next loop runs so it can't access it.
You are also never initialising tItems
which will cause problems. Change:
int tItems;
to:
int tItems = 0;
Upvotes: 1