473,378 Members | 1,417 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

help me understand this cin/fstream behavior

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;
}
Jul 22 '05 #1
5 1869
On Fri, 23 Jan 2004 19:57:21 -0500, "Danny Anderson" <bt*****@i.hate.spam> wrote:
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;}
Suggest formatting this more readable.

Place 'if' at the start of separate line.

//---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;
}
}
What's that second brace? This might be the problem.
//---function definitions
void pause(string s)
{
char c;
cout << endl << s << "...enter a key to continue: "; cin >> c;
}
}

What's that second brace? This might be the problem.
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;
}
}
What's that second brace? This might be the problem.
return is_not_finished;
}
}

What's that second brace? This might be the problem.


/* 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;
}


Jul 22 '05 #2

"Danny Anderson" <bt*****@i.hate.spam> wrote in message
news:pa****************************@i.hate.spam...
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;
}


You have other issues to address first:

f:\danny.cpp(45) : error C2143: syntax error : missing ';' before '}'
f:\danny.cpp(45) : error C2143: syntax error : missing ';' before '}'
f:\danny.cpp(45) : error C2143: syntax error : missing ';' before '}'
f:\danny.cpp(50) : error C2143: syntax error : missing ';' before '{'
f:\danny.cpp(50) : error C2447: missing function header (old-style formal
list?)
f:\danny.cpp(54) : error C2143: syntax error : missing ';' before '}'
f:\danny.cpp(54) : error C2143: syntax error : missing ';' before '}'
f:\danny.cpp(54) : error C2143: syntax error : missing ';' before '}'
f:\danny.cpp(57) : error C2143: syntax error : missing ';' before '{'
f:\danny.cpp(57) : error C2447: missing function header (old-style formal
list?)
f:\danny.cpp(79) : error C2143: syntax error : missing ';' before 'return'
f:\danny.cpp(80) : error C2143: syntax error : missing ';' before '}'
f:\danny.cpp(80) : error C2143: syntax error : missing ';' before '}'
f:\danny.cpp(80) : error C2143: syntax error : missing ';' before '}'
f:\danny.cpp(81) : error C2143: syntax error : missing ';' before '}'
f:\danny.cpp(81) : error C2143: syntax error : missing ';' before '}'
f:\danny.cpp(92) : error C2143: syntax error : missing ';' before '{'
f:\danny.cpp(92) : error C2447: missing function header (old-style formal
list?)
Error executing cl.exe.

swaptest.exe - 18 error(s), 0 warning(s)
Jul 22 '05 #3
I must not have done a very good job on the cut and paste the first
go-round. My apologies. This is the exact example code that I compiled
on my machine- I promise! :)

//example for usenet
#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;
}

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;
}


Jul 22 '05 #4
On Sat, 24 Jan 2004 08:03:39 -0500, "Danny Anderson" <bt*****@i.hate.spam> wrote:
I must not have done a very good job on the cut and paste the first
go-round. My apologies. This is the exact example code that I compiled
on my machine- I promise! :)


Goodie.

OK, the program is performing exactly as you describe, and also exactly
as it should according to the Holy Standard.

It isn't performing as you expect, sort of "skipping" one input operation,
because you have the equivalent of the following:
#include <iostream>
#include <string>

int main()
{
char c;
std::string s;

std::cout << "A char, please: " << std::flush;
std::cin >> c;

std::cout << "A line, please: " << std::flush;
std::getline( std::cin, s );

std::cout << "Finished!" << std::endl;
}
When the first read operation is finished there is a 'newline' character,
the one marked the end of the first line, left in the buffer.

The second read operation interprets this as an empty line entered by the
user.

What you need to do to get the behavior you have so far expected is to
clear the read buffer, e.g. by using a skip operation or readline.

Jul 22 '05 #5
Danny Anderson wrote:

[code snipped]

Hi,

I've played around with your code a bit. Any comments are
appreciated.

#include <iostream>
#include <fstream>
#include <string>
#include <cctype>
#include <cassert>

using namespace std;
//---function prototypes

// void pause(string s);
/* I chose a more descriptive name. Also, the
message is not related to the pausing as such.
*/

void wait_for_user();

// bool user_is_not_finished();
/* Function names containing "not" eventually
lead to confusing double negations. Turn the
condition around or formulate it positively.
Besides, returning the two-valued bool doesn't
accomodate extension.
*/

typedef enum { dReport, dMaterialList, dQuit } UserDesire;

UserDesire get_user_desire();

// string get_filename(string filetype);
/* Identifying the file types by strings
is imho not appropriate in this program.
It might be in some situations where the
wanted type is specified by an entity
outside your code, or where the available
file types are runtime-configurable.
*/

string get_output_filename(UserDesire desire);
//---

int main()
{
string out_text;
ofstream out_file;
UserDesire desire = get_user_desire();

while(desire != dQuit) {
out_file.open(get_output_filename(desire).c_str(), ios::app);
if(!out_file) {

// Note the "cerr".

cerr << "ERROR: couldn't open output file...\n";
return 1;
}

cout << "Enter some text: ";
getline(cin, out_text);
out_file << out_text << endl;

out_file.close();
desire = get_user_desire();
}

cout << "end of program...";
wait_for_user();
return 0;
}
//---function definitions

void wait_for_user()
{
string foo;
cout << "\n ...hit <Enter> to continue.";
getline(cin, foo);
}
UserDesire get_user_desire()
{
string user_choice;

// The loop terminates upon valid input.

while(true) {
cout << "Add to [r]eport, add to [m]aterial list, [q]uit? ";
getline(cin, user_choice);
if(!user_choice.empty()) {
switch(tolower(user_choice[0])) {
case 'r' : return dReport;
case 'm' : return dMaterialList;
case 'q' : return dQuit;
}
}
}
}
string get_output_filename(UserDesire desire)
{
assert(desire != dQuit);

static const string DEFAULT_REPORT_FILE
(
// "bldg_report"
""
);
static const string DEFAULT_MATERIAL_FILE("dfi_master_mtl.txt");

string filename;

switch(desire) {
case dReport :
cout << "Report file? [" << DEFAULT_REPORT_FILE << "] ";
getline(cin, filename);
if(filename.empty())
{ filename = DEFAULT_REPORT_FILE; }

filename += ".csv";
break;

case dMaterialList :
cout << "Material List Filename? ["
<< DEFAULT_MATERIAL_FILE << "] ";
getline(cin, filename);
if(filename.empty())
{ filename = DEFAULT_MATERIAL_FILE; }
break;

default :

// If we get here, this function is incomplete.

assert(false);
}

return filename;
}
//---end of file
Jul 22 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Christopher Reeve | last post by:
Hi, I wonder if anyone could help me. I am moving accross from C to C++ and am writing a program that reads and writes data into a file. I am aware of the old C ways of doing it but am trying to...
8
by: Brandon McCombs | last post by:
This may be the wrong group but I didn't see anything for VC++ so I'm trying here. I have a C++ book by Deitel and Deitel that says I can use fstream File("data.dat", ios::in | ios::out |...
4
by: Jon Hyland | last post by:
Hi all, I'm looking for the fastest way to write (and/or read) binary to a file in VC++. I've been using the fstream object in this way: //unsigned char *pDataOut and long iLength initilized...
3
by: Abhas | last post by:
> > Hi, this is Abhas, > > I had made a video library program in C++, but was facing a problem. > > After entering 12 movies, i cannot enter any more movies. > > Something gibberish comes instead....
0
by: J. | last post by:
Hello all, I need some assistance. I've been out of the C++ game way too long and I need some help getting back up to speed. I'm taking a class where STL is mostly covered...I know alot of...
9
by: Someonekicked | last post by:
In my program, I need to open multiple files, and I wont know till after the program execution how many of them (user will enter that value). So I am using a vector of fstream. I am using fstream...
1
by: dasilva109 | last post by:
Hi guys I am new to C++ and need urgent help with this part of my code for a uni coursework I have to submit by Thursday //ClientData.h #ifndef CLIENTDATA_H #define CLIENTDATA_H #include...
2
by: dasilva109 | last post by:
Hi guys I am new to C++ and need urgent help with this part of my code for a uni coursework I have to submit by Thursday //ClientData.h #ifndef CLIENTDATA_H #define CLIENTDATA_H #include...
2
by: ivotron | last post by:
Hi, I have very strange problem with std::fstream. I want to read a simple two-column space-separated file with the following code: #include <iostream> #include <fstream> int main() { ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.