Question

In: Computer Science

General overview (In C++) In this program you will be reading sales information from a file...

General overview (In C++)

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 * 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:

prompt for the input file name with the prompt "Enter input file name"
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
    if store number or sales are invalid
         display an appropriate error message (see below)
   else
         output bar chart for this store to the output file
   end if
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 * equals 5,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 * characters where each * represents $5,000 in sales for that store. For each 5,000 in sales you output one *. You do not round up the sales, so sales of $16,000 and sales of $16,999 would both output 3 * characters.

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

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

Store  9: ***

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

Store yy: *******

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 * equals 5,000 dollars)
Store  1: **
Store  2: *****
Store  3: *******
Store  4: *****
Store  5: *

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.

In this lab. and some future labs, you will be creating an output file. There could be output to cout as well.

For tests where there is output written to an output file the contents of the output file will determine if you passed that test or not. For cases where you have written out to cout the tests will check the output sent to cout.

Failure to follow the requirements for lab lessons can result in deductions to your points, even if you pass the validation tests. Logic errors, where you are not actually implementing the correct behavior, can result in reductions even if the test cases happen to return valid answers. This will be true for this and all future lab lessons.

Expected output

There are eight tests. Each test will have a new set of input data. You must match, exactly, the expected output.Tests 2, 5, 6, and 7 check the output sent to cout. Tests 1, 3, 4, and 8 check the output sent to file saleschart.txt.

You will get yellow highlighted text when you run the tests if your output is not what is expected. This can be because you are not getting the correct result. It could also be because your formatting does not match what is required. The checking that zyBooks does is very exacting and you must match it exactly. More information about what the yellow highlighting means can be found in course "How to use zyBooks" - especially section "1.4 zyLab basics".

Finally, do not include a system("pause"); statement in your program. This will cause your verification steps to fail.

Note: that the system("pause"); command runs the pause command on the computer where the program is running. The pause command is a Windows command. Your program will be run on a server in the cloud. The cloud server may be running a different operating system (such as Linux).

Solutions

Expert Solution

ales1.txt

1 10000
2 25000
3 37000
4 29000
5 8000

_______________

#include <iostream>

#include <fstream>

using namespace std;

int main()

{

// defines an input stream for the data file

ifstream dataIn;

string filename;

// Defines an output stream for the data file

ofstream dataOut;

int id;

long long int sales;

cout<<"Enter the input filename :";

cin>>filename;

// Opening the input file

dataIn.open(filename.c_str());

// checking whether the file name is valid or not

if (dataIn.fail())

{

cout << "File \""<<filename<<"\" could not opened";

return 1;

}

else

{

// creating and Opening the output file

dataOut.open("saleschart.txt");

if(dataOut.fail())

{

cout<<"File \"saleschart.txt \" could not opened"<<endl;

dataIn.close();

return 0;

}

else

{

int i = 0;

while (dataIn >>id >>sales)

{

if(id<1 || id>99)

{

cout<<"The store number "<<id<<" is not valid"<<endl;

dataIn.close();

dataOut.close();

}

if (sales < 0)

{

cout << "** Sales value must not be negative **" << endl;

dataIn.close();

dataOut.close();

return 0;

}

if(i==0)

{

dataOut << "SALES BAR CHART" << endl;

dataOut << "(Each 'X' equals to 1,000 dollars)" << endl;

}

dataOut << "Store " << id << ":";

for (int j = 0; j < sales / 1000; j++)

{

dataOut << "X";

}

i++;

dataOut << endl;

}

// closing the input file

dataIn.close();

// closing the output file

dataOut.close();

}

  

}

return 0;

}

_____________________

Output(Console):

________________

OutputFile(saleschart.txt)


Related Solutions

Write a Python program that uses function(s) for writing to and reading from a file: a....
Write a Python program that uses function(s) for writing to and reading from a file: a. Random Number File Writer Function Write a function that writes a series of random numbers to a file called "random.txt". Each random number should be in the range of 1 through 500. The function should take an argument that tells it how many random numbers to write to the file. b. Random Number File Reader Function Write another function that reads the random numbers...
In this assignment, you shall create a complete C++ program that will read from a file,...
In this assignment, you shall create a complete C++ program that will read from a file, "studentInfo.txt", the user ID for a student (first letter of their first name connected to their last name Next it will need to read three integer values that will represent the 3 exam scores the student got for the semester. Once the values are read and stored in descriptive variables it will then need to calculate a weighted course average for that student. Below...
IN BASIC C# If It Fits, It Ships! OVERVIEW In this program you will be calculating...
IN BASIC C# If It Fits, It Ships! OVERVIEW In this program you will be calculating the total amount the user will owe to ship a rectangular package based on the dimensions and we will add in insurance based on the prices of the items inside of the package.. INSTRUCTIONS: 1. Welcome the user and explain the purpose of the program. 2. In the Main, prompt the user for the width, length and height of their package. a. Each of...
Overview In this assignment, you will write a program to track monthly sales data for a...
Overview In this assignment, you will write a program to track monthly sales data for a small company. Input Input for this program will come from two different disk files. Your program will read from these files by explicitly opening them. The seller file (sellers.txt) consists of an unknown number of records, each representing one sale. Records consist of a last name, a first name, a seller ID, and a sales total. So, for example, the first few records in...
c++ Create a program that creates a sorted list from a data file. The program will...
c++ Create a program that creates a sorted list from a data file. The program will prompt the user for the name of the data file. Create a class object called group that contains a First Name, Last Name, and Age. Your main() function should declare an array of up to 20 group objects, and load each line from the input file into an object in the array. The group class should have the following private data elements: first name...
write a program in c++ that opens a file, that will be given to you and...
write a program in c++ that opens a file, that will be given to you and you will read each record. Each record is for an employee and contains First name, Last Name hours worked and hourly wage. Example; John Smith 40.3 13.78 the 40.3 is the hours worked. the 13.78 is the hourly rate. Details: the name of the file is EmployeeNameTime.txt Calculate the gross pay. If over 40 hours in the week then give them time and a...
Write a C++ program that reads a string from a text file and determines if the...
Write a C++ program that reads a string from a text file and determines if the string is a palindrome or not using stacks and queue
*****For C++ Program***** Overview For this assignment, write a program that uses functions to simulate a...
*****For C++ Program***** Overview For this assignment, write a program that uses functions to simulate a game of Craps. Craps is a game of chance where a player (the shooter) will roll 2 six-sided dice. The sum of the dice will determine whether the player (and anyone that has placed a bet) wins immediately, loses immediately, or if the game continues. If the sum of the first roll of the dice (known as the come-out roll) is equal to 7...
This class should include .cpp file, .h file and driver.cpp (using the language c++)! Overview of...
This class should include .cpp file, .h file and driver.cpp (using the language c++)! Overview of complex Class The complex class presents the complex number X+Yi, where X and Y are real numbers and i^2 is -1. Typically, X is called a real part and Y is an imaginary part of the complex number. For instance, complex(4.0, 3.0) means 4.0+3.0i. The complex class you will design should have the following features. Constructor Only one constructor with default value for Real...
JAVA make sure file name is FirstnameLastname_02_CS1Calculator. Thank you! Overview This program implements a simple, interactive...
JAVA make sure file name is FirstnameLastname_02_CS1Calculator. Thank you! Overview This program implements a simple, interactive calculator. Major topics writing a program from scratch multiple methods testing In this document program logic & program structure (class, methods, variables) input / output assumptions and limitations documentation & style suggested schedule cover letter discussion questions grading, submission, and due date additional notes challenges sample output test plan Program Logic The program prompts the user to select a type of math problem (addition,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT