Combining Random Gen/ Text file

What I am trying to figure out how to combine the text file I created and the Random Gen/Array



#include <iostream>
#include <iomanip>
#include <string>
#include <ctime>
#include<fstream>

using namespace std;

void menu();
string createFile();
void displayNumTotalAverage(string);
void displaySortedNums();
void SearchNum();
void displayLargestNum();
void appendRandomNum(string);
void exit();
void CreateFile();
void printFunc(int[]);
void fillFunc(int[]);

int main()
{
menu();
string FileName;
//createFile();
//makeRandomNum();

system("pause");
return 0;
}
void menu()
{
int choice;
string FileName;
do
{

//program output
cout << "** MENU **" << endl << endl;

cout << "Curret Data File: " << endl << endl;

cout << "(1) Select / create data file (.txt file extention will be added automaticly)" << endl;
cout << "(2) Display all numbers, total and average" << endl;
cout << "(3) Display all numbers sorted" << endl;
cout << "(4) search for a number and display how many times it occurs" << endl;
cout << "(5) display the largest number" << endl;
cout << "(6) Append a random number(s)" << endl;
cout << "(7) Exit the program" << endl << endl;

//user input
cout << "Menu Choice: ";
cin >> choice;

while (choice > 7 || choice < 1)
{
cout << "Menu Choice: ";
cin >> choice;
}

switch (choice)
{
case 1:
cout << "Choice 1";
createFile();
break;

case 2:
cout << "Choice 2";
displayNumTotalAverage(FileName.c_str());
break;

case 3:
cout << "Choice 3";
break;

case 4:
cout << "Choice 4";
break;

case 5:
cout << "Choice 5";
break;

case 6:
cout << "Choice 6";
appendRandomNum(FileName.c_str());
break;

}


} while (choice != 7);


}

string createFile()<------ Creates Text file
{
cout << "Create File - Option 1" << endl;
string FileName;
ifstream inFile;
cout << "Name of data file: ";
cin >> FileName;
FileName = "C:\\Users\Wizard\Libraries\Documents\Final Project" + FileName;
inFile.open(FileName + ".txt");
if (inFile)
{
cout << FileName;
}
else
cout << "File not found, creating file.";

system("PAUSE");
return FileName;
}

void displayNumTotalAverage(string FileName)
{
ifstream inFile;
cout << "Display Number Total Average - Option 2" << endl << endl << endl;
inFile.open("C:\\Users\Wizard\Libraries\Documents\Final Project" + FileName + ".txt");
int num;
int total;
cout << "Display Number Total Average function" << FileName;
double average;
bool containsNum = false;
inFile.open(FileName + ".txt");
if (inFile)
{
while (inFile >> num)
{
cout << num << endl;
}
inFile.close();
}
else
{
cout << "Error opening file" << FileName << "." << endl;
}

system("PAUSE");
return;
}

void displaySortedNums()
{
cout << "I AM THE displaySortedNums Function - Option 3" << endl;
{
int i, j, flag = 1; // set flag to 1 to start first pass
int temp; // holding variable
int numLength = num.length();
for (i = 1; (i <= numLength) && flag; i++)
{
flag = 0;
for (j = 0; j < (numLength - 1); j++)
{
if (num[j + 1] > num[j]) // ascending order simply changes to <
{
temp = num[j]; // swap elements
num[j] = num[j + 1];
num[j + 1] = temp;
flag = 1; // indicates that a swap occurred.
}
}
}
return; //arrays are passed to functions by address; nothing is returned
}
system("PAUSE");
return;
}

void searchNum()
{
int search;

cout << " I am the searchNum function - option 4" << endl;

cout << "Search Number: ";
cin >> search;

system("PAUSE");
return;
}

void displayLargestNum()
{
cout << "I am the displayLargestNum Function - option 5" << endl;
system("PAUSE");
return;

}

void appendRandomNum(string FileName)
{
cout << "i am in the appendRandomNum function - option 6" << endl;
int num = 0;
int count = 0;
ofstream outFile;
outFile.open(FileName + ".txt", ios::app);
cout << "How many random numbers: ";
cin >> count;
for (int i = 0; i < count; i++)
outFile << rand() % 10 << endl;
outFile.close();
cout << endl << "Number(s) Added" << endl << endl;

system("PAUSE");
return;
}

void exit()
{
cout << " I am the exit function - option 7" << endl;
system("PAUSE");
return;
}

void CreateFile()<---Creates Random Gen/ Array
{
int random[50]; //Random Numbers

srand((unsigned)time(NULL));
fillFunc(random);
printFunc(random);



return;

}

void fillFunc(int arr[])
{
for (int i = 0; i < 50; i++)
{
arr[i] = 1 + rand() % 10;

}

}

void printFunc(int arr[])
{
ofstream fout("C:\\Users\Wizard\Libraries\Documents\Final Project");
if (fout.is_open()){
for (int i = 0; i < 50; i++)
{
fout << arr[i] << std::endl;
}
}
}
Last edited on
Topic archived. No new replies allowed.