Who Wants To Be A Millionaire

I have a task,my teacher ask me to make a "who wants to be millionaire program".
He told me that my source code is too long because i put the question and the option line per line and then i realized i have type it for 500++ line. then i have an idea to put the question and the option into file stream function. But, my problem is... I cant make a code that read line per line. I mean 1 line 1 element.So I want to put 15 question , and 4 option each question, in 1 file stream.


in file stream... while it reading my .txt file. when there is a space " " it detect as 1 data.

for example : what is your favorite food?

then i use my code. and the ouput will be like :
what
is
your
favorite
food

insteaad of :
what is your favorite food
I am not sure if I understand your problem correctly.
I would store the question and the options in a file like this.

What is your favorite food,hamburger,fish,muffin,doughnut
What is your favorite pet,dog,cat,fish,hamster
....
So you would have 15 lines
Use getline() to read a full line of text:
1
2
string str
getline(cin, str);


To make the input file easy to write, I'd have one line that's the question, followed by 1 line for each possible answer, followed by a blank line to indicate the end of the question. Maybe put a '*' at the end of the correct answer. Your code can look for that and remove it before displaying the choices:

What is the the bread in a hamburger is called?
A biscuit
A muffin
A bun*

What is the capital of New Jersey?
Newark
Trenton*
Camden
Redbank
I use the following program to read quiz questions with answer keys from file:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
using namespace std;//yep, didn't know better! 

struct Question
{
	int m_q_no;
	string m_q;
	int m_Index;
	vector<string> m_answers;

	friend istream& operator>> (istream& is, Question& quest);
	friend ostream& operator<< (ostream& os, const Question& quest);
};

istream& operator >> ( istream& is, Question& quest )
{
	string dummy1, dummy2, m_q_no_temp;
	static const auto delimiter1 = '.';
	static const auto delimiter2 = '?';
	//getline(is, dummy1);
	getline(is, m_q_no_temp, delimiter1)&&
		getline(is, quest.m_q, delimiter2)&&
			is>>quest.m_Index;
	stringstream temp(m_q_no_temp);
	temp>>quest.m_q_no;
    int i{};
	while(i<4)
	{
		string temp_q{};
		getline(is, dummy2, delimiter1)&&
			getline(is, temp_q);
		quest.m_answers.emplace_back(temp_q);
		i++;
	}
	return is;
}

ostream& operator<< (ostream& os, const Question& quest)
{
	os<<"Question "<<quest.m_q_no<<": "<<quest.m_q<<"? \n";
	os<<"The options are: \n";
	for(size_t i = 0; i < quest.m_answers.size(); i++)
	{
	    os<<i+1<<". "<<quest.m_answers[i]<<"\n";

	}
	os<<"And the correct choice is option "<<quest.m_Index<<"\n";
	return os;
}
int main()
{
	ifstream File("F:\\test.txt");
	vector <Question> QQQ;
	if(File)
	{
		Question quest;
		quest.m_answers.clear();
		while (File >> quest)
        {
            QQQ.emplace_back(quest);
        }
           // quest.m_answers.clear();
	}

	for(auto& itr : QQQ)
    {
        cout<< itr;
    }
}

the program reads questions and answers in the following file format:

1. What is the capital of the UK? 2
//2 being the key to the right answer - remove this line from the .txt file

1. Aberdeen
2. London
3. Cardiff
4. Belfast

2. Which is the largest planet in the solar system? 3

1. Saturn
2. Neptune
3. Jupiter
4. Uranus
So guys, This is my simple program without file stream
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
cout<<"\t\t\tRp 100.000"<<endl;
	cout<<"1. What the capital of Indonesia "<<endl;
	cout<<"a.Bandung\nb.Jogja\nc.Jakarta\nd.London\n";
	cin>>s.s1;
	
	if(s.s1=='c')
	{
		cout<<"Correct !"<<endl;
		system("pause");
		system("cls");
	}else
	{
		cout<<"Wrong";
		return 0;
	}


So, How to write the if function, when the question and answer in file stream, and I want put fungction lifeline(), in each number question?
and @dayden , how can possible my code can read "*".
closed account (48T7M4Gy)
You can structure the data very simply if you use a pattern based on the following. The data file is just a stream of question, N, N answers and correct answer. The array just makes it clearer here but is not needed if the data is read from a file. You might need a question reference number, maybe:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>

using namespace std;

int main()
{
    // STRUCTURE THE DATA AS FOLLOWS (N  VARIES)
    string question = "What is the capital of Indonesia";
    const int N = 3;
    string answers[N] = {"a. London", "b. Jakarta", "c. Paris"};
    char correct_answer = 'b';
    char choice;
    
    
    // HERE IS THE QUESTION
    cout << question << endl;
    for(int i = 0; i < N; ++i )
        cout << answers[i] << endl;
    
    cout << "Please enter answer: \n";
    cin >> choice;
    
    // EVALUATE ANSWER
    if(choice == correct_answer)
        cout << "You won Rp10000000\n";
    else
        cout << "Kosong\n";
    
    
    return 0;
}
What is the capital of Indonesia
a. London
b. Jakarta
c. Paris
Please enter answer: 
b
You won Rp 10000000
Program ended with exit code: 0
Last edited on
I was confused in file stream, how to write question and answare on txt file, write txt to program, and evaluate answer
closed account (48T7M4Gy)
With the way I am suggesting this is how the text file would look for one question:


What is the capital of Indonesia?
3
a. London
b. Jakarta
c. Paris
b
@kemort , and how to write to the program ? hehehe , I so confused
closed account (48T7M4Gy)
Look up the tutorial on how to read the file. Try it yourself, there are sample programs you can easily adapt - you are looking for the sample 'reading a text file'.

Start with reading in a couple of questions you have prepared in Notepad or whatever. Don't forget that every second line you are reading an integer so you can read in the right number of possible answers.

http://www.cplusplus.com/doc/tutorial/files/
@Kemort , thanks bro, I'll try
closed account (48T7M4Gy)
Excellent, good luck with it. Let's see how you go.
@kemort, I already try to use code like you share on link
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// reading a text file
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main () {
  string line;
  ifstream myfile ("example.txt");
  if (myfile.is_open())
  {
    while ( getline (myfile,line) )
    {
      cout << line << '\n';
    }
    myfile.close();
  }

  else cout << "Unable to open file"; 

  return 0;
}


But the output are
What is the capital of Indonesia?
3
a. London
b. Jakarta
c. Paris
b


I want to output only question and option like,
What is the capital of Indonesia?
a. London
b. Jakarta
c. Paris

and how to evaluate the answer, when the answer on file stream ?
How to do that all ?
closed account (48T7M4Gy)
Well that's good so far. You are on the right track. Like I said the second input from the file is an integer which enable an array of answers to be read in. It's quite easy.

For each question-group on the .txt file you have to get it into the computer memory:

read by getline the question
read integer N
setup array
read by getline each answer via a for loop N times
read correct answer as a char
closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
getline (myfile,question);
            std::cout << question << '\n';
            
            myfile >> number_of_answers;
            //std::cout << number_of_answers << '\n';
            myfile.ignore(100,'\n');
            
            for(int i = 0; i < number_of_answers; ++i){
                getline(myfile,answer[i]);
                std::cout  << answer[i] <<  '\n';
            }
            
            myfile >> correct_answer;
            //std::cout << correct_answer << '\n';
            myfile.ignore(100, '\n');
@kemort
thanks bro , that work
closed account (48T7M4Gy)
Maybe the whole thing is useful:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <iostream>
#include <fstream>
#include <string>

int main() {
    
    std::string question;
    std::ifstream myfile ("millionaire.txt");
    
    int number_of_answers = 0;
    char correct_answer;
    
    const int limit{10};
    std::string answer[limit];
    
    char another_question = 'y';
    char response = ' ';
    
    if (myfile.is_open())
    {
        while ( tolower(another_question) == 'y' ) {
            
            std::cout << "===================================\n";
            
            getline (myfile,question);
            std::cout << question << '\n';
            
            myfile >> number_of_answers;
            //std::cout << number_of_answers << '\n';
            myfile.ignore(100,'\n');
            
            for(int i = 0; i < number_of_answers; ++i){
                getline(myfile,answer[i]);
                std::cout << answer[i] <<  '\n';
            }
            
            myfile >> correct_answer;
            //std::cout << correct_answer << '\n';
            myfile.ignore(100, '\n');
            
            std::cout << "What is your answer?\n";
            std::cin >> response;
            
            if(response == correct_answer)
                std::cout << "That's correct :)\n";
            else
                std::cout << "Wrong answer :(\n";
            
            std::cout << "Another question <y/n>?\n";
            std::cin >> another_question;
            
        }
        myfile.close();
    }
    
    else
        std::cout << "Unable to open file\n";
    
    return 0;
}
and millionaire text is:
What is the capital of Indonesia?
3
a. London
b. Jakarta
c. Paris
b
What is the biggest planet in the universe?
4
a. Jupiter
b. The moon
c. Who knows?
d. 2669 East
c
Who invented C++?
2
a. Ronald McDonald
b. Bjarne Stroustrup
b
Obviously this can be extended as required.
Topic archived. No new replies allowed.