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

I keep getting an error in this code.. what am i doing wrong? // reading a text

ID: 3641190 • Letter: I

Question

I keep getting an error in this code.. what am i doing wrong?



// reading a text file
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main () {
char line, line1;
ofstream myfile ("sample.txt");
if (myfile.is_open())
{
myfile << "*";
myfile << "+";
myfile << "-";
myfile << "/";
myfile << "@";
myfile << "!";
myfile << "~";
myfile << "=";
myfile.close();
}
else cout << "Unable to open file";
ifstream myfile ("sample.txt");
if (myfile.is_open())
{
while ( myfile.good() )
{
getchar (myfile,line);

if(('line'>'line')&&(line1!=line))
cout << line << endl;
line1=line;
else
myfile.close();
return 0;
}
myfile.close();

}

else cout << "Unable to open file";

return 0;
}

Explanation / Answer

while using ifstream give name as myfile1 so it removes conflict . And after if you have typed a sentence so if else mismatch . Then you cannot use getchar with two parameters . You should use myfile1.read(line,1); .For that u shld declare line as array . I ran the code below and it didnt show any error . #include #include #include using namespace std; int main () { char *line, *line1; line = new char [2]; line1 = new char [2]; ofstream myfile ("sample.txt"); if (myfile.is_open()) { myfile