Question

Right now, program pushes all data to screen in three loops need to prompt user for...

Right now, program pushes all data to screen in three loops

need to prompt user for three text files, and need to push data from each loop into those three text files.

#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
double random(double minprice, double maxprice);
string printRandomString(int n);
bool coinToss();
int clicks();
int runme();
int createfiles();

struct things{
    //things();
    int id;
    string name;
    string category;
    double price;
    bool two_day_shipping;
    int numclicked;
} things;

int main()
{

    for(int z=0; z<3; z++)
   {
    createfiles();
    }
}

int createfiles()
{

bool two_day_shipping;

srand(time(NULL));
cout << left << setw(7) << "id"
            << left << setw(15) << "name"
            << right << setw(15)<< "price"
            << "     "
            << right << setw(15)<< "category"
            << "     "
            << left << setw(15) << "2D ship"
            << left << setw(15) << "number of clicks" << endl;

for(int j=0; j<1000; j++)
    {
    string category;
    double price;
    int id;
    int click;
    int cat;
    cat = rand() % 6 +1;
    double minprice;
    double maxprice;
    cout << fixed << setprecision(2);
    id = j;
    int n;
    string name;
    switch (cat){

    case 1:
    category = "Electronics";
        minprice = 35;
        maxprice = 1500;
        price = random (minprice,maxprice);
        n= 5 + (rand()%4);
        name = printRandomString(n);
        two_day_shipping= coinToss();
        click = clicks();
        cout << left << setw(7) << id
            << left << setw(15) << name
            << right << setw(15)<< price
            << "     "
            << right << setw(15)<< category
            << "     "
            << left << setw(15) << two_day_shipping
            << left << setw(15) << click << endl;


break;

    case 2:
        category = "Clothing";
        minprice = 45;
        maxprice = 550;
        price = random (minprice,maxprice);
        n= 5 + (rand()%4);
        name = printRandomString(n);
        two_day_shipping= coinToss();
        click = clicks();
        cout << left << setw(7) << id
            << left << setw(15) << name
            << right << setw(15)<< price
            << "     "
            << right << setw(15)<< category
            << "     "
            << left << setw(15) << two_day_shipping
            << left << setw(15) << click << endl;
        break;

    case 3:
category = "Footwear";
        minprice = 99;
        maxprice = 399;
        price = random (minprice,maxprice);
        n= 5 + (rand()%4);
        name = printRandomString(n);
        two_day_shipping= coinToss();
        click = clicks();
        cout << left << setw(7) << id
            << left << setw(15) << name
            << right << setw(15)<< price
            << "     "
            << right << setw(15)<< category
            << "     "
            << left << setw(15) << two_day_shipping
            << left << setw(15) << click << endl;
        break;

    case 4:
category = "Books";
        minprice = 7;
        maxprice = 45;
        price = random (minprice,maxprice);
        n= 5 + (rand()%4);
        name = printRandomString(n);
        two_day_shipping= coinToss();
        click = clicks();
        cout << left << setw(7) << id
            << left << setw(15) << name
            << right << setw(15)<< price
            << "     "
            << right << setw(15)<< category
            << "     "
            << left << setw(15) << two_day_shipping
            << left << setw(15) << click << endl;
        break;

    case 5:
        category = "Appliances";
        minprice = 150;
        maxprice = 600;
        price = random (minprice,maxprice);
        n= 5 + (rand()%4);
        name = printRandomString(n);
        two_day_shipping= coinToss();
        click = clicks();
        cout << left << setw(7) << id
            << left << setw(15) << name
            << right << setw(15)<< price
            << "     "
            << right << setw(15)<< category
            << "     "
            << left << setw(15) << two_day_shipping
            << left << setw(15) << click << endl;
        break;

    case 6:
        category = "Apps";
        minprice =.5;
        maxprice = 4.99;
        price = random (minprice,maxprice);
        n= 5 + (rand()%4);
        name = printRandomString(n);
        two_day_shipping= coinToss();
        click = clicks();
        cout << left << setw(7) << id
            << left << setw(15) << name
            << right << setw(15)<< price
            << "     "
            << right << setw(15)<< category
            << "     "
            << left << setw(15) << two_day_shipping
            << left << setw(15) << click << endl;
        break;
    }
}
};


double random(double minprice, double maxprice)
{
    double price;
    price = ((double)rand()/RAND_MAX)* (maxprice-minprice)+minprice;
    return price;
}


string printRandomString(int n)
{
    const int MAX = 26;
    char alphabet[MAX] = { 'a', 'b', 'c', 'd', 'e', 'f', 'g',
                          'h', 'i', 'j', 'k', 'l', 'm', 'n',
                          'o', 'p', 'q', 'r', 's', 't', 'u',
                          'v', 'w', 'x', 'y', 'z' };

    string res = "";
    for (int i = 0; i < n; i++)
        res = res + alphabet[rand() % MAX];
    return res;
}

bool coinToss()
{
    return rand() % 2;
}


int clicks()
{
    int x;
    x = rand()%10000;
    return x;
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1


//I just commented whereever i modified to reach target
// Remaining code as you have written nothing was modified.....

#include <iostream> //For input, output funcitons...
#include <stdlib.h> //For rand function
#include <iomanip> //for setw function
#include <fstream> //for file related functions...
#include <string.h>
using namespace std;

double random(double minprice, double maxprice);
string printRandomString(int n);
bool coinToss();
int clicks();
int runme();
int createfiles(const char *);

struct things{
//things();
int id;
string name;
string category;
double price;
bool two_day_shipping;
int numclicked;
} things;

int main()
{
  
for(int z=0; z<3; z++)
{
    char filename[20];
    cout << "Enter txt file name (w/o .txt): ";
    cin >> filename ;
    strcat(filename,".txt");
createfiles(filename);
}
}

int createfiles(const char *filename)
{

ofstream fileout; //open file
fileout.open(filename); //convert open file to output stream....
bool two_day_shipping;

srand(time(NULL));
fileout << left << setw(7) << "id"
<< left << setw(15) << "name"
<< right << setw(15)<< "price"
<< " "
<< right << setw(15)<< "category"
<< " "
<< left << setw(15) << "2D ship"
<< left << setw(15) << "number of clicks" << endl;

for(int j=0; j<1000; j++)
{
string category;
double price;
int id;
int click;
int cat;
cat = rand() % 6 +1;
double minprice;
double maxprice;
fileout << fixed << setprecision(2);
id = j;
int n;
string name;
switch (cat){

case 1:
category = "Electronics";
minprice = 35;
maxprice = 1500;
price = random (minprice,maxprice);
n= 5 + (rand()%4);
name = printRandomString(n);
two_day_shipping= coinToss();
click = clicks();
fileout << left << setw(7) << id
<< left << setw(15) << name
<< right << setw(15)<< price
<< " "
<< right << setw(15)<< category
<< " "
<< left << setw(15) << two_day_shipping
<< left << setw(15) << click << endl;


break;

case 2:
category = "Clothing";
minprice = 45;
maxprice = 550;
price = random (minprice,maxprice);
n= 5 + (rand()%4);
name = printRandomString(n);
two_day_shipping= coinToss();
click = clicks();
fileout << left << setw(7) << id
<< left << setw(15) << name
<< right << setw(15)<< price
<< " "
<< right << setw(15)<< category
<< " "
<< left << setw(15) << two_day_shipping
<< left << setw(15) << click << endl;
break;

case 3:
category = "Footwear";
minprice = 99;
maxprice = 399;
price = random (minprice,maxprice);
n= 5 + (rand()%4);
name = printRandomString(n);
two_day_shipping= coinToss();
click = clicks();
fileout << left << setw(7) << id
<< left << setw(15) << name
<< right << setw(15)<< price
<< " "
<< right << setw(15)<< category
<< " "
<< left << setw(15) << two_day_shipping
<< left << setw(15) << click << endl;
break;

case 4:
category = "Books";
minprice = 7;
maxprice = 45;
price = random (minprice,maxprice);
n= 5 + (rand()%4);
name = printRandomString(n);
two_day_shipping= coinToss();
click = clicks();
fileout << left << setw(7) << id
<< left << setw(15) << name
<< right << setw(15)<< price
<< " "
<< right << setw(15)<< category
<< " "
<< left << setw(15) << two_day_shipping
<< left << setw(15) << click << endl;
break;

case 5:
category = "Appliances";
minprice = 150;
maxprice = 600;
price = random (minprice,maxprice);
n= 5 + (rand()%4);
name = printRandomString(n);
two_day_shipping= coinToss();
click = clicks();
fileout << left << setw(7) << id
<< left << setw(15) << name
<< right << setw(15)<< price
<< " "
<< right << setw(15)<< category
<< " "
<< left << setw(15) << two_day_shipping
<< left << setw(15) << click << endl;
break;

case 6:
category = "Apps";
minprice =.5;
maxprice = 4.99;
price = random (minprice,maxprice);
n= 5 + (rand()%4);
name = printRandomString(n);
two_day_shipping= coinToss();
click = clicks();
fileout << left << setw(7) << id
<< left << setw(15) << name
<< right << setw(15)<< price
<< " "
<< right << setw(15)<< category
<< " "
<< left << setw(15) << two_day_shipping
<< left << setw(15) << click << endl;
break;
}
}
};


double random(double minprice, double maxprice)
{
double price;
price = ((double)rand()/RAND_MAX)* (maxprice-minprice)+minprice;
return price;
}


string printRandomString(int n)
{
const int MAX = 26;
char alphabet[MAX] = { 'a', 'b', 'c', 'd', 'e', 'f', 'g',
'h', 'i', 'j', 'k', 'l', 'm', 'n',
'o', 'p', 'q', 'r', 's', 't', 'u',
'v', 'w', 'x', 'y', 'z' };

string res = "";
for (int i = 0; i < n; i++)
res = res + alphabet[rand() % MAX];
return res;
}

bool coinToss()
{
return rand() % 2;
}


int clicks()
{
int x;
x = rand()%10000;
return x;
}
OUTPUT

Add a comment
Know the answer?
Add Answer to:
Right now, program pushes all data to screen in three loops need to prompt user for...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 1. in this programe i want add some products to be shown after choosing choise (...

    1. in this programe i want add some products to be shown after choosing choise ( show all products ) before i add any products. let say 10 products have added to the program then when the user choose (show all products ) all the 10 products appear and the user going to choose one and that one must has its own name, price,and quantity after that the quantity will be reduce. 2. allow the user to buy products. ===========================================================...

  • The following is a sample inventory in C++, i want to ask the user to input a item number for removing from inventory. //CPP #include <iostream> #include <fstream> #include <cstdlib>...

    The following is a sample inventory in C++, i want to ask the user to input a item number for removing from inventory. //CPP #include <iostream> #include <fstream> #include <cstdlib> #include <iomanip> #define MAX 1000 using namespace std; //Function to Add a new inventory item to the data into the array in memory void addItem(string desc[],string idNum[], float prices[], int qty[],int &num) { cout<<"Enter the names:"; cin>>desc[num]; cout<<"Enter the item number:"; cin>>idNum[num]; cout<<"Enter the price of item:"; cin>>prices[num]; cout<<"Enter the...

  • my program wont run on my computer and im not understanding why. please help. #include<iostream> #include<iomanip>...

    my program wont run on my computer and im not understanding why. please help. #include<iostream> #include<iomanip> #include<string> #include "bookdata.h" #include "bookinfo.h" #include "invmenu.h" #include "reports.h" #include "cashier.h" using namespace std; const int ARRAYNUM = 20; BookData bookdata[ARRAYNUM]; int main () { bool userChoice = false; while(userChoice == false) { int userInput = 0; bool trueFalse = false; cout << "\n" << setw(45) << "Serendipity Booksellers\n" << setw(39) << "Main Menu\n\n" << "1.Cashier Module\n" << "2.Inventory Database Module\n" << "3.Report Module\n"...

  • Hello I need a small fix in my program. I need to display the youngest student...

    Hello I need a small fix in my program. I need to display the youngest student and the average age of all of the students. It is not working Thanks. #include <iostream> #include <iomanip> #include <fstream> #include <vector> #include <algorithm> using namespace std; struct Student { string firstName; char middleName; string lastName; char collegeCode; int locCode; int seqCode; int age; }; struct sort_by_age { inline bool operator() (const Student& s1, const Student& s2) { return (s1.age < s2.age); // sort...

  • c++ programming : everything is done, except when you enter ("a" ) in "F" option ,...

    c++ programming : everything is done, except when you enter ("a" ) in "F" option , it does not work. here is the program. #include <iostream> #include <string> #include <bits/stdc++.h> #include <iomanip> #include <fstream> using namespace std; #define MAX 1000 class Inventory { private: long itemId; string itemName; int numberOfItems; double buyingPrice; double sellingPrice; double storageFees; public: void setItemId(long id) { itemId = id; } long getItemId() { return itemId; } void setItemName(string name) { itemName = name; } string...

  • How can I make this compatible with older C++ compilers that DO NOT make use of...

    How can I make this compatible with older C++ compilers that DO NOT make use of stoi and to_string? //Booking system #include <iostream> #include <iomanip> #include <string> using namespace std; string welcome(); void print_seats(string flight[]); void populate_seats(); bool validate_flight(string a); bool validate_seat(string a, int b); bool validate_book(string a); void print_ticket(string passenger[], int i); string flights [5][52]; string passengers [4][250]; int main(){     string seat, flight, book = "y";     int int_flight, p = 0, j = 0;     int seat_number,...

  • Hello, I need to implement these small things in my C++ code. Thanks. 1- 10 characters...

    Hello, I need to implement these small things in my C++ code. Thanks. 1- 10 characters student first name, 10 characters middle name, 20 characters last name, 9 characters student ID, 3 characters age, in years (3 digits) 2- Open the input file. Check for successful open. If the open failed, display an error message and return with value 1. 3- Use a pointer array to manage all the created student variables.Assume that there will not be more than 99...

  • The following code is for Chapter 13 Programming Exercise 21. I'm not sure what all is...

    The following code is for Chapter 13 Programming Exercise 21. I'm not sure what all is wrong with the code written. I get errors about stockSym being private and some others after that.This was written in the Dev C++ software. Can someone help me figure out what is wrong with the code with notes of what was wrong to correct it? #include <cstdlib> #include <iostream> #include <iomanip> #include <fstream> #include <cassert> #include <string> using namespace std; template <class stockType> class...

  • I need a detailed pseudocode for this code in C ++. Thank you #include <iostream> #include...

    I need a detailed pseudocode for this code in C ++. Thank you #include <iostream> #include <string> #include <iomanip> using namespace std; struct Drink {    string name;    double cost;    int noOfDrinks; }; void displayMenu(Drink drinks[], int n); int main() {    const int size = 5;       Drink drinks[size] = { {"Cola", 0.65, 2},    {"Root Beer", 0.70, 1},    {"Grape Soda", 0.75, 5},    {"Lemon-Lime", 0.85, 20},    {"Water", 0.90, 20} };    cout <<...

  • i have two issues that i need help. 1- that in case three and two only...

    i have two issues that i need help. 1- that in case three and two only one line is being read from the file 2- my case 4 is not working correctly as it should as the output is only " Enter 1 for Trump, 2 for Warren:" #include <iostream> #include <cctype> // For the letter checking functions #include <fstream> // For file input #include <iomanip> // For setw #include <ctime> #include <cstdlib> // For exit and abs #include <errno.h>...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT