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");
}
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