Struggling with read file

I used the tutorial to get the read file working, however, I can not fahtom how to avoid reading all lines.

Basically what I want to do is to have a couple of the lines together. For instance:
Line 1 = Think of a number between
Line 2 = and
Line 3 = Press ENTER when you're done.

I want these 3 lines to be printed on one line, WITH some of my INT variables.
For example: cout << Line 1 << lowest << Line 2 << highest << "." << Line 3 << endl;

This is what I have 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
64
65
66
67
68
69
70
71
//Assignment 1 (Extra) by Nillen

#include <iostream>
#include <fstream> 
#include <string>
using namespace std;

int main()
{ //Main opening bracket

	int tries, highest, lowest; //Create the variables
	string file; //Create the variables

	lowest = 1; //Starting value of lowest guess
	highest = 100; //Starting value of max guess
	tries = 0; //To increase once per turn

	cout << "Which language would you prefer?\nVilket språk föredrar du?\nどの言語が好きですか\n";
	cout << "1: English\n";
	cout << "2: Svenska\n"; 
	cout << "3: 日本語\n"; 

	while(true) 
	{ //Infinite while opening bracket
		int LangPref; //User's choice
		cin >> LangPref;  

		if (LangPref == 1)
		{
			file = "english.txt";
			break;
		}
		else if (LangPref == 2) 
		{
			file = "swedish.txt";
			break;
		}
		else if (LangPref == 3) 
		{
			file = "japanese.txt";
			break;
		}
		else 
		{
			cout << "Error: " << LangPref << "is not valid.\n";
		}
	} //Infinite while closing bracket

	cout << "You chose: " << file << endl;

	string line;
	ifstream myfile (file);


	while ( getline(myfile,line) ) 
	{ 


		cout << line << endl;


	} 




	cin.get();
	return 0;


} //Main closing bracket 
I can not fahtom how to avoid reading all lines. I can't fathom what exactly your question is either? Are you asking how to read in from a file or are you asking how to get specific lines from a file? Or are you saying that you successfully read through your desired file and got all of the file but only want specific parts of the file?
Last edited on
I want to get some specific lines from the file.

I've tried using stuff like
1
2
3
Line.1 = getline(myfile,1) 
Line.2 = getline(myfile,2)
Line.3 = getline(myfile,3)

and
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
	string line;
	ifstream myfile (file);
	int lines;
	string line1;
	string line2;
	string line3;
	while ( getline(myfile,line) ) 
	{ 
		lines++;
		cout << "line: " << lines << endl;
		if (lines == 1)
		{
			line1 = line;
		}
		else if (lines == 2) 
		{
			line2 = line;
		}
		else if (lines == 3) 
		{
			line3 = line;
		}

		else if (lines > 3)
		{
			cout << line1 << lowest << line2 << highest << "." << line3 << endl;
		}

	} 

and stuff which I assumed would work, but to no avail.
Last edited on
Topic archived. No new replies allowed.