Hello, I have a program that I'm writing and it seems to just stop. I've looked at the code and I can't seem to figure it out. This is for a class, so please don't just fix it, but please tell me how to go about fixing it, I want to learn this so it's important that I can learn it, but any suggestions would be appreciated!!!
This program is reading a txt file and echoing back three lines if it matches what the user inputted.
Here is the code:
#include <iostream>
#include <fstream>
#include <cstring>
#include <cctype>
using namespace std;
int main() {
//Local variables
char line0[256], line1[256], line2[256], line3[256]; // Lines used for class, date, and assignment
char fname[256];
char request[25];
//Welcome message and file request
cout << "Welcome to the program, this program will read assignments from a file for you based on what class you're looking for. But first, what file would you like to look in?" << endl;
cin.get(fname, 256, '\n');
cin.ignore();
//Prompt for the class they are looking for and store it in request
cout << "What class are you looking for?" << endl;
cin >> request;
cin.ignore();
cout << endl;
ifstream content; //name for file stream content
content.open(fname); //Open the file that the user requested under codename content
if(!fname) {
//If the file failed to open, display an error message
cout << "There was an error opening the file." << endl;
}
// if the file opened fine, continue
else {
content.get(line0, 256, '\n');
char ch = content.get();
int i;
i = 1;
while (!content.eof()) {
content.get(line2, 256, '\n');
ch = content.get();
content.get(line3, 256, '\n');
ch = content.get();
content.get(line1, 256, '\n');
ch = content.get();
//stops endless loop
while (content.peek()=='\n') {
content.ignore();
}
// if the request is same as the content, then echo it
if ((strcmp(line3, request) ==0) || (strcmp(line2, request) ==0) || (strcmp(line1, request) ==0) || (strcmp(line0, request) ==0)) {
if (i = 1) {
cout << line0 << endl;
cout << line2 << endl;
cout << line3 << endl;
}
else {
cout << line1 << endl;
cout << line2 << endl;
cout << line3 << endl;
}
}
++i;
}
}
return(0);
}