Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

why won't this code compile? It says there are build errors when I try to run it. I'm trying to make a program to read

why won't this code compile? It says there are build errors when I try to run it. I'm trying to make a program to read a file with questions for a drivers test with 10 questions. I need the program to print the questions and the possible answers to the console and the user to be able enter their guess through console input. Then the program to print if they passed the test (get at least 7 answers right) if possible. If it's not too complicated. the questions and possible answers should be in a .txt file and the program access them and print them from the file. The answers should be stored in the program memory. The answers are on line 8 of the code

code:

#include

#include

#include

using namespace std;

//########## 1 2 3 4 5 6 7 8 9 10

const char ANSWERS[10] = { 'D' , 'C' , 'D' , 'A' , 'A' , 'A' , 'A' , 'D' , 'A' , 'D' };

void userAnswers(string);

int main() {

string questions;

char arAnswers[10];

int a = 0, c = 0, answerNum[10];

cout << " Enter the file name (with extension): ";

cin >> questions;

// userAnswers(fileName);

ifstream fileIn; //Read in from file

fileIn.open(questions);

if (fileIn.is_open()) {

while (fileIn >> arAnswers[a]) {

a++;

}

}

else

cout << " ERROR: File not found!"; //file validation

fileIn.close();

a = 0;

for (int b = 0; b < 20; b++) {

if (arAnswers[b] == ANSWERS[b]) {

answerNum[a] = b + 1;

a++;

}

else

c++;

}//Outputs:

if (a > 14)cout << endl << "You've passed the test!";

else cout << endl << "You've failed the test!";

cout << " Total correct answers: " << a;

cout << " Total incorrect answers: " << c << endl << "Correct answers are: ";

for (int b = 0; b < a; b++) {

cout << endl << answerNum[b] << endl;

}

system("pause");

return 0;

}

void userAnswers(string questions) {

char ans[20];

ofstream fileOut; //Output to file

fileOut.open(questions);

for (int a = 0; a < 20; a++)

{

if (fileOut.is_open()) {

cout << " Enter the answer for question #" << a + 1 << ": ";

cin >> ans[a];

while (ans[a] < 65 || ans[a] > 68) { //Answers validations

cout << endl << "ERROR: Invalid Answer! Try Again!";

cout << " Enter the answer for question #" << a << ": ";

fileOut << ans[a] << " ";

}

}

}

questions.txt file:

1. What is the most important driving technique to avoid crashes when driving in icy or snowy conditions?

A. Add extra weight to the vehicle to improve traction.

B. Get off the highways as quickly as possible.

C. Engage four-wheel drive on the vehicle.

D. Reduce speed and increase following distance.

2. A flashing red traffic signal at an intersection has the same requirements as which of the following?

A. A slow sign

B. A yield sign

C. A stop sign

D. An intersection sign

3. The application for an operator's license must be signed by the parent or guardian when the applicant is under what age?

A. 16

B. 20

C. 21

D. 18

4. Unless it is posted otherwise, the speed limit in a residential area is:

A. 25 miles per hour.

B. 20 miles per hour.

C. 35 miles per hour.

D. 15 miles per hour.

5. Child restraints are required for which of the following?

A. All children who are under the age of eight and are less than 4'9".

B. All children for whom the driver of the car is the parent or guardian.

C. All children who are seated in the front seat.

D. All children who are under the age of six when air bags are not available.

6. When traveling on a highway divided into four traffic lanes, which vehicles are required to stop for a school bus that has stopped to unload children?

A. Only vehicles approaching the rear of the bus traveling in the same direction as the bus.

B. All vehicles approaching the bus from either direction.

C. No one is required to stop unless children are in view.

D. All vehicles may pass the bus after providing an audible signal.

7. When two vehicles arrive at an intersection at the same time, which one has the right-of-way when no signs or signals indicate rules?

A. The car approaching from the right has the right-of-way.

B. The car approaching from the left has the right-of-way.

C. The car in which the driver sounds his horn first has the right-of-way.

D. The car that is traveling faster has the right-of-way.

8. If someone has consumed alcoholic drinks, what will help the person overcome the influence of those drinks?

A. Tomato juice and lime

B. Hot coffee

C. Fresh air

D. Time

9. When is a driver permitted to turn right on a red traffic signal?

A. When the driver has stopped and checked to see that the turn will not interfere with crossing traffic.

B. When signs are clearly posted to allow a right turn on red.

C. Only at the direction of a police officer.

D. After first slowing and verifying the turn will not interfere with other traffic and pedestrians.

10. When a stop is required at an intersection and no markings appear to indicate a stop line or crosswalk, which of the following is the appropriate place to make the stop?

A. The driver is not required to stop.

B. The driver is required to slow down to make sure crossing traffic is clear.

C. Only at a place where the driver can see at least 200 feet on either side without regard for the intersecting roadway.

D. At the point nearest the intersecting roadway where the driver has a view of approaching traffic on the intersecting roadway before actually entering the roadway.

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

Modern Dental Assisting

Authors: Doni Bird, Debbie Robinson

13th Edition

978-0323624855, 0323624855

Students also viewed these Programming questions

Question

Explain how business markets differ from consumer markets.

Answered: 1 week ago