Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Create a file using notepad called transaction.dat and store the following: 22 1

ID: 674478 • Letter: C

Question

Create a file using notepad called transaction.dat and store the following:

22

10.98

Save this file in the same folder where your Source.cpp file is located.


place a copy of your running code and screenshot in a word document and upload to blackboard by the end fo class.

// This program will read in the quantity of a particular item and its price.

// It will then print out the total price.

// The input will come from a data file and the output will go to

// an output file.

// PLACE YOUR NAME HERE

#include <fstream>

#include <iomanip>

using namespace std;

int main()

{

ifstream dataIn; // defines an input stream for a data file

ofstream dataOut; // defines an output stream for an output file

int quantity; // contains the amount of items purchased

float itemPrice; // contains the price of each item

float totalBill; // contains the total bill, i.e. the price of all items

dataIn.open("transaction.dat"); // This opens the file.

dataOut.open("bill.out");

// Fill in the appropriate code in the blank below

____________ << setprecision(2) << fixed << showpoint; // formatted output

// Fill in the input statement that brings in the

// quantity and price of the item

// Fill in the assignment statement that determines the total bill.

// Fill in the output statement that prints the total bill, with a label,

// to an output file

return 0;

}

Explanation / Answer

#include <stdio.h>
#include <conio.h>
#include<math.h>
#include "valid.c"struct purch_detail
{
char desc[20];
int unit_price;
int qty;
};

void display();
void entryData();
void readFile();
void getName(char *str);
void getWriteFileName(char *);
void getReadFileName(char *);
void getzeroInteger(char *_chr, int *_value);

int count=0;
int total_cost=0;
int total_amt=0;
char option;
char filename [14];
FILE *fp;
struct purch_detail *pd_ptr, *tmptr;

void main()
{
clrscr();
pd_ptr = (struct purch_detail *) malloc(sizeof(struct purch_detail) * 10);
tmptr = pd_ptr;

printf(" if you create data file type 'y' for yes and 'n' for no.");
scanf("%c", &option);
fflush(NULL);

if( option == 'y' || option == 'Y')
{ printf(" You can generate bill of maximum 10 item. ");
getWriteFileName(filename);
display();
}
elseif( option == 'n' || option == 'N')
{ getReadFileName(filename);
readFile();
}


getch();
}

void entryData()
{
while(1)
{
printf(" Enter name of the item : ");
getName(tmptr->desc);

getPosInteger("Enter item price : ", &tmptr->unit_price);
getzeroInteger("Enter purched quantity : ", &tmptr->qty);

if(tmptr->qty == 0)
break;

count ++;
if (count >= 10)
break;

tmptr++;
}
}

void display()
{
tmptr = pd_ptr;

fprintf(fp," Item Unit Price Quantity Total cost ");
fprintf(fp," --------------------------------------------");

printf(" Item Unit Price Quantity Total Cost");
printf(" -------------------------------------------------");
while(1)
{
total_cost = tmptr->unit_price * tmptr->qty ;
fprintf(fp, " %-20s %4d %4d %5d", tmptr->desc, tmptr->unit_price, tmptr->qty, total_cost);
printf(" %-20s %4d %4d %5d", tmptr->desc, tmptr->unit_price, tmptr->qty, total_cost);
//printf("");
total_amt += total_cost;

count--;
if ( count <= 0)
break;
tmptr++;
}

fprintf(fp, " -----------------------------------------");
fprintf(fp, " Total Amount : %5d", total_amt);
printf(" ----------------------------------------------");
printf(" Total Amount : %5d", total_amt);
}

void getWriteFileName(char *fname)
{
printf("Enter Output File name maxium 14 character : ");
scanf("%s", fname);

if( ( fp = fopen(fname, "wt") ) == NULL )
{ fclose(fp);
printf(" Invalid file name entered.");
}
else
entryData();
}

void getReadFileName(char *fname)
{
printf("Enter Input File name maxium 14 character : ");
scanf("%s", fname);

if( ( fp = fopen(fname, "r+t") ) == NULL )
{ fclose(fp);
printf(" Invalid file name entered.");
}
}

void readFile()
{
char chr;
while( feof(fp) == 0)
{
chr = getc(fp);
printf("%c",chr);
}
}
void getName(char *str)
{
int count1=0;
char *getstr;
char readch='';
int errorflag = 0;

fflush(NULL);
getstr = str;

while(1)
{
scanf("%c", &readch);
if( readch == ' ')
break;

*getstr = readch;

count1++;
getstr++;
if( count1 >= 20 )
break;
}
fflush(NULL);

if(errorflag)
getName(str);

*getstr='';
}

void getzeroInteger(char *chr1, int *value1)
{
float tempval =1;
int ival1=0;

printf(" %s", chr1);
scaint1 = scanf("%f", &tempval);
fflush(NULL);


if ( scaint1 != 0 )
{
ival1 = tempval;

if ( tempval < 0 )
{
printf("%s", " Enter value greater then or equal to zero.");
getzeroInteger(chr1, value1);
}

elseif ( ival1 != tempval)
{
printf("%s", " Error. Truncated the digit after the percision. Enter integeronly.");
getzeroInteger(chr1, value1);
}
elseif( ! ( (tempval >= -32768 ) && (tempval <= 32767 ) ) )
{
printf("%s", " out of range . The range of value -32768 to 327678 only.");
getzeroInteger(chr1, value1);
}
else
{
*value1 = (int) tempval;
}

}
else
{
printf(" %s","Wrong entry. Please Enter integers only.");
getzeroInteger(chr1, value1);
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote