Answer to Question #341229 in C++ for Rooney

Question #341229

Write a program in which there is five questions having 4 choices (a,b,c,d). You have to write question in (.txt) file and use fstream and use file handling concept in a [void question ()] function. Open the .txt file inside a function given above.









15 Marksp









Thanks.

1
Expert's answer
2022-05-15T16:59:30-0400
#include<iostream>
#include<string>
#include<fstream>

using namespace std;

class Player
{
	string name;
	int rollNo;
public:
	Player() {}
	Player(string _name, int _rollNo)
		:name(_name), rollNo(_rollNo) {}
	void Assign()
	{
		cout << "Please, enter a name of player: ";
		cin >> name;
		cout << "Please, enter a rollNo of player: ";
		cin >> rollNo;
	}
	void DisplayPlayer()
	{
		cout << "\nPlayer`s name: " << name
			<< "\nPlayer`s roll no: " << rollNo;


	}
};


class Quiz :public Player
{
	float result;
public:
	Quiz() :result(0) {}
	Quiz(string _name, int _rollNo) :result(0), Player(_name, _rollNo) {}
	void AddValue() { result += 10; }
	void StartQuiz()
	{
		cout << "Your are welcome in a quiz!\n"
			<< "Rules:\n correct answer 10 points\n"
			<< "No correct answer 0 points\n"
			<< "Max result - 80 points\n";
		ifstream ifs("in.txt");
		if (!ifs.is_open())
			cout << "File isn`t opened!";
		else
		{
			string line;
			char ch;
			int n = 0;
			while (getline(ifs, line, '\n'))
			{
				cout << line << endl;
				if (n % 5 == 4)
				{


					cout << "\nYour select: ";
					cin >> ch;
					if (((n == 4) || (n == 14) || (n == 34) || (n == 39)) && ch == 'a')
						AddValue();
					if (((n == 9) || (n == 19) || (n == 29)) && ch == 'c')
						AddValue();
					if ((n == 24) && ch == 'd')
						AddValue();
				}
				n++;
			}
		}
	}
	void Display()
	{
		DisplayPlayer();
		cout << "\nYour result is " << result;
	}
};




int main()
{
	Quiz q;
	q.Assign();
	q.StartQuiz();
	q.Display();
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS