Noob.Alone.Programmer
Noob.Alone.Programmer

Reputation: 125

File Handling C++ Error

I wanted write a program which allows users to write some random stuff, but i got an error saying

 no matching call to 
which I am not able to figure it out. please Help me. while you are trying to answer to this question, try to be more noob specific.

here is my code

#include<iostream>
#include<fstream>
#include<string>

using namespace std;

int main()
{
    string story;
    ofstream theFile;
 theFile.open("Random.txt");
 while(cin.get(story,5000)!=EOF)
{
    theFile<< story;
}
return 0;
}

Upvotes: 0

Views: 226

Answers (2)

hfhc2
hfhc2

Reputation: 4391

You seem to be trying to write the content of cin to a file. You could just use stream operators:

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{
  string story;
  ofstream theFile;
  theFile.open("Random.txt");

  if(cin >> story)
  {
    theFile << story.substr(0, 5000);
  }

  return 0;
}

I am assuming that you only want the first 5000 characters in Random.txt...

Upvotes: 1

sasha.sochka
sasha.sochka

Reputation: 14715

cin.get with 2 arguments expects char* as first argument and you are trying to pass string as the first argument.

If you want to read std::string instead of C-string until the end of line use getline(cin, story)

If you want to read string until the next space or newline or another blank symbol use cin >> story;

Upvotes: 1

Related Questions