Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Use an ifstream object named indata to read the first three integers from a file

ID: 3640405 • Letter: U

Question

Use an ifstream object named indata to read the first three integers from a file called lottowins and write each number to standard output, on a line by itself. Assume that this is the extent of the input that this program will do.

This is what I have, I know it could of been wrote a bit easier but I wanted to break it down to see mabe what it is that I am doing wrong?

int num1,num2,num3;

indata.open("lottowins");

indata >> num1;
cout << num1;

indata >> num2;
cout << num2;

indata >> num3;
cout << num3;

indata.close ();

Explanation / Answer

There was a couple things you were doing wrong... First, indata needs to be an object of type ifstream (which is short for Input File Stream) and you need to include the fstream header file to use that. Then, when you open a document, you need to include the ENTIRE document name. You can't just say open "lottowins", you have to say open "lottowins.txt" or whatever the file type is. And you have to make a newline after each number you output in order to make them on a separate line. Here is the correct code: #include #include using namespace std; int main() { ifstream indata; int num1,num2,num3; indata.open("lottowins.txt"); indata >> num1; cout