Write a program which allows the user to read a list of numbers from a data file. If the user specifies a data file which does not currently exist, then the program will create it. Assume there will never be more than 50 numbers in the data file.
The program should present the user with a menu containing the following options:
Current data file being processed.
(1) Select / create data file (.txt file extension will be added automatically)
(2) Display all the numbers in the data file, along with their total and average
(3) Display all the numbers in the data file sorted from smallest to largest
(4) Search for a number in the data file and display how many times the number occurs in the file
(5) Display the largest number in the data file
(6) Append one or more random numbers to the data file
(7) Exit the program
Prompt the user to make a selection from the menu and use input validation to ensure the user makes a valid selection (1 - 7).
Use a switch statement to process each of the above options. Each case statement should call a function which carries out the required action.
When asking the user which data file they wish to process, ask the user to only enter the part of the filename preceding the .txt extension and automatically add the .txt extension for the user. Create an empty data file if the file does not already exist.
Options (2) - (6) should test whether a data file has been specified before calling the function. If a data file has not yet been specified, the option should not call the function and the display should be refreshed.
When a data file is first created it will be empty. The functions which process the data file will need to handle the special case when the file exists, but does not contain any numbers. To make the program easier to write, you may opt to ignore this special case and assume that the user will immediately add numbers to the file once it has been created.
When adding a number, append it to the end of the data file.
How would I do this? What I am stuck on is how do I generate the 50 random numbers? And how do I add, average, etc with them?
If someone could at least show me how to write the 1st part (creating a file and generating 50 random numbers) I think I will get it after that.
No one is going to make this program for you, you'll have to actually try to do it yourself. If you get stuck on something specific, such as "how do I sort a vector of numbers?", then ask it here. But don't ask "How do I do this entire thing?". Google is your friend, and so is the Reference section of this website. Start writing the code that you know how to do, then try to write the code that you aren't so sure on. When you encounter a problem, Google it and check the Reference section. If you can't figure it out, then ask here.
std::string file_mame ;
std::srand( std::time(nullptr) ) ; // seed the lcg
// See: http://en.cppreference.com/w/cpp/numeric/random/srand
// (1) Select / create data file (.txt file extension will be added automatically)
// get the file name
std::cout << "file name without extension: " ;
std::cin >> file_mame ;
file_mame += ".txt" ; // add extension
bool exists = std::ifstream(file_mame) ; // check if the file exists
// by checking if the stream is in a good state
// See: http://www.mochima.com/tutorials/fileIO.htmlif(exists) std::cout << "the file " << file_mame << " exists\n" ;
else // if not, create an empty file
if( std::ofstream(file_mame) ) std::cout << "created file " << file_mame << '\n' ;
else std::cout << "file " << file_mame << " could not be created\n" ;
// (6) Append one or more random numbers to the data file
std::ofstream file( file_mame, std::ios::app ) ; // open file for appending
if( !file ) std::cout << "file " << file_mame << " could not be opened\n" ;
else // append 50 pseudo random numbers to the file
for( int i = 0 ; i < 50 ; ++i ) file << std::rand() << '\n' ;