C++ Write a program to ask for Part number, part name, partclass, number in stoc
ID: 3850045 • Letter: C
Question
C++
Write a program to ask for Part number, part name, partclass, number in stock and unit price. The data should be read into a struct and passed to a function which writes it out to a data file. Part number and name will be strings, partclass is a character, number in stock is an integer and unit price is a double. Enclose your program in a do while which asks the user if he/she wants to enter more parts. Typical run:
Enter part information (part number, name, class, on hand balance and price):
P-14376 Widget B 120 34.95
More parts? Y or N
y
P-16543 Wodget C 80 15.75
More parts? Y or N
n
Explanation / Answer
C++ code:
#include <bits/stdc++.h>
using namespace std;
ofstream myoutfile ("output.txt");
struct info
{
string part_number;
string name;
char partclass;
int on_hand_balance;
double price;
};
void print(info current)
{
myoutfile << current.part_number << " " << current.name << " " << current.partclass ;
myoutfile << " " << current.on_hand_balance<< " " << current.price << endl;
}
int main()
{
cout << "Enter part information (part number, name, class, on hand balance and price):";
string part_number;
string name;
char partclass;
int on_hand_balance;
double price;
while(true)
{
cin >> part_number;
cin >> name;
cin >> partclass;
cin >> on_hand_balance;
cin >> price;
info current;
current.part_number = part_number;
current.name = name;
current.partclass = partclass;
current.on_hand_balance = on_hand_balance;
current.price = price;
print(current);
cout << "More parts? Y or N" <<endl;
string tmp;
cin >> tmp;
if(tmp == "Y" or tmp == "y")
{
}
else
{
break;
}
}
myoutfile.close();
return 0;
}
Sample Output:
Enter part information (part number, name, class, on hand balance and price):
P-14376 Widget B 120 34.95
More parts? Y or N
y
P-16543 Wodget C 80 15.75
More parts? Y or N
N
output.txt
P-14376 Widget B 120 34.95
P-16543 Wodget C 80 15.75
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.