D
Danny Anderson
Hola-
I didn't get any responses on a previous post, so I am trying to reword my
problem and post compile-able code that exhibits the behavior I am
describing.
On the second iteration of the loop below, the file opened is the default
(which in this case is ".csv", which makes a 'hidden' file for Linux
folk). The step that prompts for a file name step is skipped completely.
Why? Is this a cin issue or does it have something to do with the
filestream?
As always, comments and suggestions appreciated, Danny
//---start code
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
using namespace std;
//---function prototypes
void pause(string s);
bool user_is_not_finished();
string get_filename(string filetype);
int main()
{
string foo;
ofstream dataOut;
do
{
//---open report file for writing
dataOut.open(get_filename("report").c_str()); if(!dataOut)
{cout << "ERROR: couldn't open output file..."; return 1;}
else
{cout << "ready to start...\n" << endl;}
//---get information for report
cout << "Enter some text: ";
getline(cin, foo);
dataOut << foo << endl;
dataOut.close();
}while(user_is_not_finished() );
pause("end of program...");
return 0;
}
}
//---function definitions
void pause(string s)
{
char c;
cout << endl << s << "...enter a key to continue: "; cin >> c;
}
}
bool user_is_not_finished()
{
char user_choice;
bool is_not_finished;
cout << endl <<"[Q]uit program or enter another [r]eport? "; cin >>
user_choice;
while(user_choice!='Q'&& user_choice!='q'&& user_choice!='r'&&
user_choice!='R')
{
//---keep prompting until a valid answer is encountered cout <<
"[Q]uit program or enter another [r]eport? "; cin >> user_choice;
}
if(user_choice=='r'|| user_choice=='R')
{
is_not_finished=true;
}
else
{
is_not_finished=false;
}
}
return is_not_finished;
}
}
/* get_filename(string filetype)
* purpose: get the name of the file to be opened *
* input: a string that is either "report" or "material list". other
strings * can be valid as long as they are defined in the function body
* output: a string that can be sent to the a fstream.open command (after
* being converted to a c_str
*/
string get_filename(string filetype)
{
string filename;
string dummy;
if(filetype=="report")
{
cout << "Output file? [bldg_report] ";
getline(cin, filename);
// if(filename.empty())
// {filename="bldg_report.csv";}
//else
{filename+=".csv";}
}
else if(filetype=="material list")
{
cout << "Material List Filename? [dfi_master_mtl.txt] ";
getline(cin, filename);
if(filename.empty())
{filename="dfi_master_mtl.txt";}
}
return filename;
}
I didn't get any responses on a previous post, so I am trying to reword my
problem and post compile-able code that exhibits the behavior I am
describing.
On the second iteration of the loop below, the file opened is the default
(which in this case is ".csv", which makes a 'hidden' file for Linux
folk). The step that prompts for a file name step is skipped completely.
Why? Is this a cin issue or does it have something to do with the
filestream?
As always, comments and suggestions appreciated, Danny
//---start code
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
using namespace std;
//---function prototypes
void pause(string s);
bool user_is_not_finished();
string get_filename(string filetype);
int main()
{
string foo;
ofstream dataOut;
do
{
//---open report file for writing
dataOut.open(get_filename("report").c_str()); if(!dataOut)
{cout << "ERROR: couldn't open output file..."; return 1;}
else
{cout << "ready to start...\n" << endl;}
//---get information for report
cout << "Enter some text: ";
getline(cin, foo);
dataOut << foo << endl;
dataOut.close();
}while(user_is_not_finished() );
pause("end of program...");
return 0;
}
}
//---function definitions
void pause(string s)
{
char c;
cout << endl << s << "...enter a key to continue: "; cin >> c;
}
}
bool user_is_not_finished()
{
char user_choice;
bool is_not_finished;
cout << endl <<"[Q]uit program or enter another [r]eport? "; cin >>
user_choice;
while(user_choice!='Q'&& user_choice!='q'&& user_choice!='r'&&
user_choice!='R')
{
//---keep prompting until a valid answer is encountered cout <<
"[Q]uit program or enter another [r]eport? "; cin >> user_choice;
}
if(user_choice=='r'|| user_choice=='R')
{
is_not_finished=true;
}
else
{
is_not_finished=false;
}
}
return is_not_finished;
}
}
/* get_filename(string filetype)
* purpose: get the name of the file to be opened *
* input: a string that is either "report" or "material list". other
strings * can be valid as long as they are defined in the function body
* output: a string that can be sent to the a fstream.open command (after
* being converted to a c_str
*/
string get_filename(string filetype)
{
string filename;
string dummy;
if(filetype=="report")
{
cout << "Output file? [bldg_report] ";
getline(cin, filename);
// if(filename.empty())
// {filename="bldg_report.csv";}
//else
{filename+=".csv";}
}
else if(filetype=="material list")
{
cout << "Material List Filename? [dfi_master_mtl.txt] ";
getline(cin, filename);
if(filename.empty())
{filename="dfi_master_mtl.txt";}
}
return filename;
}