Need help with this program.... 1. Include required libraries. 2. Create prototy
ID: 3837456 • Letter: N
Question
Need help with this program....
1. Include required libraries.
2. Create prototypes before main for the functions getInt, getDouble, getChar, and upper.
3. Declare variables for each of the fields the record has.
4. Declare a character variable for asking the user if he wants to enter another record.
5. Open a file named seqfile.txt for text output where you want to append to the end of the file.
6. If the file failed to open, display an error and exit with a failure exit status.
7. Create a loop that will run until the user chooses to not enter another record.
8. In the loop, prompt the user for each field in the record and validate the input.
9. In the loop, convert the department and employee ID to uppercase.
10. In the loop, output each field to the file separated by a comma, and followed by a newline.
11. After the loop, close the file and exit with a success exit status.
12. There should be four functions used, complete with prototypes:
- int getInt(string prompt, int min, int max): which prompts the user, gets a valid integer within the range of min and max, and returns that value
- double getDouble(string prompt, double min, double max): which prompts the user, gets a valid double within the range of min and max, and returns that value
- char getChar(string prompt, string allowed): which prompts the user, gets a valid character, validates that the character is within the allowed list, and returns that character
- string& upper(string& s): which converts the string passed in to uppercase and returns the uppercase version
SAMPLE RUN:
The file so far would look like this:
Explanation / Answer
PROGRAM CODE:
#include <iostream>
#include <cstdlib>
using namespace std;
int getInt(string , int , int );
double getDouble(string , double , double );
char getChar(string , string );
string& upper(string& );
int main() {
char choice = 'y';
string filename = "seqfile.txt";
ofstream output;
output.( filename.c_str(), ios::out | ios::app );
while(choice == 'Y' || choice == 'y')
{
string id, dept;
int month, day, year;
double hrs;
cout<<"Enter dept ID: ";
cin>>dept;
cout<<" Employee ID: ";
cin>>id;
month = getInt("Enter month: ", 1, 12);
day = getInt("Enter day: ", 1, 31);
year = getInt("Enter year: ",1970, 2017);
hrs = getDouble("Enter hours:", 0, 24);
id = upper(id);
dept = upper(dept);
output<<endl<<dept<<","<<id<<","<<month<<","<<day<<","<<year<<","<<hrs;
choice = getChar("Enter another record (Y/N)? ", "yYnN");
}
return 0;
}
int getInt(string prompt, int min, int max)
{
int value;
cout<<endl<<prompt;
cin>>value;
if(value>=min && value<=max)
return value;
else return 0;
}
double getDouble(string prompt, double min, double max)
{
double value;
cout<<endl<<prompt;
cin>>value;
if(value>=min && value<=max)
return value;
else return 0;
}
char getChar(string prompt, string allowed)
{
char ch;
cout<<endl<<prompt;
cin>>ch;
for(int i=0; i<allowed.length(); i++)
{
if(ch == allowed.at(i))
return ch;
}
return '';
}
string& upper(string& s)
{
for(int i=0; i<s.length(); i++)
{
s[i] = toupper(s.at(i));
}
return s;
}