Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In C++,Write a program that allows the user to create files of random data. They

ID: 3836242 • Letter: I

Question

In C++,Write a program that allows the user to create files of random data. They should be offered a menu of possibilities:

The first 3 options should ask for appropriate bounds. Your program should make sure they are in order before passing them to your random value generation function. (The overloaded ones we did in class, remember? — place these in a 'random' library.)

Then, ask how many random values to generate and what file to place them in. Assuming the file opened correctly, proceed to generate all requested random values.

This fourth option should ask for a file of given names and a file of family names. If both those files open successfully, ask how many names each person should have (given; given and family; given, middle, and family; etc.). (Hint: middle names are typically the same as most given names, but may occasionally be a family name. My estimate is 80% of middle names were once given names and the remaining 20% were probably family names.)

Once you know how many random names to create and what file to store them in (and that file opened successfully), you'll need to pick names from either the given name file or the family name file (or both) and store them to the output file. To do this quickly, it might help to pre-process the given/family name files to count how many of each you have and figure out where each name starts in the file (an index file, perhaps?). Once you know how many names, you can pick a random one easily (a random integer between 0and the number of names-1).

The menu should continue to loop until the quit option is chosen. Options should be choosable by either the number or the capitalized word. (For example, to quit, they should be able to enter 4, Q, or q.)

Explanation / Answer

#include <iostream>
#include <ctime>
#include <cstdlib>
#include <fstream>
#include <string>
using namespace std;

int random(int limit)
{
    //generates random integer within limit and returns it.
    int temp;
    while(true)
    {
        temp = rand(); //generating random integer.
        if(temp <= limit)
            return temp;
    }
}

void whole()
{
    int i, limit, nos;
    char fname[30];
    ofstream out;
    cout<<" How many values you want to generate: ";
    cin>>nos;
    cout<<" What will be the largest no. generated: ";
    cin>>limit;
    cin.ignore();//To clear buffer to read strings properly.
    cout<<" Enter file name to write nos.: ";
    cin>>fname;
    cin.ignore();
    out.open(fname);
    for(i = 1; i <= nos; i++)
        out<<random(limit)<<endl;//Writing random integers into file.
    out.close();
    cout<<" File created. ";
}

void decimal()
{
    int i, nos;
    float limit;
    char fname[30];
    ofstream out;
    cout<<" How many values you want to generate: ";
    cin>>nos;
    cout<<" What will be the largest no. generated: ";
    cin>>limit;
    cin.ignore();
    cout<<" Enter file name to write nos.: ";
    cin>>fname;
    cin.ignore();
    out.open(fname);
    for(i = 0; i <= nos; i++)
    {
        out<<(float)random(limit * 50) / 50.0<<endl; //writing float decimals into file.
    }
    out.close();
    cout<<" File created. ";
}

void character()
{
    int i, nos, limit = 127;//127 is the highest ascii value
    char fname[30];
    ofstream out;
    cout<<" How many values you want to generate: ";
    cin>>nos;
    cin.ignore();
    cout<<" Enter file name to write characters: ";
    cin>>fname;
    cin.ignore();
    out.open(fname);
    for(i = 0; i <= nos; i++)
    {
        out<<(char)(random(limit))<<endl;//Generating integers and converting them to characters to write into file.
    }
    out.close();
    cout<<" File created. ";
}

void names()
{
    int nos, i, n, j, k, nnos;
    char gname[30], fname[30], output[30];
    string name;
    string gnames[1000], fnames[1000], mnames[1000];
    cout<<" Name of given names file: ";
    cin>>gname;
    cin.ignore();
    cout<<" Name of family names file: ";
    cin>>fname;
    cin.ignore();
    cout<<" Name of output file: ";
    cin>>output;
    cin.ignore();
    cout<<" How many names to create: ";
    cin>>nos;
    ifstream gin, fin;
    ofstream out;
    gin.open(gname);
    fin.open(fname);
    i = 0;
    while(getline(gin, name))//Reading line by line from file
    {
        gnames[i] = mnames[i] = name;//mnames stores both gnames and fnames
        i++;
    }
    k = i;
    j = 0;
    while(getline(fin, name))
    {
        fnames[j++] = mnames[i++] = name;
    }
    gin.close();
    fin.close();
    out.open(output);
    while(true){
    cout<<" How many names each person should have (1/2/3): ";
    cin>>nnos;
    //creating names according to users choice
    if(nnos == 1)
    {
        for(n = 1; n <= nos; n++)
            //gnames indices are selected by random function within gnames limit
            out<<gnames[random(k - 1)]<<endl;
        break;
    }
    else if(nnos == 2)
    {
        for(n = 1; n <= nos; n++)
            out<<gnames[random(k - 1)]<<" "<<fnames[random(j -1)]<<endl;
        break;
    }
    else if(nnos == 3)
    {
        for(n = 1; n <= nos; n++)
            out<<gnames[random(k - 1)]<<" "<<mnames[random(i - 1)]<<" "<<fnames[random(j -1)]<<endl;
        break;
    }
    else
        cout<<" Invalid choice. Try again. ";
    }
    out.close();
    cout<<" File created. ";
}

int main()
{
    time_t ti;
    //Initializing random no. generator
    srand((unsigned)time(&ti));
    char ch; //To read user choice
    while(true)
    {
        cout<<" 1. create random Whole number data file. ";
        cout<<"2. create random Decimal number data file. ";
        cout<<"3. create random Character data file. ";
        cout<<"4. create a random name data file. ";
        cout<<"5. Quit program. ";
        cout<<" Enter choice: ";
        cin>>ch;
        cin.ignore();
        if(ch == '1' || ch == 'W' || ch == 'w')
        {
            whole();
        }
        else if(ch == '2' || ch == 'D' || ch == 'd')
        {
            decimal();
        }
        else if(ch == '3' || ch == 'C' || ch == 'c')
        {
            character();
        }
        else if(ch == '4')
        {
            names();
        }
        else if(ch == '5' || ch == 'Q' || ch == 'q')
        {
            cout<<" Thank you for using. ";
            return 0;
        }
        else
            cout<<" Invalid choice. Try again. ";
    }
}