Upon successful completion of this workshop, you will have demonstrated the abil
ID: 3624365 • Letter: U
Question
Upon successful completion of this workshop, you will have demonstrated the ability to design and code a class for a simple object to write code that includes robust user input validation, and to write code that includes formatted program output. Design and code a class named Product that holds information about a single-atom. Place your class definition in a file named Product.h Include the following member functions in your design: bool accept()- prompts for and accepts from standard input an integer holding the product identified number. a string of no more than eight(8) characters holding the product code, a string of no more than twenty (50) characters holding the full name of the product and a floating-point value holding the product price. If any input is invalid, your function rejects that input and requests fresh data void display () const - displays the product information on standard output. Design and code a main program that accepts information for up to 10 product elements and displays the product information in tabular format.Explanation / Answer
Program has 3 files:
1. file main.cpp
#include <iostream>
using namespace std;
#include "Product.h"
#include <stdio.h>
#include <string.h>
void main()
{
Product p[10];
int i, j;
//Nhap du lieu vao
cout << "Product Information" << endl;
cout << "==============================" << endl;
i=0;
while(1)
{
bool check;
check = p[i].accept();
i++;
if (check == false) break;
}
//In ra man hinh
printf(" ID No. Code Name Price ");
printf("---------------------------------------------------------------- ");
for (j=0; j<i-1; j++)
{
p[j].display();
}
cout << endl;
//Dung man hinh
system("pause");
}
2. file Product.cpp
#include <iostream>
using namespace std;
#include "Product.h"
#include <stdio.h>
#include <string.h>
bool Product::accept()
{
char so[10], ma[15], ten[50];
float gia;
//Nhap number
while(1)
{
cout << "Enter product identified number: ";
cin >> so;
fflush(stdin);
int ktra;
ktra = checkso(so);
if (ktra ==0) goto thoat;
num = ktra;
if (ktra >0) break;
cout << "Ban da nhap sai. Vui long nhap lai! ";
}
//Nhap product code
cout << "Enter product code : ";
cin >> ma;
fflush(stdin);
strncpy (code, ma, 8);
code[8]='';
//Nhap product name
cout << "Enter product name : ";
cin >> ten;
fflush(stdin);
strncpy (name, ten, 20);
name[20]='';
//Nhap product price
while(1)
{
cout << "Enter product price : ";
cin >> gia;
fflush(stdin);
price = gia;
if (gia>0) break;
cout << "Ban da nhap sai. Vui long nhap lai! ";
}
return true;
thoat:return false;
}
int checkso(char so[])
{
int i, n, ra;
n=strlen(so);
ra=0;
for(i=0; i<n; i++)
{
if (so[i]>47 && so[i]<59)
{
ra=ra*10;
ra=ra+so[i]-48;
}
else
{
ra = -1;
break;
}
}
return ra;
}
void Product::display()
{
printf("%d %s %s %.1f ", num, code, name, price);
}
3. file Product.h
class Product
{
private:
int num;
char code[9], name[21];
float price;
public:
bool accept();
void display();
};
int checkso(char so[]);
#include <iostream>
using namespace std;
#include "Product.h"
#include <stdio.h>
#include <string.h>
void main()
{
Product p[10];
int i, j;
//Nhap du lieu vao
cout << "Product Information" << endl;
cout << "==============================" << endl;
i=0;
while(1)
{
bool check;
check = p[i].accept();
i++;
if (check == false) break;
}
//In ra man hinh
printf(" ID No. Code Name Price ");
printf("---------------------------------------------------------------- ");
for (j=0; j<i-1; j++)
{
p[j].display();
}
cout << endl;
//Dung man hinh
system("pause");
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.