C++结构循环错误

问题描述:

这段代码运行良好,但是“意思是逐行读回结构的每行”注释代码的for语句不起作用,因为它应该是,它的意思是列出项目,价格为每个座位,项目每个座位的名单之前输出座位#,座位号的部分作品C++结构循环错误

编辑:错误是它表明项目中读出循环中未申报,造成这种不编译

line 64 'item' undeclared (first use this function)

`上面的字符串改为'代码高亮功能

计划使用开发 - 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

它做什么,它不应该? – spencercw 2012-02-24 20:16:52

+0

哦应该补充说:/ 它表示项目尚未申报尚未公布的主要使用。 – Arcticfoxx 2012-02-24 20:19:48

+0

哦,我的多脚神!看到你使用循环变量的全局变量后,我无法安静地沉默。我必须下车才能告诉你先生;我非常鄙视你的COBOLized伪C样本,我真的会这么做 – lurscher 2012-02-24 20:34:18

我通过您的代码行由行在Visual Studio中去。它现在有效。评论我的所有更改和原因。

使用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

函数'dash'稍后会用到,但是非常感谢你 – Arcticfoxx 2012-02-24 20:36:36

+0

哇我完全忘了'return 0;' 它在代码中,但我删除了大部分注释掉了部分,并且显然与它相关 – Arcticfoxx 2012-02-24 20:39:18

+1

另外,请停止使用Dev-C++!使用Visual C++ Express,或者如果您是学生,请从[http://www.dreamspark.com](http://www.dreamspark.com)免费获取完整版本。它需要一点时间才能习惯,但它在每一个方面都很出色。 – ssell 2012-02-24 20:42:27

移动ITM_TYPE item[N_ITEMS];你上面的第一for循环得到它的编译。 item在下一次循环运行时已超出范围,因此无法访问它。

你也永远不会初始化tItems这会导致问题。变化:

int tItems; 

到:

int tItems = 0; 
+0

'tItems + = N_ITEMS;'39行 不会抛出错误 – Arcticfoxx 2012-02-24 20:21:49

+0

@Arcticfoxx尽管你从未给它初始值, N_ITEMS'实际上是一个随机数。这是法律行为,这就是编译器接受它的原因,但它是未定义的。 – spencercw 2012-02-24 20:22:50

+0

好的,我会补充一点,然后尝试进入该习惯,谢谢。 – Arcticfoxx 2012-02-24 20:24:20

既然你不提前知道项目总规模,你不能使用C数组。避免他们是无论如何推荐,所以你应该重写你的程序使用矢量:

#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++; 
    } 
} 

这里是你的代码的更地道的版本,使用项目向量每个座位:

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