| SCozzi (6) | |
|
Hey guys, I've been looking around for a while and can't seem to find anything useful. I'm trying to get my program to take data from a text file and store them into two different variables. The problem is the data has to be laid out like this. 20 68 300 57 etc I'm having problems getting the two numbers from the one line and storing them, any suggestions would be amazing? | |
|
|
|
| m4ster r0shi (2047) | |
| Post your code. | |
|
|
|
| Incis B (92) | |
|
int x,y; scanf ( " %d %d " , &y, &z ) ; or better yet,... fscanf fscanf( fp , " %d %d " , &y, %z ) ; rem if ( ( fp = fopen ( "myFile.txt" , "r" )) == NULL ) { fprint(stderr, "oops"\n "); exit(1); } rem use stdio.h & stdlib.h | |
|
Last edited on
|
|
| SCozzi (6) | |||
|
My program has to take a time from the user in minutes, store it then have it read data and take data from a file display it and work out if they can complete the trip in a certain time. Any help at all would be brilliant thanks again Heres the data from the text file: Journey One 1 200 90 100 60 Journey Two 0 300 120 Journey Three 2 50 50 50 75 100 60
| |||
|
Last edited on
|
|||
| m4ster r0shi (2047) | ||||
inFile.open("journeydata.txt", ifstream::in+ ifstream::binary);Your file is a text file. You don't want to open it in binary mode. What you want here is: inFile.open("journeydata.txt", ifstream::in);Also, the std::ifstream::open function opens the file for input by default, so, you could just write: inFile.open("journeydata.txt");
Let's focus on the bold part first. For a single record: Getting the journey value from the file looks ok. So does getting the changes value. After that, you'll have to get some more values. Their number depends on the changes value you just read. You'll need a for loop and (maybe) something to store these values in. Are you allowed to use std::vector? (Dynamic) Arrays? For the entire file: You'll have to keep reading records until you reach the end of file. You could do something like this:
This may not be necessary, depending on what you want to do with the file data (it's not clear to me yet), but if you want to, you could store it in several arrays or in an array of a properly defined struct. Do you know how to / Are you allowed to use structs? PS: Edit your post above, re-enter your code and put [code][/code] tags around it. | ||||
|
Last edited on
|
||||
| SCozzi (6) | |
|
Pretty sure I can use anything really as long as I can explain what they do, using that while loop for the whole file would defiantly make more sense didn't really occur to me. So I should just delete the loop I've done there and use the while loop instead? The reason I'm looking to get the value of the two numbers from the one line is so I can divide the first against the second (distance/speed), then compare it against the users value and tell them if they can make the journey. Thanks again | |
|
Last edited on
|
|
| m4ster r0shi (2047) | ||||
Yes, that would look better.
I see. But why are there several pairs in some journeys? How do you use them? Do you add the times corresponding to these pairs and test the sum against usertime for each journey? Something else?
That's interesting. Take a look here then -> http://www.cplusplus.com/doc/tutorial/structures/ (if you haven't already), and try to define a struct able to hold the information of a record of your file. Post your updated code. EDIT: You could define a separate struct to hold a distance-speed pair, and then use an array [1] or a std::vector [2] of such structs in your record struct to hold the list of distance-speed pairs. [1] http://www.cplusplus.com/doc/tutorial/arrays/ [2] http://www.cplusplus.com/reference/vector/vector/ | ||||
|
Last edited on
|
||||
| SCozzi (6) | |
|
The program is based on a travelling by train, the changes means the amount of times you have to switch trains. The sets of data with the two values of two numbers represent two journeys, so I will have to take both of the values do distance/speed and add the two results together. Have a feeling I'm digging myself into a hole with this program, cheers for the links will have look :) | |
|
|
|
| SCozzi (6) | |
| I've managed to convert the two values from the columns into a string, how would I go about chopping those two values from the string and storing them in integers? | |
|
|
|
| m4ster r0shi (2047) | |||||
|
[EDIT] It's probably better to store these numbers as doubles and not integers, otherwise you'll have a small issue when doing the distance/speed division. If you store them as ints, the 50 / 75 division will yield 0. Not what you want. To get the expected result (0.66...) you'll have to either cast at least one of your values to a double before dividing, or simply store them as doubles. http://bytes.com/topic/c/answers/585097-divide-2-integers-get-double-result [/EDIT] You don't have to convert them into a string. You can simply do this:
If you really want to convert them into a string first, which, on second thought, may be a better option, since this way you also get rid of the newline character at the end of the line, you could do something like this:
Useful link -> http://www.cplusplus.com/reference/sstream/stringstream/ | |||||
|
Last edited on
|
|||||
| SCozzi (6) | |||
|
You legend thanks so I've pretty much done all of the working out for the first journey an I'm able to display if the user can make the journey or not, one last question (sorry for about this pretty new to it) how would I go about it like you said turn this into a loop so it automatically goes on to the next journey with in the file? Code so far
| |||
|
Last edited on
|
|||
| m4ster r0shi (2047) | ||
The code you posted wouldn't work if your file looked like this, though:
You'll have to do something more generic than what you have here. After you get change, you'll need a for loop [1] that will run change + 1 times. In each iteration of this for loop, you'll get a distance-speed pair from the file, perform the division and add the result to a totalTime variable declared and initialized to zero above the for loop. After the loop ends, you'll test totalTime against usertime. If you do this, you'll also have solved the problem of getting the data for the rest of the journeys in your file. EDIT: Well, most of it... You'll have to also add a couple of inFile.get(); statements after the for loop, in order to get rid of a newline character or two...[1] http://www.cplusplus.com/doc/tutorial/control/#for | ||
|
Last edited on
|
||