Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Need C++ Help. My instructor said the following: The following variables should be defined in main(), not global variables: ofstream outf; ifstream inf; What will

Need C++ Help. My instructor said the following:

"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 screen shot of error:

My code:

#include

#include

#include

#include

#include

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

cout

}

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

for (int count = 0; count

cout

}

cout

}

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

outf

outf

outf

outf

outf

outf

outf

outf

outf

outf

outf

outf

outf

outf

outf

outf

outf.close();

}

else cout

//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

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

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

{

myQuestions[count]->printOptions();

cout

}

getchar();

return 0;

}

Error:

image text in transcribed

Please help me with this!!!!

C5215-Joseph-Hofman-P3 Microsoft Visual Studio Quick Launch (Ctrl-O) File Edit iew Project Build Debug Team Tools Test Analyze Window Help Joe Hotman JH Debug x86 S215_Joseph Hoffman_IP3.cpp* [ . CS21 5Joseph-Hoffman-IP3 x Solution Explorer ?? ?QuestionTF setQuestion(string theQuestion, int pointValue) ? class QuestionTF publie question/ elass for true and false questions Search Solution Explorer (Ctrl+) Solution'CS215_Joseph_Hoffman_JP3 (1 public: CS215 Joseph Hoffman IP3 References void setQuestian(string thequestion, int peintvalue) 75 7% External Dependencies Header Files Resource Files 75 string theAnswer quastion -thequestion points -pointvalus cptions -"true/false" get the answer fron the file getline int. theAnswer): Source Files CS215 Joseph Hoffiman IP3.cpp ?5 85 91 75% Error List ?1 ErrorlA0warningsl@oMessages m Build Only Entire Solution Search Error List File Code Description C2065 'inf: undeclared identifier Line Su CS215 Joseph Hoffman IP3 cs215 joseph hoffman ip... 89 Error List Output Find Symbol Results Solution Explorer Team Explorer Ln 78 Col 9 Ch 9 INS Add to Source Control

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Privacy In Statistical Databases International Conference Psd 2022 Paris France September 21 23 2022 Proceedings Lncs 13463

Authors: Josep Domingo-Ferrer ,Maryline Laurent

1st Edition

3031139445, 978-3031139444

More Books

Students also viewed these Databases questions