Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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

C++
#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
Posted
Updated 17-Apr-16 13:49pm
v2
Comments
Mohibur Rashid 17-Apr-16 18:20pm    
Describe your error message
Member 12466410 17-Apr-16 18:23pm    
I got no error message. The program worked but it did not display anything
Mohibur Rashid 17-Apr-16 18:27pm    
What is the exact message?
Member 12466410 17-Apr-16 18:34pm    
it displays " enter'e' to exit"
and that's it

1 solution

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)

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


and then use that like

C++
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]
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900