#include<iostream>
#include<fstream>
#include<cstdlib>
#include<string>
using namespace std;
**int main()
{
double write();
double read();
string choice;
while(1)
{
cout<<"Enter read to read a file and write to write a file.\n";
cin>>choice;
if (choice == "read")
cout<< read();
if (choice == "write")
cout<< write();
}
}
double read()
{
const int size = 60;
ifstream inFile;
char filename[size];
cout<<"Enter the name of the file you want to read \n";
cin.getline(filename, size);
inFile.open(filename);**
if(!inFile.is_open())
{
cout<<"could not open "<<filename<<endl<<"program terminating";
exit(EXIT_FAILURE);
}
double value;
double sum = 0.0;
int count = 0;
inFile >> value;
while(inFile.good())
{
cout<<value<<"\n";
++count;
sum += value;
inFile >> value;
}
if (inFile.eof())
cout<<"End of file reached. \n";
else if (inFile.fail())
cout<<"Input Terminated by data mismatch.\n";
else
cout<<"input terminated for unknown reason";
if (count == 0)
cout<<"no data processed";
else
{
cout<<"Items read: "<<count<<endl;
cout<<"Sum: "<<sum<<endl;
cout<<"Average: "<<sum / count << endl;
}
inFile.close();
return 0;
}
double write()
{
char type[81];
char filename[81];
cout<<"this program is more or less pointless, any text edtor on earth is better than this for writing"<<endl;
cout<<"files; However This is the first step in learning how to create my file tranfer program."<<endl;
cout<<"Enter the name of a file you want to create.\n";
cin>>filename;
ofstream outFile;
outFile.open(filename);
outFile<<fixed;
outFile.precision(2);
outFile.setf(ios_base::showpoint);
while(!cin.fail()){
cin.getline(type,81);
outFile<<type<<endl;
}
outFile.close();
}
The problem seems to be that when I type in “read” the program does what its supposed to until it gets to cin>>filename; at which point I think it assigns the value of choice to filename because the program skips to if(!inFile.is_open()){ after I type in “read”.(the write function of my program works fine.
could someone please tell me how to solve this problem or another way for the computer to decide weather to choose from functions read or write based on text input.
I am new to C++ so I would appreciate it if the answer is simple, thanks.
P.S. I,m on ubuntu if that makes a difference.
You just have to reconcile your use of “cin>>” versus “getline” when you want to read in a string. Their behavior is slightly different.
Remember that when using
cinto read from the console, every keystroke that the user touches goes into thecinbuffer including the \n for the return key.So when your user types in “read” and hits return to enter their choice, the contents of
cinat that moment are:read\n
Then you do:
The problem is that the
>>operator is defined to read up until it hits whitespace, so it will read in “read”, but it will leave \n still sitting in the cin buffer.So after your
cin >> choicestatement, the contents ofcinare:\n
Then you go to do this:
The problem here is that the
>>operator is defined to read up until whitespace (like \n), butgetlineis defined to read up until it hits a \n and it will read everything on the line up until it sees a \n and then also read in the \n and discard it.Well your issue is that when you get to your
cin.getline(filename,size)statement, there’s still a \n character sitting in thecinbuffer and sogetlineimmediately sees the \n and discards it, storing the empty string intofilename.You have a few choices here.
If you made filename a string and changed your filename read to:
It will work properly because the
>>operator also ignores leading whitespace.You could also change your initial read of
choiceto be like this (usinggetlinefrom the string header here):And that will work because
getlinewon’t leave the stray \n character sitting in thecinbuffer.You could use the
cin.ignorefunction to wipe thecinbuffer clean. You don’t want to just blindy useignoreif you don’t know what you’re doing, but we know in this case that you’re leaving a stray \n oncin, so if you want to get rid of it, you can do this:That
ignorestatement essentially wipes the remaining data incinby saying “discard the next INT_MAX characters or everything up until and including a \n.”Hope that clears up what’s going on for you.