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.
#include <fstream>
#include <iostream>
//This saves me writing std; all the way through the programme/
usingnamespace 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
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.
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
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