views:

50

answers:

4
#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.

A: 

You can use cin.ignore(INT_MAX); to clear the input buffer before cin.getline(filename, size);

syork
You are just hiding the error rather than correcting it.
Martin York
A: 

The problem is that you are not reading the "EOL sequence" (AKA '\n') from the input stream:

cin>>choice;

This reads one word. But does not read the '\n' character from the input.
So the following read:

cin.getline(filename, size);

Just reads the '\n' character that you left on the stream after the last read (rather than the next line that you were expecting).

When reading input from the user.
I always prefer to read the line of the input and then process it internally.

std::string  command;
std::getline(std::cin, command); // reads the whole line (but discards the '\n')

if (command == "read") { /* Stuff */ }

Then your code for getting the file name should work:

std::string  filename;
std::getline(std::cin, filename);

std::ifstream(filename.c_str());

All done.

While we are here: This is clever in that you avoid the normal problem with looping on a stream.
But there is a better solution (by better I mean more compact):

inFile >> value;
while(inFile.good()) 
{
  cout<<value<<"\n";
  ++count;
  sum += value;
  inFile >> value;
}

Try this:

while(inFile >> value) 
{
  cout<<value<<"\n";
  ++count;
  sum += value;
}

Tip 2: Prefer to use std::string over a fixed size char buffer.

Martin York
Hey Thanks everyone, I used char because thats what my book said and I could not get cin.getline to workout it. i'll use std::string now,thanks
A: 

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 cin to read from the console, every keystroke that the user touches goes into the cin buffer including the \n for the return key.

So when your user types in "read" and hits return to enter their choice, the contents of cin at that moment are:

read\n

Then you do:

string choice;
cin >> choice;

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 >> choice statement, the contents of cin are:

\n

Then you go to do this:

char filename[size];
cout<<"Enter the name of the file you want to read \n";
cin.getline(filename, size);

The problem here is that the >> operator is defined to read up until whitespace (like \n), but getline is 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 the cin buffer and so getline immediately sees the \n and discards it, storing the empty string into filename.


You have a few choices here.

If you made filename a string and changed your filename read to:

cin >> filename;

It will work properly because the >> operator also ignores leading whitespace.

You could also change your initial read of choice to be like this (using getline from the string header here):

string choice;
getline(cin,choice);

And that will work because getline won't leave the stray \n character sitting in the cin buffer.

You could use the cin.ignore function to wipe the cin buffer clean. You don't want to just blindy use ignore if you don't know what you're doing, but we know in this case that you're leaving a stray \n on cin, so if you want to get rid of it, you can do this:

#include <limits>

string choice;
cin >> choice;
cin.ignore(std::numeric_limits<int>::max(),'\n');

That ignore statement essentially wipes the remaining data in cin by 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.

Brent Nash
Thanks i appreciate it
I really do appreciate it when people go out of their way to help a stranger.
A: 

Thank you all; I found my answer using brent nash's by putting this include at the top of the source file;

(#)include

and by putting this code after my cin>>choice;

cin.ignore(std::numeric_limits::max(),'\n');

I will look into the other answers though;