Write C++ statements to: 1) Declare a struct named partType that has two members
ID: 3832396 • Letter: W
Question
Write C++ statements to:
1) Declare a struct named partType that has two members: name (holds the name of a part) and code (holds a whole number).
2) Declare two variables of type partType.
3) Prompt the user to enter the name and code of the first item and store them into the corresponding variable.
4) Prompt the user to enter the name and code of the second item and store them into the corresponding variable.
5) Compare the items by code and display an output like the one shown in the examples below:
Example 1:
Enter name and code of item1: bolt 22222
Enter name and code of item2: nut 11111
bolt goes after nut in the inventory
Explanation / Answer
1) Declare a struct named partType that has two members: name (holds the name of a part) and code (holds a whole number).
typedef struct partType
{
string name;
int code;
} partType;
2) Declare two variables of type partType.
partType item1;
partType item2;
3) Prompt the user to enter the name and code of the first item and store them into the corresponding variable.
cout << "Enter name and code of item1: ";
cin >> item1.name >> item1.code;
4) Prompt the user to enter the name and code of the second item and store them into the corresponding variable.
cout << "Enter name and code of item2: ";
cin >> item2.name >> item2.code;
5) Compare the items by code and display an output like the one shown in the examples below:
Example 1:
Enter name and code of item1: bolt 22222
Enter name and code of item2: nut 11111
bolt goes after nut in the inventory
if (item1.code >= item2.code)
{
cout << item1.name << " goes after " << item2.name << " in the inventory" << endl;
}
else
{
cout << item2.name << " goes after " << item1.name << " in the inventory" << endl;
}
A full working version of program is also given for your reference along with sample execution output !
File: Items.cpp
#include <iostream>
using namespace std;
// 1) Declartion of struct partType
typedef struct partType
{
string name;
int code;
} partType;
int main()
{
// 2) Declare two variables of type partType
partType item1;
partType item2;
// 3.1) Prompt user to enter name and code of first item
cout << "Enter name and code of item1: ";
// 3.2) Store entered values in first item
cin >> item1.name >> item1.code;
// 4.1) Prompt user to enter name and code of second item
cout << "Enter name and code of item2: ";
// 4.2) Store entered values in second item
cin >> item2.name >> item2.code;
// 5) Compare code of two items and accordingly display expected output
if (item1.code >= item2.code)
{
cout << item1.name << " goes after " << item2.name << " in the inventory" << endl;
}
else
{
cout << item2.name << " goes after " << item1.name << " in the inventory" << endl;
}
return 0;
}
Compilation:
$ g++ Items.cpp
Sample Execution Output:
$ ./a.out
Enter name and code of item1: bolt 22222
Enter name and code of item2: nut 11111
bolt goes after nut in the inventory
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.