How to create Nested Loop *REALLY NEED HELP*

Hey guys, I'm a uni student new to programming so I only know the basics. Basically I have to create a nested loop in my program so once the user has entered the time they want to travel it will come up on the screen the best journey to travel.

Here's the bit I cant do..

The task requires you to produce an application that first requests a time period from the user then proceeds to read the available journeys from a file, in the following format:

<journey name>
<c> // where c is the number of changes
<dn><sn> // for each leg n, dn is the distance and sn is the speed. Note n=(c+1).


Your application will calculate the total journey time for each journey and then compare it with the time period specified by the user. If the journey can be completed in the specified time then the result must be written to the screen and to an output text file called results.txt. If the journey cannot be completed in time then the results must be written to the screen only. Messages to the screen must indicate whether the journey is suitable or not.
7


The file I have to read from is set out a bit like

Journey one
1 - That being the amount of times you have to swap trains
200 - Miles 90 - Time
100 - Miles 60 - Time

Help will be much appreciated as I am so confused and have no idea where to go with this because all I have done so far is where the user enters their time and it will convert it to minutes.
Last edited on
can you post the code you have so far. and please use codetags "<>" for that
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
#include <fstream>
#include <iostream> 


//This saves me writing std; all the way through the programme/ 
using namespace std;


int main () 

{

	// Create an ifstream input stream for reading of data from the file./
	ifstream outFile;
	outFile.open("Task 3 data.txt", ifstream::in +ifstream::binary); 

	//These are the different variables I will use, I have appreviated these so it will be quicker for me to type them eg. j1l stands for journey1length./ 
	int time, j1l, j2l, j3l, usermins, userhours, usertime; 
	
	cout <<"Journey Planner" <<endl;

	//This will show this message on the screen and the user will enter how long they want to spend on their journey./ 
	cout <<"Please enter the max time you would like to spend on your journey " <<endl;
	cout <<"Hours" <<endl; 
	cin >> userhours; 
	cout <<"Minutes" <<endl; 
	cin >> usermins; 
	
	usertime=userhours*60+usermins;

	//This is going to show the user how many minutes they will be travelling for./
	cout <<"The total time you will be traveling is ";
	cout << usertime; 
	cout << " minutes"; 

	while (outFile)

	{
		

	//This will keep the programme open./
	cin.ignore(); 
	cin.get(); 
	return 0; 



}  
what excactly is your problem? where are you stuck right now?

also dont use variables like j1l and explain them with comments, just use
journey1length instead. you can use autofill or copypaste if you are getting lazy
Last edited on
I have to include a nested loop in the program that will take the usertime and then look in this file

Journey One
1
200 90
100 60

Journey Two
0
300 120

Journey Three
2
50 50
50 75
100 60


and then will tell you by the time you have entered what journey is the best to take. The ones that aren't good enough will only be shown to the screen but the journeys that can be taken will be shown to the screen and to a file.
ok thats the task, but what is your problem?

I wont write this programm for you, since it's your homwork, but i can help you with it
Last edited on
No idea how to/where to start with the loops or how to get the program to understand what is in the file.
you need to open the file and read its data. this is explained here: http://www.cplusplus.com/doc/tutorial/files/


i didnt really get the traveling problem. does the user select some of these 3 journeys and you simply add the times of each journey?
The user enters a time they want to take (enters hours and minutes) for example they will put in 2 hours and 30 minutes then my program will show text saying "Your total journey time is 150 minutes"

From there my program is meant to get the files and look what journey is best for them.
I have in the past created a program where it reads from another file, but that was very simple and I just entered my own numbers in to it and it would read it line by line, but this time it is meant to read from this file


Journey One
1
200 90
100 60

Journey Two
0
300 120

Journey Three
2
50 50
50 75
100 60
you can use your old programm, reading the data line by line is ok. just make sure you use something to seperate the different journeys.

for example you could read line by line until you reach an empty line or the end of the file
Do you know what I should put for my nested loop?
Last edited on
asking for pms is not a good idea, since others cant read them, but might help you with that problem.

and no i dont have an idea about your nested loop.

i would try to read the data from the file and save it somewhere as a first task. it's explained in the link i posted earlier. once you're done with that or need help ask again
Thank you!
Topic archived. No new replies allowed.