Taking numbers from txt storing as int

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?
Post your code.
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
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

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
49
50

#include "stdafx.h"
#include <fstream> 
#include <iomanip>
#include <string>
#include <iostream>
using namespace std;


int _tmain(int argc, _TCHAR* argv[])
{
	int usertime,changes0,changes1,distance,speed;
	string journey;
	usertime = 0;

	ifstream inFile;
    inFile.open("journeydata.txt", ifstream::in+ ifstream::binary);

//Get user time in minutes//
cout << "Please enter a time in minutes: ";
//store user time 
cin >> usertime;


do {
	string journey;
	
	
	getline (inFile,journey);
	
	cout << journey << endl;
	 
	inFile >> changes1;
	cout << "Number of changes:" << changes1 << endl;


	


} while (usertime < 1);





	cin.get();
	return 0;

}
Last edited on
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");

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.

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:

1
2
3
4
5
6
7
while (getline(inFile, journey)) // while you can get a value for journey
{
    // get number of changes
    // get some pairs of numbers
    // ignore some newlines
    // store the data you got somewhere (maybe)
}

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
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
So I should just delete the loop I've done there and use the while loop instead?

Yes, that would look better.

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.

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?

Pretty sure I can use anything really as long as I can explain what they do

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
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 :)
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?
[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:

1
2
3
4
5
6
7
8
/* ... */

int distance;
int speed;

inFile >> distance >> speed;

/* ... */

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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <sstream>

/* ... */

int distance;
int speed;

std::string line;
getline(inFile, line);

std::istringstream inStream(line);
inStream >> distance >> speed;

/* ... */

Useful link -> http://www.cplusplus.com/reference/sstream/stringstream/
Last edited on
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


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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63

#include "stdlib.h"
#include "stdio.h"
#include "stdafx.h"
#include <fstream> 
#include <iomanip>
#include <string>
#include <iostream>
#include <sstream>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{

double usertime,change,distance,speed,time1,time2,distance1,speed2,time3,whitespace;


string journey;
string time;

ifstream inFile;
 inFile.open("journeydata.txt", ifstream::in);

 cout << "Please enter a time in minutes ";
 cin >> usertime;

 while(getline(inFile,journey))
 {

cout << journey <<endl;
inFile >> change;



inFile >> distance >> speed;
inFile >> distance1 >> speed2;

time1 = distance / speed;

time2 =  distance1 / speed2;

time3 = time1 + time2 * 60;

cout << time3 << endl;


if (time3 <= usertime)

	cout << "You can complete the journey in the time you have entered" << endl;

else if (time3 > usertime)

cout << "You are unable to complete this journey" << endl;



 cin.get();
 }


	return 0;
}
Last edited on
The code you posted wouldn't work if your file looked like this, though:

Journey One
2
50 50
50 75
100 60

Journey Two
0
300 120

Journey Three
1
200 90
100 60

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
Topic archived. No new replies allowed.