First problem I noticed when I looked at your code was the missing semicolon ; after
myfile >> string1 but this is just a syntax error.
The while loop you're trying to use will only evaluate when both conditions are true because you're using && operator.
As @sftrabbit said extraction operator >> in C++
leaves a new line character \n in the input buffer and then when you're trying to input
std::getline(myfile, string2) && myfile >> int1
First condition std::getline(myfile, string2) doesn't have a problem getting a new line character and will evaluate to true, but then second condition myfile >> int1 will evaluate to false because it will get a character(s) when it's expecting an integer. That's why your while loop doesn't want to execute.
You can easily fix this problem when you change the extraction operator in
myfile >> string1 with getline(myfile, string1); because getline will leave an empty input buffer.
But then you will have another problem. The while loop will execute only once again because there is \n left in the input buffer so you'll need a myfile.ignore(numeric_limits<streamsize>::max(), '\n');.
Your final code should lool like this:
int main ()
{
ifstream myfile;
string string1;
string string2;
int int1;
myfile.open("filename.txt");
getline(myfile, string1);
while(getline(myfile, string2) && myfile >> int1)
{
myfile.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "read string " << string2 << " and int " << int1 << endl;
}
return 0;
}
I hope this helps you.