有人可以帮我“结构”吗? [英] Can someone help me with "struct"?

查看:76
本文介绍了有人可以帮我“结构”吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的教授要求我们这样做:



编写并运行一个构建名为part_rec的结构的程序。每个part_rec结构将是名为Robot_parts []的数据库(结构数组)中的记录。

每个part_rec结构由一个名为part_num的整数,一个名为part_name的字符串,一个名为part_quantity的整数和一个名为

的part_cost组成。 。一个名为void display()的函数通过指针获取整个Robot_parts []数组,并在函数中显示此信息。

确保在函数中显示具有良好列标题和对齐的信息。



在main()中,将Robot_parts []初始化为:

part_rec Robot_parts [] = {{7789,QTI,4, 12.95},

{1654,bolt,4,0.34},

{6931,nut,4,0.25}};

一旦在main()中初始化,调用display()函数并将指针地址传递给Robot_parts []作为其参数。



当我编译并运行时程序,没有结果



  #include   <   iostream  >  
#include < 字符串 >
#包括 < iomanip >

使用 命名空间标准

void display();

struct part_rec
{
int part_num;
string part_name;
int part_quantity;
double part_cost;
};

int main()
{
char CH;


part_rec Robot_parts [] = {
{ 7789 QTI 4 12 95 },
{ 1654 bolt 4 0 34 },
{ 6931 nut 4 0 25 }
};

cout<< 输入'e'退出;
cin>> ch;


return 0 ;
}

void display(part_rec * Robot_parts [])
{
int i;

for (i = 0 ; i< 3; i ++)
cout<< setw( 5 )<< Robot_parts [i] - > part_num;
cout<< setw( 7 )<< Robot_parts [i] - > part_name;
cout<< setw( 5 )<< Robot_parts [i] - > part_quantity;
cout<< setw( 10 )<< Robot_parts [i] - > part_cost;

return ;
}





我的尝试:



N / A

解决方案

那是因为你实际上没有调用你的显示/输出例程。也许你应该改变这个



 part_rec Robot_parts [] = {
{7789,QTI,4,12.95},
{1654,bolt,4,0.34},
{6931,nut,4,0.25}
};

cout<<输入'e'退出;
cin>> ch;





to



 part_rec Robot_parts [] = {
{7789,QTI,4,12.95},
{1654,bolt,4,0.34},
{6931 ,坚果,4,0.25}
};

显示(Robot_parts);

cout<<输入'e'退出;
cin>> ch;





[edit]



这个: -



 void display(part_rec * Robot_parts [])
{
int i;

for(i = 0; i< 3; i ++)
cout<< setw(5)<< Robot_parts [i] - > part_num;
cout<< setw(7)<<< Robot_parts [i] - > part_name;
cout<< setw(5)<<< Robot_parts [i] - > part_quantity;
cout<< setw(10)<< Robot_parts [i] - > part_cost;

返回;
}





会'更好'/?更正确的



 void display(part_rec * Robot_parts [])
{
for(int i = 0; i< 3; i ++)
{
cout<< setw(5)<<< Robot_parts [i] - > part_num;
cout<< setw(7)<<< Robot_parts [i] - > part_name;
cout<< setw(5)<<< Robot_parts [i] - > part_quantity;
cout<< setw(10)<< Robot_parts [i] - > part_cost;
}
返回;
}





而不是硬编码3作为数组中元素的数量,你可能(制作你的程序)未来安全)



  int  numberOfParts =  sizeof (Robot_parts)/  sizeof (Robot_parts [ 0 ]); 





然后使用它就像



  for  int  i =  0 ; i< numberOfParts; i ++)







或将数组中的元素数传递给显示器( ...)功能



[/ edit]







这样做



 int numberOfParts = sizeof(Robot_parts)/ sizeof (Robot_parts [0]); 





i很好,但是,我不认为它可以在显示器内完成(...)所以,这意味着你做这样的事情



 int numberOfParts = sizeof(Robot_parts)/ sizeof(Robot_parts [0]); 
显示(Robot_parts,numberOfParts);





并将display()的声明更改为



 void display(part_rec * Robot_parts [],int numberOfParts)





[/ EDIT2]


My professor requires us to do:

Write and run a program that builds a struct called "part_rec". Each "part_rec" struct will be a record in a database (struct array) called Robot_parts[ ].
Each "part_rec" struct is composed of an integer called "part_num", a string called "part_name", an integer called "part_quantity" and a double called
"part_cost". A function called void display() takes the entire Robot_parts[ ] array in by pointer and displays this information in the function.
Make sure to display this information with good column headings and alignment in the function.

In main(), initialize Robot_parts[ ] as:
part_rec Robot_parts[ ] = {{ 7789, "QTI", 4, 12.95},
{1654, "bolt", 4, 0.34},
{6931, "nut", 4, 0.25}};
Once initialized in main(), call the display() function and pass a pointer address to Robot_parts[ ] as its argument.

As I compile&run the program, there are no results

#include <iostream>
#include <string>
#include<iomanip>

using namespace std;
 
void display();

struct part_rec
{
   	int part_num;
  	string part_name;
  	int part_quantity;
  	double part_cost;
};

int main()
{
	char ch;
	
	 
    part_rec Robot_parts[ ] = {
                              {7789, "QTI", 4, 12.95},
                              {1654, "bolt", 4, 0.34},
                              {6931, "nut", 4, 0.25} 
                                                    };
                                                
    cout<<" enter 'e' to exit";
    cin>>ch;
    
    
    return 0;
}

void display(part_rec *Robot_parts[])
{
     int i; 
     
     for(i = 0; i<3; i++)
           cout<<setw(5)<<Robot_parts[i]->part_num;
           cout<<setw(7)<<Robot_parts[i]->part_name;
           cout<<setw(5)<<Robot_parts[i]->part_quantity;
           cout<<setw(10)<<Robot_parts[i]->part_cost;
           
    return;
}



What I have tried:

N/A

解决方案

thats because you dont actually call your display/output routine. Perhaps you should change this

part_rec Robot_parts[ ] = {
{7789, "QTI", 4, 12.95},
{1654, "bolt", 4, 0.34},
{6931, "nut", 4, 0.25} 
};

cout<<" enter 'e' to exit";
cin>>ch;



to

part_rec Robot_parts[ ] = {
{7789, "QTI", 4, 12.95},
{1654, "bolt", 4, 0.34},
{6931, "nut", 4, 0.25} 
};

display(Robot_parts);

cout<<" enter 'e' to exit";
cin>>ch;



[edit]

this :-

void display(part_rec *Robot_parts[])
{
     int i; 
     
     for(i = 0; i<3; i++)
           cout<<setw(5)<<Robot_parts[i]->part_num;
           cout<<setw(7)<<Robot_parts[i]->part_name;
           cout<<setw(5)<<Robot_parts[i]->part_quantity;
           cout<<setw(10)<<Robot_parts[i]->part_cost;
           
    return;
}



would be 'nicer' / ? more correct as

void display(part_rec *Robot_parts[])
{    
     for(int i = 0; i<3; i++)
     {
           cout<<setw(5)<<Robot_parts[i]->part_num;
           cout<<setw(7)<<Robot_parts[i]->part_name;
           cout<<setw(5)<<Robot_parts[i]->part_quantity;
           cout<<setw(10)<<Robot_parts[i]->part_cost;
     }     
    return;
}



and instead of hardcoding 3 as the number of elements in the array, you could probably (to make your program future safe)

int numberOfParts = sizeof(Robot_parts) / sizeof(Robot_parts[0]);



and then use that like

for(int i = 0; i<numberOfParts; i++)




or pass the number of elements in the array into the display(...) function

[/edit]

[edit 2 - warning bells in my head]

doing this

int numberOfParts = sizeof(Robot_parts) / sizeof(Robot_parts[0]);



is fine, but, I dont think it can be done from within display(...) so, that means you do something like this

int numberOfParts = sizeof(Robot_parts) / sizeof(Robot_parts[0]);
display(Robot_parts, numberOfParts);



and change the declaration of display() to

void display(part_rec *Robot_parts[], int numberOfParts)



[/edit2]


这篇关于有人可以帮我“结构”吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆