1 Replies - 361 Views - Last Post: 31 May 2018 - 07:35 AM Rate Topic: -----

#1 josephhoffman28   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 28-February 18

Need C++ help with error File I/O.

Posted 31 May 2018 - 07:33 AM

Need C++ Help. My instructor said the following after making many changes I am at lost it works when ofstream and ifstream are global but not when I move it down in main() please help:

"The following variables should be defined in main(), not global variables:

ofstream outf;
ifstream inf;

What will happen if you move them in main(). Localize them."

So I did this now I am getting an error on line 98. My code bellow followed by the error codes:

My code:
#include <iostream>

#include<vector>

#include <string>

#include<sstream>

#include <fstream>

using namespace std;

//the files I used

//ofstream outf;

//ifstream inf;

class Question // super class

{

public:

string getQuestion()//gets the question

{

return question;

}

int getValue() //gets the point value of the question

{

return value;

}

virtual void setQuestion(string answer, int value)

{

}

virtual void printOptions()

{

}

virtual string getAnswer()

{

return answer;

}

private:

string question, answer;

int value;

};

class QuestionTF : public Question// class for true and false questions

{

public:

void setQuestion(string theQuestion, int pointValue)

{

string theAnswer;

question = theQuestion;

points = pointValue;

options = "true/false";

//get the answer from the file

getline(inf, theAnswer);

answer = theAnswer;

}

void printOptions()//prints the options for that question

{

cout << question << endl;

cout << answer << endl;

}

string getAnswer()//outputs the answer for that question

{

return answer;

}

private:

string question;

string answer;

int points;

string options;

};

class QuestionMC : public Question//class for multiple choice

{

public:

void setQuestion(string answerarray, int pointValue)

{

stringstream ss(answerarray);

string tok;

getline(ss, tok, ',');

numberOfOptions = stoi(tok);

getline(ss, tok, ',');

int i = 0;

while (getline(ss, tok, ','))

{

if (i>numberOfOptions)

break;

options[i] = tok;

i++;

}

answer = tok;

}

void printOptions()// prints the questions, options, and answer

{

char first = 'A';

cout << question << endl;

for (int count = 0; count<numberOfOptions; count++) {

cout << first++ << ". " << options[count] << endl;

}

cout << "Answer: " << answer << endl;

}

string getAnswer()// prints the answer

{

return answer;

}

private:

int value, numberOfOptions;

string question, answer;

string options[6];

};

int main() {

ofstream outf;

ifstream inf;

Question *myQuestions[10];

string questiontype, questiontxt;

string answertxt, optiontxt;

int numquestions = 4, questionvalue;

// this is something to write the test bank test file from the text given Week 2

// You might use a text file that you create separately and avoid this

// in Week 3 the focus is developing a test file section that can replace this.

outf.open("c:\\temp\\IP2_testbank.txt");

if (outf.is_open())

{

outf << "3\n";

outf << "TF 5\n";

outf << "There exists birds that cannot fly?\n";

outf << "true\n";

outf << "MC 10\n";

outf << "Who was the President of the USA in 1991?\n";

outf << "6\n";

outf << "Richard Nixon\n";

outf << "Gerald Ford\n";

outf << "Jimmy Carter\n";

outf << "Ronald Reagan\n";

outf << "George Bush Sr.\n";

outf << "Bill Clinton\n";

outf << "E\n";

outf << "TF 10\n";

outf << "The city of Boston hosted the 2004 Summer Olympics?\n";

outf << "false\n";

outf.close();

}

else cout << "Unable to open file";

//opening the testbank file and processing as a question of each type

inf.open("c:\\temp\\IP2_testbank.txt");

string line, theQuestion, theAnswer;

if (inf.is_open())

{

//get the number of questions from the first line in the file

getline(inf, line);

numquestions = stoi(line);

for (int count = 0; count<numquestions; count++) {

getline(inf, line);

//get the next line with the question type and the value of the question

int npos = line.size();

int prev_pos = 0;

int pos = 0;

while (line[pos] != ' ')

pos++;

questiontype = line.substr(prev_pos, pos - prev_pos);

prev_pos = ++pos;

questionvalue = stoi(line.substr(prev_pos, npos - prev_pos)); //Last word

//process a true/false question

if (questiontype == "TF")

{

myQuestions[count] = new QuestionTF;

getline(inf, theQuestion);

myQuestions[count]->setQuestion(theQuestion, questionvalue);

}

//process a multiple choice question

if (questiontype == "MC")

{

myQuestions[count] = new QuestionMC;

getline(inf, theQuestion);

//myQuestions[count]->setQuestion(theQuestion, questionvalue);

string str = "";

string line;

//get the number of choices from the file

getline(inf, line);

str = str + line + ",";

int numberOfOptions = stoi(line);

//question = theQuestion;

//value = pointValue;

//get the individual choice lines and load to options array

for (int count = 0; count<numberOfOptions; count++) {

getline(inf, line);

str = str + line + ",";

//options[count] = line;

}

//get the answer from the file and load into answer

getline(inf, line);

str = str + line;

myQuestions[count]->setQuestion(str, 0);

}

}

}

//print out the questions that have been processed

for (int count = 0; count<numquestions; count++)

{

myQuestions[count]->printOptions();

cout << endl;

}

getchar();

return 0;

}



Error:
E0304 no instance of overloaded function "getline" matches argument list line 89
E0020 identifier "inf" is undefiened line 89
C2065 'inf: undeclared identifier

Please help me with this!!!!

Is This A Good Question/Topic? 0
  • +

Replies To: Need C++ help with error File I/O.

#2 no2pencil   User is offline

  • Professor Snuggly Pants
  • member icon

Reputation: 6644
  • View blog
  • Posts: 30,937
  • Joined: 10-May 07

Re: Need C++ help with error File I/O.

Posted 31 May 2018 - 07:35 AM

** Closed as duplicate **
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1