1. email1-algorithm.txt Input: prompt for both the input and output file names,
ID: 3770604 • Letter: 1
Question
1. email1-algorithm.txt Input: prompt for both the input and output file names, allowing blank for the "default" name. Output: output to the console the input and output file names selected, converting any blanks to their corresponding default names. There is NO file input or output in this version. Here's a general algorithm for defaulted console input: iFileName is the input file name dFileName is the default file name oFileName is the output file name blankFlag is true if either user input for file names is blank (user pressed ENTER key) dFileName = "fileContainingEmails.txt" blankFlag = false output a prompt to the console using dFileName input iFileName if iFileName is blank, then blankFlag = true if blankFlag is true, then iFileName = dFileName if blankFlag is true, then dFileName = "copyPasteMyEmails.txt", else dFileName = iFileName output a prompt to the console using dFileName input oFileName output label for the input file name, and iFileName output label for the output file name, and oFileName output a prompt to press ENTER key to continue input a character See examples below and write the complete algorithm. 2. email1.cpp The program should prompt the user for the input and output filenames, and output the user's choices, and that's all. The prompts should include the default names for the files, per the term project writeup. A prompt should look something like this: "Enter input filename [default: fileContainingEmails.txt]: ". Remember that if the user selects the default for the input filename, then the default for the output filename is "copyPasteMyEmails.txt". Otherwise, if the user types something for the input filename, and does not accept the default, then the default for the output filename is the same as what the user entered for the input filename. Do NOT type fileContainingEmails.txt or copyPasteMyEmails.txt more than once in your code. It is bad programming practice to repeat values in code. Instead, store otherwise repeated values in variables, and refer to those variables instead. Here are some guidelines: If you name the program's source file somthing other than what is specified, you are not doing this right. If you spell or case the default filenames wrong, you are not doing this right. If the user does not see exactly two prompts every time, you are not doing this right. If the character sequence "fileContainingEmails.txt" is in your code more than once, you are not doing this right. If the character sequence "copyPasteMyEmails.txt" in your code more than once, you are not doing this right. If your prompts do not include the names of the defaults, you are not doing this right. Do not open the input or output files. Program should work like this: Output a prompt for the input filename, that also shows the default name, fileContainingEmails.txt. If user simply presses ENTER key, then the input filename should be set by your program to the default name, fileContainingEmails.txt. Whether user types a filename and press ENTER key in response to the prompt, or types nothing and press ENTER key, user should get another prompt -- one for the output filename. User should ALWAYS get exactly 2 prompts. If user typed a name for the input filename, then whatever user typed also becomes the default for the output filename, instead of copyPasteMyEmails.txt. Otherwise, if user just pressed ENTER key for the input filename, the default for the output filename is to be copyPasteMyEmails.txt. If user typed something and press ENTER key for the output filename, then what user typed should become the output filename. Otherwise, if user just pressed ENTER key, the output filename should be set by your program to the default, which will either be copyPasteMyEmails.txt or what user typed for the input filename. Make sure that you understand what is meant by a "default". If an input value has a default value, then (1) the prompt should say what is the default value, and (2) if the user presses ENTER key without typing anything first, then the default value should be substituted for the blank that got input. That means the programmer (you) has to test the input value with an if-statement. If the input value is blank, then replace it with the default value. Example. Your program's console I/O should look something like this, with user input in blue (excluding your identifying information): Test Case #1: Enter input filename [default: fileContainingEmails.txt]: Enter output filename [default: copyPasteMyEmails.txt]: Input file: fileContainingEmails.txt Output file: copyPasteMyEmails.txt press any key to continue: ...or... Test Case #2: Enter input filename [default: fileContainingEmails.txt]: x.txt Enter output filename [default: x.txt]: Input file: x.txt Output file: x.txt press any key to continue: ...or... Test Case #3: Enter input filename [default: fileContainingEmails.txt]: Enter output filename [default: copyPasteMyEmails.txt]: y.txt Input file: fileContainingEmails.txt Output file: y.txt press any key to continue: ...or... Test Case #4 Enter input filename [default: fileContainingEmails.txt]: x.txt Enter output filename [default: x.txt]: y.txt Input file: x.txt Output file: y.txt press any key to continue:
Explanation / Answer
Below is the C++ code with comments
#include <iostream>
#include <cstdio>
#include <cstdlib>
using namespace std;
int main()
{
/* Declarations */
string inFile, outFile, defOutFile;
/* Inputting input filename */
cout<< "Enter input filename [default: fileContainingEmails.txt]: " ;
cin>>inFile;
/* Below code checks if an ENTER was pressed */
string::iterator it = inFile.begin();
if(inFile[*it] == ' ')
{
defOutFile = "copyPasteMyEmails.txt";
}
else
{
defOutFile = inFile;
}
/* Inputting output filename */
cout << "Enter output filename [default: " << defOutFile << " ]: ";
cin >> outFile;
/* Below code checks if an ENTER was pressed */
it = outFile.begin();
if(outFile[*it] == ' ')
{
outFile = defOutFile;
}
/* Printing the results */
cout << "Input file: " << inFile;
cout << "Output file: " << outFile;
cout << "press any key to continue: ...or... ";
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.