Ten kod działa dobrze, ale instrukcja for przez kod komentarza „przeznaczony do odczytu wstecznego struct wiersz po wierszu dla każdego” na dole nie działa tak, jak powinna, ma na celu wylistowanie pozycji z cenami za każde stanowisko, wyprowadzając miejsce # przed listą pozycji dla każdego miejsca działa numer miejsca
EDYCJA: błąd polega na tym, że element w pętli odczytu nie jest zadeklarowany, co powoduje, że się nie kompiluje
line 64 'item' undeclared (first use this function)
` w powyższym łańcuchu zmieniono na ' dla funkcji podświetlania kodu
Używany program 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");
}
3 odpowiedzi
Przejrzałem Twój kod wiersz po wierszu w programie Visual Studio. Teraz działa. Skomentował wszystkie moje zmiany i dlaczego.
zaktualizowana wersja przy użyciu 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;
}
Przenieś ITM_TYPE item[N_ITEMS];
nad swoją pierwszą pętlę for
, aby ją skompilować. item
wyszedł poza zakres do czasu uruchomienia następnej pętli, więc nie ma do niego dostępu.
Nigdy też nie inicjujesz tItems
, co spowoduje problemy. Zmiana:
int tItems;
Do:
int tItems = 0;
Ponieważ nie znasz z góry całkowitego rozmiaru elementu, nie możesz użyć tablicy C. Zaleca się ich unikanie, więc powinieneś przepisać swój program tak, aby używał wektora:
#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++;
}
}
Oto bardziej idiomatyczna wersja twojego kodu, używająca wektora elementu dla każdego miejsca:
#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));
}
}
Podobne pytania
Nowe pytania
c++
C ++ to język programowania ogólnego przeznaczenia. Pierwotnie został zaprojektowany jako rozszerzenie C i ma podobną składnię, ale teraz jest to zupełnie inny język. Użyj tego tagu w przypadku pytań dotyczących kodu (który ma zostać) skompilowany za pomocą kompilatora C ++. Użyj znacznika specyficznego dla wersji w przypadku pytań związanych z określoną wersją standardu [C ++ 11], [C ++ 14], [C ++ 17], [C ++ 20] lub [C ++ 23] itp. .