ifstream question

Hello! I've enjoyed reading the posts here for a while. I've learned a lot, but I have something that's giving me a real headache. I'm trying to write a program that displays a series of test questions, but I can't seem to get it working. Oh I can use GOTO statements and stuff like that...but that's no fun! :)

Below is a snip of the core code that retrieves the data from the text file. It reads the first five lines and lists them as the question and probable answers 1-4. However, whenever I input an answer, ifstream returns to the start of the text file and displays the same question again.

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
string testfile, question, answer, useranswer;
int line, score;
int runExam();
int processYES();
int processNO();

int runExam(){
            
             ifstream gxtfile (testfile.c_str());
                    
             for ( line = 0; line < 5; line++ ) {   // loop to process test questions
             getline(gxtfile, question);            // assigns the current line to variable "question"
	 	     cout << question + "\n";               // outputs the current line
	 	     }
			
			 getline(gxtfile, answer);		        // assigns the current line to variable "answer"
			
			 cout << "\nPlease input your answer: ";   // prompts for user answer input
			 cin >> useranswer;                        // assigns user input to variable "useranswer"
			 if ( useranswer == "exit" ) { return 0; }     // allows the user to terminate the test
			 if ( useranswer == answer ) { processYES(); }  // processes useranswer
			 else { processNO(); }                          
			 
             }

int processYES(){    // output for user selecting correct answer
            
            cout << "YES";
            cin.get();
            system("cls");
            runExam();
                 }

int processNO() {    // output for user selecting incorrect answer
            cout << "NO";
            cin.get();
            system("cls");
            runExam();
                 }


Here are some of the things I've done to try and fix the problem:

1) I tried moving the code ifstream gxtfile (testfile.c_str()); and declaring it globally before the main() function executes, but that causes the questions to appear blank.

2) I also tried moving ifstream gxtfile (testfile.c_str()); into the main() function, but I get the same result even if I declare gxtfile as a global variable. It's as if the compiler can't read from the variable in function main().

I realize that my code may be a little hackney and amateurish, but I'm relatively new and still experimenting. Can anyone offer any advice on how to proceed?

Thank you!
Hola !!! :) the behaviour is correct , because you open the filestream every time you call runExam() both from processYES() and processNO() functions. So after every such call the pointer is set to the beginning of the file. And one more thing, you don't return anything from your functions so let's declare the return type void. You can try this variant:

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
string testfile, question, answer, useranswer;
int line, score;
void runExam();
void processYES();
void processNO();

void runExam(){
            
    ifstream gxtfile (testfile.c_str());
    do {    
             for ( line = 0; line < 5; line++ ) {   // loop to process test questions
             getline(gxtfile, question);            // assigns the current line to variable "question"
	 	     cout << question + "\n";               // outputs the current line
             }
			
			 getline(gxtfile, answer);		        // assigns the current line to variable "answer"
			
			 cout << "\nPlease input your answer: ";   // prompts for user answer input
			 cin >> useranswer;                        // assigns user input to variable "useranswer"
			 if ( useranswer == answer ) { processYES(); }  // processes useranswer
			 else { processNO(); }                          
    } while ( useranswer != "exit" && !gxtfile.eof() ); // allows the user to terminate the test
}

void processYES(){    // output for user selecting correct answer            
    cout << "YES";
    cin.get();
    system("cls");
    // do not call runExam() here
}

void processNO() {    // output for user selecting incorrect answer
    cout << "NO";
    cin.get();
    system("cls");
    // do not call runExam() here
}
Last edited on
Arsen, you are a lifesaver. This has been hurting my head for a long time. Thank you!
Topic archived. No new replies allowed.