soniccool
soniccool

Reputation: 6058

Read one line from .txt file and insert into variable

I am trying to write a program that will read just the first line of my textfile and then input that number into an int variable. But im confused as how to do it.

    int highscore; // Starting highscore

  ifstream myfile ("highscore.txt");
  if (myfile.is_open())
  {
    while ( myfile.good() )
    {
      getline(myfile,highscore);
      cout << highscore << endl;
    }
    myfile.close();
  }

But for some reason im getting the error. |25|error: no matching function for call to 'getline(std::ifstream&, int&)'|

Upvotes: 0

Views: 1446

Answers (1)

John Carter
John Carter

Reputation: 6986

If you replace the getline with:

if (myfile >> highscore)
    cout << "Read " << highscore << '\n';
else
    cout << "Couldn't read an int\n";

You'll be able to read an int into highscore. Are you required to use getline?

Upvotes: 1

Related Questions