Protected Word fi Home I sat Design Layou. Rede ences Mainas Review vew Telme wh
ID: 3804019 • Letter: P
Question
Protected Word fi Home I sat Design Layou. Rede ences Mainas Review vew Telme what you de you need to edit tssster to stay default ktneter and trurtor thar ch of the class dara bers to pecified value. Remember that quantityOnHand should be negati Nacreise 2 Modify the lient program to test your constractors .g. initial itemi mith defa etor and th the een trauren TASK 4: INPUTAOUTPUT R FUNCTIONS 1. Add a member fiuscri ta class In ren yltem to display the data members an the standard outpat device. The d criptios and quantity on land should be displayed co two valdy lan to tost th ber functio Show led tesulls here 2. Add a ember function (r:adInwortoryIton to class Ir.r:n cryIt:n to read duce fron file) and the data members. Dat the input stream has Emput stre rhe fallawime firmat Show expected results hExplanation / Answer
Hi, using the >> operator will drop spaces and read it one word at a time, so the 1st word of the 1st line in the file will be read into 'name' and the the 2nd word into 'quantity'. You can try reading the file one line at a time instead, and use the stringstream class to convert the 2nd line (quantity) into integer.
In the header file, declare the method as :-
public:
void readInventoryItem();
In the cpp file, first include stringstream
#include<stringstream>
using namespace std;
(You may also need to include <string> in case Visual Studio throws a linker error)
then, define your method as:-
void InventoryItem::readInventoryItem()
{
ifstream myfile("Text.txt"); //open the file for reading
string line; //each line from the file will be read into this string
int lineNo=1; //keep track of line numbers, as line No 2 has the int quantity
while(getline(myfile,line)) //read the file one line at a time until EOF, each line from the file will be read into the string 'line'
{
if(lineNo==1) //the line being read is the description
description=line; //assign the line read to the data member 'description' of the calling object
else if(lineNo==2) //the line being read is the quantity
{
stringstream str(line); //str is an object of the class stringstream to which we are passing the read line
str>>QuantityOnHand; //as QuantityOnHand is an int, the stringstream object str will convert 'line' to int and then
} //the >> operator will send it into the data member QuantityOnHand of the calling object
lineNo++; //prepare to read the next line
}
}
Hope this helps!
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.