刚刚接触 C++ 编程。 我必须做的练习:“在一家商店中,有 500 种产品由 ID、产品名称、品牌、货架(0 到 34)标识。使用局部变量实现两个功能: - 加载数据; - 找到大部分产品所在的货架。” 这就是我所做的。 (我用了 10 个而不是 500 个)。
#include <iostream>
#include <string>
using namespace std;
class product {
public:
string ID, prod_name, brand;
float price;
int shelf;
void data_load();
int freq_shelf();
};
product a[10];
void data_load() {
int i;
cout << "Recording ten products" << endl;
for(i=0; i<10; i++) {
cout << "Id product "<<i<<":" << endl;
cin >> a[i].ID;
cout << "Name product "<<i<<":" << endl;
cin >> a[i].prod_name;
cout << "Brand product "<<i<<":" << endl;
cin >> a[i].brand;
cout << "Price product "<<i<<":" << endl;
cin >> a[i].price;
cout << "Shelf (shelves 0-34)"<<i<<":" << endl;
cin >> a[i].shelf;
if (a[i].shelf<0 || a[i].shelf>34) {
cout << "I said 0 a 34" << endl;
}
}
}
int freq_shelf() {
int coor[2]; //first element should be the number of the shelf, second one how many times that shelf is found
int i, j, k, c;
int t[10]={0,0,0,0,0,0,0,0,0,0}; //kth-element indicates the number of presences of the ith-element
for (k=0; k<10; k++) {
for (i=0; i<10; i++) {
for (j=0; i<10; j++) {
if (a[i].shelf==a[j].shelf) {
t[k]++;
}
}
}
}
int max=t[0];
for (c=1; c<10; c++)
{
if (t[c]>max)
{
max=t[c]; //c should be the number of the shelf, max how many times that shelf is found
coor[2]=coor[c, max];
cout << "The most frequent shelf is "<<coor[1]<<". It is found "<<coor[2]<<" times." << endl;
return 0;
}
}
}
int main()
{
void data_load();
int freq_shelf();
system("PAUSE");
return 0;
}
使用 Microsoft Visual C++ 2010,我在调试和编译时没有遇到任何错误,但命令提示符直接转到“按任意键继续”。 任何人都可以解释为什么? 感谢您的耐心等待和帮助。
最佳答案
您声明了一个函数而不是调用它;
改变
int main()
{
void data_load();
到
int main()
{
data_load();
关于c++ - 基本 C++ 练习(命令提示符不显示任何 cout。没有编译器错误),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25999618/