Write your question here.
Hey everyone. I have a final project due for my C++ class - the assignment reads as follows : Here is what the program needs to do:
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.
_____________________________________________________________________-
I am only trying to do parts 1 and 2 for now, I was struggling with passing a filename to a function as an argument but I think I may have mastered this, here is the code I have so far:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
|
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int menu();
string createFile();
void displayNumTotalAverage(string);
int main(int choice, string FN)
{
string FN;
cout << "** MENU **" << endl << endl;
cout << "Current Data File: " << FN << endl << endl;
menu();
displayNumTotalAverage(FN);
if (choice == 1){
cout << "Choice 1";
createFile();
}
else
{
if (choice == 2)
{
cout << "Choice 2: Display all numbers, total, and average";
displayNumTotalAverage(FN);
}
else
{
if (choice == 3)
{
cout << "Choice 3";
}
else{
if (choice == 4){
cout << "Choice 4";
}
else
{
if (choice == 5){
cout << "Choice 5";
}
else
{
if (choice == 6){
cout << "Choice 6";
}
else
{
exit(1);
}
system("PAUSE");
return 0;
}
int menu()
{
int choice;
cout << endl << "(1) Select / create data file (.txt file extension will be added automatically)\n"
<< "(2) Display all numbers, total, and average\n"
<< "(3) Display all numbers sorted\n"
<< "(4) Search for a number and display how many times it occurs\n"
<< "(5) Display the largest number\n"
<< "(6) Append a random number(s)\n"
<< "(7) Exit the program\n\n";
cout << "Menu Choice: ";
cin >> choice;
while (choice < 1 || choice > 7)
{
cout << "Menu Choice: ";
cin >> choice;
}
cout << "you have choosen option" << choice;
system("PAUSE");
return choice;
}
string createFile()
{
cout << "I AM THE CREATE / SELECT FILE OPTION" << endl;
ifstream inFile;
cout << "Name of data file: ";
cin >> FN;
inFile.open(FN + ".txt");
if (inFile){
cout << "File successfully found" << endl;
return FN;
}
else
{
cout << "File not found, creating file.";
inFile.open(FN);
}
return FN;
}
void displayNumTotalAverage(string FN)
{
ifstream inFile;
int num;
int total;
cout << "In the displayNumTotalAverage function" << FN;
double average;
bool containsNum = false;
inFile.open(FN + ".txt");
if (inFile)
{
while (inFile >> num)
{
cout << num << endl;
}
inFile.close();
}
else
{
cout << "Error opening file" << FN << "." << endl;
}
}
|