Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7542741
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T08:14:48+00:00 2026-05-30T08:14:48+00:00

This code runs fine but the for statement by the meant to read back

  • 0

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");
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T08:14:49+00:00Added an answer on May 30, 2026 at 8:14 am

    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;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Please check this code out it compiles and runs absolutely fine.. The question is
This code produces a FileNotFoundException, but ultimately runs without issue: void ReadXml() { XmlSerializer
This code runs on my local RoR/Windows 7 (64-bit): sql = ActiveRecord::Base.connection() last_pk =
I have a python script the runs this code: strpath = sudo svnadmin create
This code in a GUI application compiles and runs: procedure TForm1.Button1Click(Sender: TObject); begin Self
This piece of code compiles and runs as expected on GCC 3.x and 4.x:
I have some C++ source code with templates maybe like this - doxygen runs
I have some code that runs fine when I have a web proxy defined
The following code is crashing my application on startup. It compiles just fine but
This is a snippet of code from an array library I'm using. This runs

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.