Help with passing input file by reference!

So Everytime I run the program it prompts me for the path to the file and I input it, but it never goes into the function fillQuestionsArray. I've been trying for hours and can't find a solution. I've seen people don't like using in.peek , but my teacher WANTS it like that. Please help me!

Not sure if it matters, but this is the file contents -

Q1. When driving through a work zone, it is a good safety practice to:
A. Drive close to the vehicle in front of you to keep traffic flowing freely

B. Lengthen your usual following distance - by double

C. Turn on your cruise control

D. Shorten your usual following distance - by about half
$

Q2. When driving on a freeway entrance ramp, you should look for a gap in freeway traffic by:
A. Looking in the inside rearview mirror only

B. Looking in the sideview mirror only

C. Looking in both rearview and sideview mirrors

D. Looking in your mirrors and turning your head to look over your shoulder
$

Q3. After a train has passed, you should:
A. Check again for approaching trains and proceed with caution

B. Wait for a green light

C. Proceed across the tracks

D. Blow horn and proceed
$

Q4. Your car starts to skid on a slippery road. You should
A. Steer in the direction you want the front wheels to go.

B. Lock your brakes until you come to a full stop.

C. Brake quickly and keep the wheel straight.

D. Steer toward the side of the road to get off the wet pavement.
$

Q5. You want to turn left at an intersection. The light is green but oncoming traffic is heavy. You should
A. Use the next intersection.

B. Wait at the crosswalk for traffic to clear.

C. Wait in the center of the intersection for traffic to clear.

D. Take the right-of-way since you have the light.
$

Q6. If you are driving behind a motorcycle, you must:
A. Allow the motorcycle to use a complete lane

B. Drive on the shoulder beside the motorcycle

C. Allow the motorcycle to use only half a lane

D. Pass in the same lane where the motorcycle is driving
$

Q7. A traffic light which has a green arrow and a red light means that
A. You may only drive straight ahead.

B. You may drive only in the direction of the green arrow.

C. You must wait for a green light.

D. Vehicles moving in any direction must stop.
$

Q8. When you want to make a right turn, your car must be
A. Close to the right side of the street.

B. Close to the left side of the street.

C. Near the center of the street.

D. Past the center of the intersection when you begin to turn.
$

Q9. When driving in bad weather, drivers should:
A. Increase speed to reach final destination more quickly

B. Decrease the "two-second" rule

C. Increase the "two-second" rule

D. None of the above
$

Q10. When driving on a one way street and an emergency vehicle with flashing lights is behind your car, you:
A. Drive with your flashers on

B. Slow down until the vehicle passes you

C. Speed up and take the nearest exit

D. Drive toward the nearest road side and stop
$

Q11. People under 16 years of age who use a false identification card to buy alcohol will:
A. Not be able to take the driver`s exam until their 21st birthday

B. Receive a driving suspension that starts on their 16th birthday

C. Receive a driving suspension that starts on their 21st birthday

D. Be sent to an alcohol safety education class
$

Q12. After an emergency vehicle passes you with its siren on, you must:
A. Drive closely to the police car

B. Drive as fast as the police car

C. Avoid driving closer than 500 feet behind the emergency vehicle

D. Drive near the curb very slowly
$

Q13. A safe speed to drive your car
A. Is the posted speed limit.

B. Is less than the posted speed limit.

C. Depends on the mechanical skill of the driver.

D. Depends on the weather and road conditions.
$

Q14. A NO PARKING sign at a certain location means
A. You may stop temporarily to load or unload passengers.

B. You may never stop your vehicle there.

C. You may park there if the driver remains in the vehicle.

D. You may leave your vehicle unattended for less than 5 minutes.
$

Q15. What is the definition of defensive driving?
A. Drive just below the speed limit in certain situations to improve your braking distance.

B. Keep your eyes moving to look for possible hazards.

C. Keep your foot over your brake pedal when needed to reduce your reaction times.

D. All of the above.
$

Q16. When you hear a fire engine siren, you must:
A. Slow down until it passes you

B. Drive with your flashers on

C. Pull over to the side of the road and stop

D. Speed up and take the nearest exit
$

Q17. To turn left on multi-lane streets and highways, you should start from:
A. The middle of the intersection

B. The right lane

C. The left lane

D. Any lane
$

Q18. What should you do if your car starts hydroplaning or skidding?
A. Turn the wheel firm and sharp

B. Try to gradually lower your speed

C. Step firm on the brake

D. Try to steer off or away from the road
$

Q19. When entering a highway from an entrance ramp, you should generally:
A. Enter above the speed of traffic to get ahead

B. Enter slowly to avoid other vehicles

C. Stop first, then slowly enter traffic

D. Accelerate to the speed of traffic
$

Q20. Which of the following statements is true about BAC (blood alcohol content)
A. The breathalyzer is a test of a person's BAC.

B. A chemical test for BAC is needed for an alcohol conviction.

C. BAC levels are reduced by a person's physical fitness.

D. After drinking, coffee or a cold shower will lower your BAC.

tldr;
I need help passing the file from opeFile() to fillQuestionArray()

[code]
#include <fstream>
#include <iostream>
#include <string>
using namespace std;

void openFile(ifstream & input, const string & message);
int fillQuestionsArray(string questions[], ifstream & in);

void openFile(ifstream & input, const string & message)
{
string inputFileName;
cout << "Enter the correct file path to the questions/answers: " << endl;
getline(cin, inputFileName);
ifstream in(inputFileName);
if (!in.is_open())
{
cout << "Error in opening the file. " << endl;
system("pause");
exit(0);
}

}

int fillQuestionsArray(string questions[], ifstream & in)
{

while (in.peek() != EOF)
{
cout << "Go into the loop? in peek loop" << endl; // test to see if it even goes into the loop, it does not print it so it never goes in
int index = 0;
string line = " ";
getline(in, line);
cout << line << endl;
questions[index] = questions[index] + "\n" + line;
while (line != "$")
{
getline(in, line);
questions[index] = questions[index] + "\n" + line;

}
cout << "Index is: " << index << endl;
index++;
}
cout << questions[0] << endl;
cout << questions[13] << endl;


return 0;
}

int main()
{
ifstream input = { };
string message;
string questions[19];
openFile(input, message);
fillQuestionsArray(questions,input);
cout << "Did the loop go through?" << endl; // Test to see where the code went
system("pause");
}
Last edited on
9:26: warning: unused parameter ‘input’ [-Wunused-parameter]
 void openFile (ifstream& input, const string& message)



You are not openning the file passed as parameter, but a local one.
ifstream in(inputFileName);
you may use the .open() function
http://www.cplusplus.com/reference/fstream/ifstream/open/

1
2
3
cout << "Go into the loop? in peek loop" << endl; // test to see if it even goes into the
                                                  // loop, it does not print it so it never
                                                  // goes in 
you may also use a breakpoint.



> I've seen people don't like using in.peek , but my teacher WANTS it like that.
1
2
3
4
5
6
7
8
	while (in.peek () != EOF)
	{
		//...
		while (line != "$")
		{
			getline (in, line); //¿what if this reading fails?
			questions[index] = questions[index] + "\n" + line;
		}

Topic archived. No new replies allowed.