I need a program to do the following: 1) Asks the user for the output filename.
ID: 3556408 • Letter: I
Question
I need a program to do the following:
1) Asks the user for the output filename.
2) Asks the user for how many records are to be created.
3) A function that accepts an integer and returns a string with any
one of 5 strings containing the name of the object. For example object number 3 might be
"Pen". Object 4 might be "Paper".
4)Another function that has no parameters and returns a random integer
number for choice. That choice number will be a number that helps choose which object
we are going to create.
5)Another function that has no parameters and returns a random
positive number from 0 to 999.99. These will be our coordinate numbers used for the x y
and z portions of the the file.
6) Program displays each created record once it is created.
Example output might look like this;
How many records are to be created? 3
Pen 2013.3 100.9 13.008
Paper 100.09 32.7 45.01
Computer 10.0 100.0 10.0
Thank you for whoever takes the time to go through this. I will give highest points possible. Use C++ and basic codes as well. Introductory course.
Explanation / Answer
#include <iostream>
#include <stdlib>
#include <time>
#include <fstream>
using namespace std;
// A function that accepts an integer and returns a string with anyone of 5 strings containing the name of the object
string objName (int n)
{
string result;
switch (n)
{
case 1:
result = "Desk";
break;
case 2:
result = "Lamp";
break;
case 3:
result = "Pen";
break;
case 4:
result = "Paper";
break;
case 5:
result = "Computer";
break;
}
return result;
}
// Another function that has no parameters and returns a random integer number for choice.
int numGenerator()
{
int num = 0;
num = rand() % 5 + 1;
return num;
}
// Another function that has no parameters and returns a random positive number from 0 to 999.99
double genPos ()
{
double num;
num = (double) rand()/RAND_MAX;
return (num * 100);
}
int main()
{
string filename;
int rec;
ofstream file;
double x, y, z;
string obj;
// Asks the user for the output filename
cout << "Please enter filename" << endl;
getline(cin, filename);
// Asks the user for how many records are to be created
cout << "Please enter how many records are to be created" << endl;
cin >> rec;
// seed
srand (time(NULL));
// Program displays each created record once it is created
file.open(filename.c_str());
for (int i = 0; i < rec; i++)
{
obj = objName(numGenerator());
x = genPos();
y = genPos();
z = genPos();
cout << obj << " " << x << " " << y << " " << z << endl;
file << obj << " " << x << " " << y << " " << z << " ";
}
file.close();
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.