Question

C++ requirements The store number must be of type unsigned int. The sales value must be...

C++ requirements

The store number must be of type unsigned int. The sales value must be of of type long long int.

Your program must properly check for end of file. See the section Reading in files below and also see your Gaddis text book for details on reading in file and checking for end of file.

Your program must properly open and close all files.

Failure to follow the C++ requirements could reduce the points received from passing the tests.

General overview

In this program you will be reading sales information from a file and writing out a bar chart for each of the stores. The bar charts will be created by writing out a sequence of X characters.

You will have to create input files for your program. You can use a text editor such as Notepad or Notepad++ to create this. There may also be an editor in your IDE that you can use. You can use the TextEdit program on macOS but you will need to change the format to Make Plain Text. You will not be uploading these text files to zyBooks/zyLabs. The submit tests will be using their own files. Make sure that some of your files contain a newline at the end of the last line of text in the file. You can do this by hitting the enter key after the last number of the last line in the file. This will match the type of file usually created by programs. This is the type of file that is used for the zyBooks tests. See the description below on reading in files.

Reading in files

When reading data from a file you need to make sure you are checking for end of file properly.

See Demo of file input and output of data in this unit for an explanation of how to properly check for end of file.

The general method shown in the Gaddis text book is:

ifstream inputFile;
inputFile.open("input.txt");
int num;
if (inputFile)
{
   // the file opened successfully
   while (inputFile >> num)
   {
      // process the num value
      cout << num << endl;
   }
   inputFile.close();
}
else
{
   cout << "The file could not be opened" << endl;
}

If you want to read in two values with every read you can simply replace inputFile >> num with something like inputFile >> num1 >> num2 as shown here:

   while (inputFile >> num1 >> num2)
   {
      // process the num1 and num2 values
      cout << num1 << " " << num2 <<  endl;
   }

Text files are more complicated that they seem. Different operating systems handle text files differently. On Windows lines of text end with \r followed by \n. On Unix (or Linux) the lines end with just \n. On old Macs lines ended with \r but now use the Unix convention. The use of the >> operator takes care of these line ending issues for you.

But it is still more complicated than that. Most text files have every line ending with either \r \n (for Windows) or \n (for Unix/Linux and MacOS) but it is also possible to create a text file where the last line does NOT have the line ending characters. The use of the following code will work for all line endings even when the last line of text input does not end with any line endings.

if (inputFile >> num1 >> num2)
{
   // executes only if the read worked
}

or

while (inputFile >> num1 >> num2)
{
   // executes while the read works
}

There are other ways to test for end of file but you have to make sure your code will work for all of the cases discussed above. It is STRONGLY recommended that you use the process outlined above.

General overview (continued)

Your program will read in a file name from cin. It will then open the input file.

Your program must also open an output file called saleschart.txt. You will write the bar char headings and data to this file.

Your program needs to have the following general flow:

read in the file name
open the input file for this file name
display a message if the input file does not open and quit your program 
open the output file ("saleschart.txt")
display a message if the output file does not open, close the input file,  and quit your program
while (readFile into store number and sales for store
    display an error if the store number or sales are invalid
    if the data is good output the bar char to the output file
end while
close the input and output files

The processing loop will read the input data and process it until it gets and end of file indication from the file read operation

Assuming you have read in valid data AND this is the first sales data being processed your program should output some headings to the output file before processing the data. The headings are as follows:

SALES BAR CHART
(Each X equals 1,000 dollars)

Note: Your program must not output the headings to the output file if all of the data in the input file is invalid, or if there is not any valid data in the input file.

You need to come up with a way of keeping track if this is the first valid read or not. .

Assuming you have valid data the processing will consist displaying the output

Once the loop has completed you need to close the input file.

If the input file could not be opened your program should output an error message to cout. Assume the file we are reading in from is called sales.txt, and the file does not exist. The error message written to cout is:

File "sales.txt" could not be opened 

The store number is of type unsigned int. Your program must verify that the store number is in the range 1 to 99 (inclusive). If the store number is invalid display the following message:

If the store number is less than 1 or greater than 99 you need to output the following message to cout:

The store number xx is not valid 

Where xx is the store number.

If the sales data is read in as a long long int. If the sales value is less than 0 you need to output the following message to cout:

The sales value for store xx is negative

Where xx is the store number.

Don't forget to close both files, if they were opened.

Write the bar chart information to the file.

You will be outputting a string of X (upper case X) characters where each X represents $1,000 in sales for that store. For each 1,000 in sales you output one X. You do not round up the sales, so sales of $6,000 and sales of $6,999 would both output 6 X characters.

You will output the sales bar chart to the output file.

Assuming a store number of 9 and sales of $6,999. the display function will write the following to the output file:

Store  9: XXXXXX

Note that the store width is 2 characters, so the output is:

Store yy: XXXXXXX

The yy has a width of 2 even if the store number is 1 through 9.

The format of the input file

The data in the input file is in the order store number followed by the store sales. There will be zero or more of these input pairs in the file.

Here is the contents of a sample input text file:

1 10000
2 25000
3 37000
4 29000
5 8000

Sample runs

Here is an example run. Assume the following input being read in from cin:

sales.txt

Assume that the content of the file sales.txt are as follows:

1 10000
2 25000
3 37000
4 29000
5 8000

The output (to file saleschart.txt) for this input would be:

SALES BAR CHART
(Each X equals 1,000 dollars)
Store  1: XXXXXXXXXX
Store  2: XXXXXXXXXXXXXXXXXXXXXXXXX
Store  3: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Store  4: XXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Store  5: XXXXXXXX

You are reading from an input file and you are writing to an output file. Make sure you close both files after you are finished using them. You must do this in your program, you cannot just let the operating system close the files for you.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

If you have any doubts, please give me comment...

#include<iostream>

#include<string>

#include<fstream>

using namespace std;

int main(){

string in_filename;

cout<<"Enter filename: ";

cin>>in_filename;

ifstream in;

in.open(in_filename.c_str());

if(in.fail()){

cout<<"File \""<<in_filename<<"\" could not be opened."<<endl;

return -1;

}

ofstream out;

out.open("saleschart.txt");

if(out.fail()){

cout<<"Output file does not open!"<<endl;

in.close();

return -1;

}

out<<"SALES BAR CHART"<<endl;

out<<"(Each X equals 1,000 dollars)"<<endl;

int store_no, sales;

while(in>>store_no>>sales){

if(store_no>0 && store_no<100){

if(sales<0)

cout<<"The sales value for store "<<store_no<<" is negative"<<endl;

else{

out<<"Store "<<store_no<<": ";

sales /= 1000;

while(sales>0){

out<<"X";

sales--;

}

}

out<<endl;

}else{

cout<<"The store number "<<store_no<<" is not valid";

}

}

in.close();

out.close();

return 0;

}

nagaraju@nagaraju-Vostro-3550:~/Desktop/CHEGG/November/04112018$ g++ saleChart.cpp nagaraju@nagaraju-Vostro-3550:~/Desktop/CHEGG/November/04112018$ /a.out Enter filename: sales.txt nagaraju@nagaraju-Vostro-3550:~/Desktop/CHEGG/November/04112018$ more ./saleschart.txt SALES BAR CHART (Each X equals 1,000 dollars) Store 1: XXXXXXXXXX Store 2: Store 3: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX Store 5: XXXXXXXX

Add a comment
Know the answer?
Add Answer to:
C++ requirements The store number must be of type unsigned int. The sales value must be...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C++ problem where should I do overflow part? in this code do not write a new...

    C++ problem where should I do overflow part? in this code do not write a new code for me please /////////////////// // this program read two number from the user // and display the sum of the number #include <iostream> #include <string> using namespace std; const int MAX_DIGITS = 10; //10 digits void input_number(char num[MAX_DIGITS]); void output_number(char num[MAX_DIGITS]); void add(char num1[MAX_DIGITS], char num2[MAX_DIGITS], char result[MAX_DIGITS], int &base); int main() { // declare the array = {'0'} char num1[MAX_DIGITS] ={'0'}; char...

  • Program 2 #include #include using namespace std; int main() { int total = 0; int num...

    Program 2 #include #include using namespace std; int main() { int total = 0; int num = 0; ifstream inputFile; inputFile.open("inFile.txt"); while(!inputFile.eof()) { // until we have reached the end of the file inputFile >> num; total += num; } inputFile.close(); cout << "The total value is " << total << "." << endl; Lab Questions: We should start by activating IO-exceptions. Do so using the same method in Step 3 of the Program 1 assignment above, except that instead...

  • C ++ Implement cat command The purpose of this assignment is to provide practice using the...

    C ++ Implement cat command The purpose of this assignment is to provide practice using the system calls we discussed for working with files on a UNIX system. You will be writing a basic implementation of the cat command using C++. Description As you should recall, the cat command takes a list of files as command line arguments. It then opens each file in turn, writing each file’s entire contents to standard output in the order they were supplied. You...

  • I couldn't find where to comment. But it has to be written in C++!!!!! Task-1: Rational...

    I couldn't find where to comment. But it has to be written in C++!!!!! Task-1: Rational fractions are of the form a / b, in which a and b are integers and ! ≠ 0. In this exercise, by ‘‘fractions’’ we mean rational fractions. Suppose a / b and c / d are fractions. Arithmetic operations and relational operations on fractions are defined by the following rules: Arithmetic Operations: a/b + c/d = (ad + bc)/bd a/b – c/d =...

  • Write a C# code for a Sales Calculator: Note: You cannot use the foreach loop in...

    Write a C# code for a Sales Calculator: Note: You cannot use the foreach loop in any part of this program! sales.txt file: 1189.55, 1207.00, 1348.27, 1456.88, 1198.34, 1128.55, 1172.37, 1498.55, 1163.29 The application should have controls described as follows: • A button that reads Get Sales. If the user clicks this button, the application should call a method named ProcesSalesFile. ProcessSalesFile Method: accepts no arguments and does not return anything The ProcessSalesFile Method will do the following: o Open...

  • Write a C# code for a Sales Calculator: Note: You cannot use the foreach loop in any part of this...

    Write a C# code for a Sales Calculator: Note: You cannot use the foreach loop in any part of this program! sales.txt file: 1189.55, 1207.00, 1348.27, 1456.88, 1198.34, 1128.55, 1172.37, 1498.55, 1163.29 The application should have controls described as follows: • A button that reads Get Sales. If the user clicks this button, the application should call a method named ProcesSalesFile. ProcessSalesFile Method: accepts no arguments and does not return anything The ProcessSalesFile Method will do the following: o Open...

  • Theres an error on The bolded line, what am I doing wrong? #include <iostream> //just for...

    Theres an error on The bolded line, what am I doing wrong? #include <iostream> //just for writing and reading from console #include <fstream> //this one to deal with files, read and write to text files using namespace std; int main() { //first thing we need to read the data in the text file //let's assume we have the reading in a text file called data.txt //so, we need a stream input variable to hold this data ifstream infile; //now we...

  • I need help debugging this C++ prgram. What Am i doing wrong? //******************************************************** // This program...

    I need help debugging this C++ prgram. What Am i doing wrong? //******************************************************** // This program reads two input files whose lines are //ordered by a key data field. This program should merge //these two files, writing an output file that contains //all lines from both files ordered by the same key field. // //********************************************************* #include <iostream> #include<string> #include<fstream> //prototype void mergeTwoFiles (ifstream&,ifstream&, ofstream&); using namespace std; int main() {string inFile1,inFile2,outFile; // input and output files ifstream in1; ifstream in2;...

  • Write c program. Do part 4 and 5 CH-12 TEXT FILES SE 12-3 Create a text file named grade.txt that you type yourself by your Each record in grade.txt should have the following format: by Columns...

    Write c program. Do part 4 and 5 CH-12 TEXT FILES SE 12-3 Create a text file named grade.txt that you type yourself by your Each record in grade.txt should have the following format: by Columns 1-4 6-8 number of a student A grade out of 100 EYERCISE 12-4 Write a program that will read the identification numbers of students and their letter grades (A, B, C, D, or F) from the keyboard and store them in a text file...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT