Hi, I'm Sven
I'm a student from Belgium and one of my classes is about C++
I'm working on a program for such a long time, but I can't find the solution.
(First of all : sorry for my bad english)
Here's what I'm supposed to do :
I have to write a program to reads in a txt file with the following sentences :
" I'm not sure, but I think you will find the answer in Chapter
That's a good question..
And so on..
Al the sentences in this txt file start ar a new line. And there 8 sentences...
The program should ask the user to enter a question and gives one of the answer.. First question, first answer .. Second question, second answer..
When the first answer in the file is given there should be a number behind Chapter starting from 18.
Each time the whole file is read, the file should be restarted from the beginning and the number behind Chapter should be -1.
When the number behind Chapter is 0, the number should be reset to 18.
But I really can't figure out how I have to close and reopen the file when the program is at the end of the file. And how should I reset the number behind Chapter when it's 0 ?
This is what I have untill now :
#include <fstream>
#include <iostream>
#include <cstdlib>
using namespace std;
void geef_antwoord(ifstream& answer, int chapter_count);
void new_line ();
const int NUMBER_OF_CHAPTERS = 17;
int main ()
{
using namespace std;
int ch_count=18;
ifstream fin;
fin.open("antwoorden.txt");
if (fin.fail())
{
cout << "Het openen van antwoorden.txt is mislukt \n";
exit(1);
}
else
{
geef_antwoord (fin, ch_count);
if (fin.eof())
{
fin.close();
fin.open("antwoorden.txt");
geef_antwoord (fin, ch_count);
}
}
return 0;
}
void new_line ()
{
char symbol;
do
{
cin.get(symbol);
}while (symbol != '\n');
}
void geef_antwoord (ifstream& answer, int chapter_count)
{
char question, answer_file;
int answer_count=0;
while (!answer.eof())
{
cout << "Give in a question, and the computer will give you the answer ! \n";
cin.get(question);
new_line();
do
{
answer.get(answer_file);
cout << answer_file;
}while (answer_file != '\n');
if ((answer_count==0) && (answer_file == '\n'))
{
cout << chapter_count << ".";
answer_count++;
}
if (!answer.eof())
{
answer_count++;
}
cout << endl;
}
}